Hello,
need some help as I am new to Docker and having troubles configuring a NGINX, Django and PostgreSQL environment.
I have created in a Ubuntu server a docker with the 3 containers. I can start PostgreSQL and i can access it from the psql client on db container ip port 5432 and on localhost on port 55432, but django cannot access it on the db container IP and port 5432.
The error on the start on the db container is:
smartblock-api-1 | django.db.utils.OperationalError: could not connect to server: Connection timed out
smartblock-api-1 | Is the server running on host "db" (172.27.0.2) and accepting
smartblock-api-1 | TCP/IP connections on port 5432?
But commands:
psql -h 172.27.0.2 -p 5432 -U sbmadmin -d sbm
administrator@smartblock:~/git/smartblock$ psql -h 172.27.0.2 -p 5432 -U sbmadmin -d sbm
Password for user sbmadmin:
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1), server 11.7 (Debian 11.7-2.pgdg90+1))
Type "help" for help.
sbm=#
See files below.
What am I missing in the configuration?
docker-compose.yml
version: '3.8'
services:
api:
build:
context: ./SmartblockAPI/SmartblockAPI
dockerfile: ./Dockerfile
command: python /usr/src/app/manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app/
- api_volume:
ports:
- 8000:8000
env_file:
- ./SmartblockAPI/SmartblockAPI/.env
depends_on:
- db
db:
image: postgres:11.7
init: true
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 55432:5432
environment:
- POSTGRES_USER=sbmadmin
- POSTGRES_PASSWORD=999999
- POSTGRES_DB=sbm
spa:
build:
context: ./SmartblockSPA/SmartblockSPA
dockerfile: ./Dockerfile
volumes:
- static_volume:/usr/share/nginx/html
ports:
- 80:80
depends_on:
- api
volumes:
postgres_data:
static_volume:
api_volume:
Dockerfile for NGINX
# production environment
FROM nginx:stable
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./dist/ /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Dockerfile for Django
FROM python:3.9.13-buster
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y --no-install-recommends \
unixodbc-dev \
unixodbc \
libpq-dev \
libglib2.0-dev
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \
&& echo "deb http://download.mono-project.com/repo/debian stretch/snapshots/5.20 main" > /etc/apt/sources.list.d/mono-official.list \
&& apt-get update \
&& apt-get install -y clang \
&& apt-get install -y mono-devel=5.20\* \
&& rm -rf /var/lib/apt/lists/* /tmp/*
RUN pip install --upgrade pip
RUN pip install pycparser \
&& pip install pythonnet==2.5.2 \
&& pip install gunicorn==20.0.4
RUN python --version
ADD requirements.txt /usr/src/app/requirements.txt
RUN pip install -r /usr/src/app/requirements.txt
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN mkdir /usr/logs
CMD ["gunicorn", "--workers=2", "--bind=0.0.0.0:8000", "SmartblockAPI.wsgi:application"]
.env file for django container variables
DEBUG=1
SECRET_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DJANGO_ALLOWED_HOSTS=127.0.0.1 10.0.0.133 localhost
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=sbm
SQL_USER=sbmadmin
SQL_PASSWORD=999999
SQL_HOST=db
SQL_PORT=5432
SQL_OPTIONS=-c search_path=sbm
LOG_LEVEL=INFO
NGINX conf file
# auto detects a good number of processes to run
worker_processes auto;
#Provides the configuration file context in which the directives that affect connection processing are specified.
events {
# Sets the maximum number of simultaneous connections that can be opened by a worker process.
worker_connections 8000;
# Tells the worker to accept multiple connections at a time
multi_accept on;
}
http {
# what times to include
include /etc/nginx/mime.types;
# what is the default one
default_type application/octet-stream;
# Sets the path, format, and configuration for a buffered log write
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $upstream_addr '
'"$http_referer" "$http_user_agent"';
server {
# listen on port 80
listen 80;
# save logs here
access_log /var/log/nginx/access.log compression;
# where the root here
root /usr/share/nginx/html;
# what file to server as index
index index.html index.htm;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to redirecting to index.html
try_files $uri $uri/ /index.html;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# Javascript and CSS files
location ~* \.(?:css|js)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
}
}
Thanks in advance,
Pedro