1

I can't find solution, please help!

I have Dockerfile

FROM python:3

ENV PYTHONUNBUFFERED=1

RUN mkdir /app
WORKDIR /app

RUN pip install Django \
    && pip install psycopg2 \
    && pip install jinja2 \
    && pip install Pillow

COPY . /app/

And docker-compose.yaml

version: "3"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=folivora
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432:5432

  site:
    image: folivora:latest
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/app
    depends_on:
      - db
    ports:
      - 8000:8000

And settings.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'folivora',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': 'db',
    'PORT': '5432',
}

}

And ERROR when run "docker-compose up"

site_1  | django.db.utils.OperationalError: could not connect to server: Connection refused
site_1  |   Is the server running on host "db" (192.168.224.2) and accepting
site_1  |   TCP/IP connections on port 5432?

When I edit docker-compose.yaml, change string command to:

command: bash -c "python manage.py runserver 0.0.0.0:8000"

all is fine. So, migration line broke my code, but i don't know why.

I try to create empty django project to check the same config. On first "docker-compose up", all started and working fine, but from second start, all is broke again with the same error. Over time this problem gone from test project, I dont know why, maby cache or something...

New info

  1. Run docker-compose up with command command: bash -c "python manage.py runserver 0.0.0.0:8000" in docer-compose.yaml and catch error.

  2. Then Ctrl+C (exit from docker compose "shell") but don't do docker-compose down, because this command delete created container.

  3. Run docker-compose up with command command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000" in docer-compose.yaml and all is successfull started.

I think it is bad solution.

1
  • I think the problem is that your Django app starts calling migration commands before your database becomes actually running. Note that depends_to only manages order of creation of containers, not their readiness. Can you try wait-for-it db:5432 before migrations? Commented Oct 17, 2020 at 10:32

1 Answer 1

1

According to your edit, you don't have any networking issues.

The problem is that your Django app starts calling migration commands before your database becomes actually running (you can check that in postgres logs). Also note that depends_to only manages order of creation of containers, not their readiness.

You can use wait-for-it to wait until your postgres port is available. So, your command would be:

bash -c "wait-for-it db:5432 -- python manage.py migrate && python manage.py runserver 0.0.0.0:8000"

If your image doesn't have this package installed, add apt install wait-for-it to your Dockerfile.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it is help. But in Dockerfile I add not RUN apt install wait-for-it but RUN apt-get -q update && apt install wait-for-it
FileNotFoundError: [Errno 2] No such file or directory: 'db:5432'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.