I am trying to connect Database container form offical Postgres image with Django container. But it can’t find Postgres container. I checked Firewall is enabling specifyed ports, containers are under same network. Project works on localhost with Docker Desktop. But can’t connect to Postgres container on Ubuntu Server
my docker-compose looks like this:
version: '3.8'
services:
qutqaruv:
build: ./app
command: gunicorn qutqaruv_core.wsgi --bind 0.0.0.0:8001
volumes:
- ./app/:/usr/src/app/
- static_volume:/static
ports:
- "8001:8001"
env_file:
- ./app/.env.dev
depends_on:
qutqaruv-db:
condition: service_healthy
restart: always
nginx:
image: nginx:1.22.1
ports:
- "82:8081"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
- static_volume:/static
depends_on:
- qutqaruv
qutqaruv-db:
image: postgres:15
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U qutqaruv -d qutqaruv_dev" ]
interval: 5s
timeout: 5s
retries: 5
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=qutqaruv
- POSTGRES_PASSWORD=qutqaruv
- POSTGRES_DB=qutqaruv_dev
volumes:
postgres_data:
static_volume:
Dockerfile:
# pull official base image
FROM python:3.11.2-slim-buster
# set working directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# updated
# install system dependencies
RUN apt-get update \
&& apt-get -y install gcc postgresql \
&& apt-get clean
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# new
# copy entrypoint.sh
RUN apt-get update && apt-get install -y netcat
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
# add app
COPY . .
# new
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
entrypoint.sh:
#!/bin/sh
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
echo $SQL_HOST $SQL_PORT
sleep 0.1
echo "Got lost in this process"
done
echo "PostgreSQL started"
python manage.py flush --no-input
python manage.py migrate
python manage.py collectstatic --no-input
exec "$@"