Issue
I'm trying to setup my django project with docker. It will have multiple containers one is for code that is server, worker for celery, redis for redis, db for postgres and there is nginx. Every one of them are running without any error but the localhost:8080 is not accessible. I tried accessing localhost:8000 directly and it is still not accessible. Here's all the relevant configuration files of docker.
the docker-compose.yml
is as:
version: '2'
services:
nginx:
restart: always
image: nginx:1.23-alpine
ports:
- 8080:8080
volumes:
- /default.conf:/etc/nginx/conf.d
- static_volume:/app/django_static
server:
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
entrypoint: /app/server-entrypoint.sh
volumes:
- static_volume:/app/django_static
expose:
- 8000
environment:
DEBUG: "True"
CELERY_BROKER_URL: "redis://redis:6379/0"
CELERY_RESULT_BACKEND: "redis://redis:6379/0"
DJANGO_DB: postgresql
POSTGRES_HOST: db
POSTGRES_NAME: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_PORT: 5432
worker:
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
entrypoint: /app/worker-entrypoint.sh
volumes:
- static_volume:/app/django_static
environment:
DEBUG: "True"
CELERY_BROKER_URL: "redis://redis:6379/0"
CELERY_RESULT_BACKEND: "redis://redis:6379/0"
DJANGO_DB: postgresql
POSTGRES_HOST: db
POSTGRES_NAME: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_PORT: 5432
depends_on:
- server
- redis
redis:
restart: unless-stopped
image: redis:7.0.5-alpine
expose:
- 6379
db:
image: postgres:13.0-alpine
restart: unless-stopped
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
expose:
- 5432
volumes:
static_volume: {}
postgres_data: {}
Dockerfile
:
#!/bin/bash
# Base image
FROM python:3.11.4
# Set working directory
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install gunicorn
ADD ./requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
ADD ./server-entrypoint.sh /app/
ADD ./worker-entrypoint.sh /app/
RUN chmod +x /app/server-entrypoint.sh
RUN chmod +x /app/worker-entrypoint.sh
server-entrypoint.sh
:
#!/bin/bash
# Run database migrations or initialization tasks
echo "Running database migrations..."
python manage.py makemigrations
python manage.py migrate
# Start the development server
echo "Starting Django application..."
gunicorn core.wsgi --bind 0.0.0.0:8000 --workers 4 --threads 4
worker-entrypoint.sh
:
#!/bin/bash
echo "Starting celery worker..."
# Run a worker
celery -A core worker --loglevel=info --concurrency 1 -E
and lastly the default.conf is as:
server {
listen 80;
server_name _;
server_tokens off;
client_max_body_size 20M;
location / {
try_files $uri @proxy_api;
}
location /admin {
try_files $uri @proxy_api;
}
location @proxy_api {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://server:8000;
}
location /django_static/ {
autoindex on;
alias /app/django_static/;
}
}
this is the whole logs of the containers:
docker compose up
[+] Running 5/5
✔ Container celery-redis-django-nginx-1 Created 0.0s
✔ Container celery-redis-django-db-1 Created 0.0s
✔ Container celery-redis-django-redis-1 Created 0.0s
✔ Container celery-redis-django-server-1 Recreated 0.3s
✔ Container celery-redis-django-worker-1 Recreated 0.3s
Attaching to db-1, nginx-1, redis-1, server-1, worker-1
server-1 | exec /app/server-entrypoint.sh: no such file or directory
nginx-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx-1 | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
redis-1 | 1:C 26 Jan 2024 09:28:48.558 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1 | 1:C 26 Jan 2024 09:28:48.559 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis-1 | 1:C 26 Jan 2024 09:28:48.559 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-1 | 1:M 26 Jan 2024 09:28:48.559 * monotonic clock: POSIX clock_gettime
redis-1 | 1:M 26 Jan 2024 09:28:48.562 * Running mode=standalone, port=6379.
redis-1 | 1:M 26 Jan 2024 09:28:48.562 # Server initialized
redis-1 | 1:M 26 Jan 2024 09:28:48.562 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-1 | 1:M 26 Jan 2024 09:28:48.562 * Ready to accept connections
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
db-1 |
db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-1 |
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: using the "epoll" event method
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: nginx/1.23.4
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: OS: Linux 5.15.133.1-microsoft-standard-WSL2
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker processes
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 20
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 21
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 22
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 23
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 24
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 25
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 26
nginx-1 | 2024/01/26 09:28:51 [notice] 1#1: start worker process 27
db-1 | 2024-01-26 09:28:53.150 UTC [1] LOG: starting PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
db-1 | 2024-01-26 09:28:53.150 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2024-01-26 09:28:53.150 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2024-01-26 09:28:53.159 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2024-01-26 09:28:53.170 UTC [22] LOG: database system was shut down at 2024-01-26 08:45:06 UTC
db-1 | 2024-01-26 09:28:53.180 UTC [1] LOG: database system is ready to accept connections
worker-1 | Starting celery worker...
server-1 exited with code 0
worker-1 | /usr/local/lib/python3.11/site-packages/celery/platforms.py:829: SecurityWarning: You're running the worker with superuser privileges: this is
worker-1 | absolutely not recommended!
worker-1 |
worker-1 | Please specify a different user using the --uid option.
worker-1 |
worker-1 | User information: uid=0 euid=0 gid=0 egid=0
worker-1 |
worker-1 | warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
worker-1 |
worker-1 | -------------- celery@8d0c0884d6ae v5.3.6 (emerald-rush)
worker-1 | --- ***** -----
worker-1 | -- ******* ---- Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.36 2024-01-26 09:29:03
worker-1 | - *** --- * ---
worker-1 | - ** ---------- [config]
worker-1 | - ** ---------- .> app: core:0x7f97b38c6890
worker-1 | - ** ---------- .> transport: redis://redis:6379/0
worker-1 | - ** ---------- .> results: redis://redis:6379/0
worker-1 | - *** --- * --- .> concurrency: 1 (prefork)
worker-1 | -- ******* ---- .> task events: ON
worker-1 | --- ***** -----
worker-1 | -------------- [queues]
worker-1 | .> celery exchange=celery(direct) key=celery
worker-1 |
worker-1 |
worker-1 | [tasks]
worker-1 | . core.celery.debug_task
worker-1 | . send_email_task
worker-1 |
worker-1 | [2024-01-26 09:29:03,762: WARNING/MainProcess] /usr/local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py:507: CPendingDeprecationWarning: The broker_connection_retry configuration setting will no longer determine
worker-1 | whether broker connection retries are made during startup in Celery 6.0 and above.
worker-1 | If you wish to retain the existing behavior for retrying connections on startup,
worker-1 | you should set broker_connection_retry_on_startup to True.
worker-1 | warnings.warn(
worker-1 |
worker-1 | [2024-01-26 09:29:03,873: INFO/MainProcess] Connected to redis://redis:6379/0
worker-1 | [2024-01-26 09:29:03,874: WARNING/MainProcess] /usr/local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py:507: CPendingDeprecationWarning: The broker_connection_retry configuration setting will no longer determine
worker-1 | whether broker connection retries are made during startup in Celery 6.0 and above.
worker-1 | If you wish to retain the existing behavior for retrying connections on startup,
worker-1 | you should set broker_connection_retry_on_startup to True.
worker-1 | warnings.warn(
worker-1 |
worker-1 | [2024-01-26 09:29:03,878: INFO/MainProcess] mingle: searching for neighbors
worker-1 | [2024-01-26 09:29:04,887: INFO/MainProcess] mingle: all alone
worker-1 | [2024-01-26 09:29:04,901: INFO/MainProcess] celery@8d0c0884d6ae ready.
server-1 | exec /app/server-entrypoint.sh: no such file or directory
server-1 | exec /app/server-entrypoint.sh: no such file or directory
server-1 | exec /app/server-entrypoint.sh: no such file or directory
server-1 | exec /app/server-entrypoint.sh: no such file or directory
server-1 | exec /app/server-entrypoint.sh: no such file or directory
server-1 exited with code 1
Solution
Try this for a quick fix. Once you have fixed your port mappings. Remove entrypoint in your docker-compose config for the django app. Add this inplace of entrypont.
command: >
sh -c "python manage.py makemigrations &&
python manage.py migrate &&
gunicorn core.wsgi:application --bind 0.0.0.0:8000 --workers 4"
Answered By - Mess
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.