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
Run
docker-compose upwith commandcommand: bash -c "python manage.py runserver 0.0.0.0:8000"in docer-compose.yaml and catch error.Then Ctrl+C (exit from docker compose "shell") but don't do
docker-compose down, because this command delete created container.Run
docker-compose upwith commandcommand: 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.
depends_toonly manages order of creation of containers, not their readiness. Can you trywait-for-it db:5432before migrations?