0

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

jackygeee
  • 45
  • 8

1 Answers1

0

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • first of all thanks for your reply - my question is: i used github actions (ci/cd pipeline) to create an image automatically when pushing to the repository... so I thought now from the host where I want to run my container I pull that image (which is including all the steps i defined in my docker-compose file) from github packages and run that image. Or am I having a comprehension problem ? – jackygeee Mar 22 '21 at 18:38
  • The image doesn't include any of the steps in the `docker-compose.yml` file, only a single specific `build:` block and the corresponding Dockerfile. – David Maze Mar 22 '21 at 19:30
  • Okey so I can only build images from single docker files - so docker-compose files can’t be used in ci/cd pipelines? – jackygeee Mar 22 '21 at 19:44
  • You can `docker-compose build` the images and `docker-compose push` them, but that only contains the built images and not the instructions on how to run them. – David Maze Mar 22 '21 at 19:59
  • I think I misunderstood the concept of how to build an application with docker. So I think i should run my python script in one container and the database in another container. Still not sure, how the production flow is when programming locally and then run on the production server but I hope I figure that out. Thanks for your help :) – jackygeee Mar 22 '21 at 21:14