1

Few days ago I asked a question about a Postgres error.

I followed your suggestions and they helped a bit but in addition to not solving my problem some new problems arose.

I have a django-postgres app which works locally with no problems. When I try to build a docker image it builds but when I try to set up the container I have the following error:

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

I'll show you my Dockerfile:

# Origin image
FROM python:3.8

RUN apt-get update

# Define directory
RUN mkdir /project
WORKDIR /project

# Install requirements
RUN apt-get install -y vim
RUN python -m pip install --upgrade pip
COPY requirements.txt /project/
RUN pip install -r requirements.txt
COPY . /project/

# Expose some ports
EXPOSE 22 5432 8080 8009 8000

# default command
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

And here is my docker-compose file:

version: "3.3"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_NAME=plataforma
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=administrador
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=plataforma
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=administrador
    depends_on:
      - db
    env_file:
      - ./plataforma/.env

On settings.py I configure the database on this way:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env('POSTGRESQL_NAME'),
        'USER': env('POSTGRESQL_USER'),
        'PASSWORD': env('POSTGRESQL_PASS'),
        'HOST': env('POSTGRESQL_HOST'),
        'PORT': env('POSTGRESQL_PORT'),
    }
}

And this is my .env file:

POSTGRESQL_NAME=plataforma
POSTGRESQL_USER=admin
POSTGRESQL_PASS=administrador
POSTGRESQL_HOST=db
POSTGRESQL_PORT=5432

When I run my app locally I use localhost instead db for POSTGRESQL_HOST. Now, when I run $ sudo docker-compose run web python manage.py runserver . the image builds and the database container is running, but the app container is stopped. If I run $ docker start container-name it doesn't start.

If I run $ docker run -d --restart always --name new-container-name image-name a new container starts correctly and if I get inside it and try to make django migrations, i have the same error:

/usr/local/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not translate host name "db" to address: Name or service not known

Maybe I am using the docker-compose file in a wrong way. I've tried to install postgres from Dockerfile direct but there I have the error from my last question:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

Also I tried this solution and this solution but they didn't work either.

So, I am literally lost. I also have read this docker Quickstart but I don't figure out of what I'm doing wrong.

Can anyone help me?

Sorry if I'm misunderstanding some stackoverflow rules, but I'm still figuring out how it works.

Thankyou!

EDIT: Thanks to @DavidMaze 's help we found out what the problem was! David said:

The postgres image accepts an environment variable POSTGRES_DB to set the initial database name, not POSTGRES_NAME; does changing this (and deleting the ./data/db host directory) help?

So I Changed POSTGRES_NAME variable to POSTGRES_DB and erased /data folder (and purged images and containers list) and ran docker-compose up At the beginning when I run that command for the first time it seems like it try to setup first the web container and fails (with OperationalError), but when I run the same command again it works without errors.

Thank you so much for the help!

Daniela
  • 29
  • 3
  • The usual way to start up a Compose file like this is `docker-compose up`; you can pass a `-d` option to have it start in the background, but if you think things aren't starting up correctly, leave off that option for now. If the application container is still crashing, what's in its logs? – David Maze Jan 28 '22 at 00:05
  • (`docker run` isn't aware of anything that Compose sets up, including the `default` network, which could result in the "name or service not known" error. You shouldn't normally need `docker start` at all; if you have a container in a strange state, delete it and recreate it the normal way.) – David Maze Jan 28 '22 at 00:06
  • @DavidMaze when I run `docker-compose up` the database seems to start correctly and even gives the message `LOG: database system is ready to accept connections` but suddenly it shows the message `FATAL: database "plataforma" does not exist`. The logs for the web container also show the same error message. – Daniela Jan 28 '22 at 01:53
  • 2
    The [`postgres` image](https://hub.docker.com/_/postgres) accepts an environment variable `POSTGRES_DB` to set the initial database name, not `POSTGRES_NAME`; does changing this (and deleting the `./data/db` host directory) help? – David Maze Jan 28 '22 at 02:11
  • That worked! It's strange because when I run `docker-compose up` it seems like it try to setup first the web container and fails (with OperationalError), but when I run the same command again it works without errors! Thank you so much @DavidMaze ! – Daniela Jan 28 '22 at 04:42

0 Answers0