Answer a question

I've been trying for a few hours now but no solution from similar asked questions seem to work for me... I am using docker-compose to setup a postgresql database and run a python webserver from where I want to connect to my postgressql database (so it's running inside the container)

version: '3.8'

services:
  database:
    container_name: database
    hostname: database
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    volumes:
      - postgres:/pgdata
      - ./application/ressources/fixtures.sql:/docker-entrypoint-initdb.d/fixtures.sql 
    ports:
      - "5432:5432"

  application:
    container_name: application
    build: .
    ports:
      - "5001:5001"
    volumes: 
      - ./application:/application
    restart: always
    depends_on:
      - database

volumes:
    postgres:

I trying to connect as follows ( I have read that despite the depends on in my dockerfile the database needs some more time until it can accept connections so i added a retry logic):


        retries = 0
        while retries < 5:
            retries = retries + 1
            self.conn = psycopg2.connect(user='postgres', password='password',
                                         host='database', port="5432", database='mydatabase')
            if not self.conn:
                logging.info("retry to connect")
                sleep(5)

The weird thing is that when running it with docker-compose -f docker-compose.yml up everything works fine. But when I built the image (docker build -t myapp:0.1) and run it (docker run myapp:0.1) it gives me the following error:

File "/application/libraries/database.py", line 18, in establishConnection
    self.conn = psycopg2.connect(user=CONFIG.DATABASE_USER, password=CONFIG.DATABASE_PASSWORD,
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "database" to address: Name or service not known

I've read that when using docker-compose a single network is created, so this can't be the error here i guess Docker Compose doku

Thanks in advance, Jacky

Answers

If you run docker run on an image, it does only what's on that command line and no more. In particular, the plain docker commands don't know about docker-compose.yml and any of the settings you might specify there.

The short answer is to always use docker-compose up to launch the containers.

In principle you could translate the docker-compose.yml file into explicit docker commands. At a minimum you'd need to manually create a Docker network and specify the container names:

docker network create app-net
docker run -d --net app-net --name database -p 5432:5432 postgres
docker run -d --net app-net -e PGHOST=database -p 5001:5001 myapp:0.1

This hasn't included the other options in the Compose setup, though, notably database persistence. There are enough settings that you'd want to write them down in a file, and at that point you've basically reconstructed the Compose environment.

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐