0

I have a Django app that runs locally with no problems. Now, I want to get a docker image of my app but when I try to build it, it gives me the following error:

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?

I am new on Django and Docker development and i was looking for similar questions but none answer solved my problem.

I'll show you my Dockerfile, I have copied the Postgres commands from another project that does work:

FROM python:3.8

RUN apt-get update

RUN mkdir /project
WORKDIR /project

RUN apt-get install -y vim
COPY requirements.txt /project/
RUN pip install -r requirements.txt
COPY . /project/

# Install postgresql
RUN apt install -y postgresql postgresql-contrib
RUN service postgresql start

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role
RUN /etc/init.d/postgresql start &&\
    psql --command "CREATE USER admin WITH SUPERUSER PASSWORD 'passwd';" &&\
    createdb -O admin plataforma

USER root

# setup postgresql
RUN sed -i "/^#listen_addresses/i listen_addresses='*'" /etc/postgresql/13/main/postgresql.conf
RUN sed -i "/^# DO NOT DISABLE\!/i # Allow access from any IP address" /etc/postgresql/13/main/pg_hba.conf
RUN sed -i "/^# DO NOT DISABLE\!/i host all all 0.0.0.0/0 md5\n\n\n" /etc/postgresql/13/main/pg_hba.conf


# running commands of my app
RUN python manage.py makemigrations accounts
RUN python manage.py sqlmigrate accounts 0001
RUN python manage.py migrate
RUN python manage.py inicializar

# Expose some ports
EXPOSE 22 5432 8080 8009 8000

# volumes
VOLUME ["/var/lib/postgresql/12/main"]


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

Here is my requirements.txt file:

#requirements.txt
Django==3.2.8
djangorestframework==3.9.1
gunicorn==19.9.0
pandas==1.3.3
path==16.2.0
six==1.14.0
Pillow==7.0.0
psycopg2>=2.8
django-environ==0.8.1

Here is my settings.py file (the databases fragment):


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

And here is my .env file:

POSTGRESQL_NAME=plataforma
POSTGRESQL_USER=admin
POSTGRESQL_PASS=passwd
POSTGRESQL_HOST=localhost
POSTGRESQL_PORT=5432
DEBUG=True

Some people told me that I could make a docker-compose file so I erased the postgres install commands from Dockerfile and made this:

version: "3.3"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  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=passwd
    depends_on:
      - db

But this doesn't work either.

Footnote: If I use the Django's default database (SQLite) and (obviously) erase postgress commands from Dockerfile, I can build the Docker image without problems, and, again, this app with postgres works good if I run it locally. So, something is happening with Docker + postgres and I don't know what to do.

Somebody can help me? Thank you!

Edit: I erased the migrations commands from Dockerfile and replaced environ var of POSTGRESQL_HOST to db and when I run $ sudo docker-compose run web python manage.py runserver . the image is created but the container not, and when I try to run a container with that image i get the following error: django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

Sorry my english, I still learning it too.

EDIT AGAIN: I finally solved this issue in this question

Thanks for the help!

Daniela
  • 29
  • 3
  • If you are still using compose, you'll have to set `POSTGRESQL_HOST` to `db` – Brian Destura Jan 23 '22 at 22:07
  • You should remove all of the database setup and configuration from the Dockerfile. You can't run migrations (or start a database) from the Dockerfile, it has to be done when the container starts up, or at another point in your application lifecycle. Do the recipes in [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose) help? – David Maze Jan 23 '22 at 22:12
  • Thank you for your answer, @BrianDestura When I use compose and set `POSTGRESQL_HOST` to `db` I get the following error: `django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known` – Daniela Jan 23 '22 at 23:09
  • Hi @DavidMaze I followed the recipes of the Quickstart linked on that post and erased the migrations from Dockerfile. So i can build the image but when I try to run a container it shows me the error: `django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known` – Daniela Jan 23 '22 at 23:12

1 Answers1

0

While creating a superuser your command is

CREATE USER admin WITH SUPERUSER PASSWORD 'administrador'

And in your env file you are using passwd as password.

Change your env from

POSTGRESQL_PASS=passwd

To this

POSTGRESQL_PASS=administrador
user8193706
  • 2,387
  • 2
  • 8
  • 12
  • I'm sorry for the confusion, i've edited the password while I was trying to fix this problem and writing this post and I forgot to edit it here. I have the same password on both files and have the same problem. But i will edit the post to avoid confusions. Thanks! – Daniela Jan 23 '22 at 20:58