Docker-compose Django Supervisord Configuration - python

I would like to run some programs when my django application running. That's why I choose supervisord. I configured my docker-compose and Dockerfile like :
Dockerfile:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# some of project settings here
ADD supervisord.conf /etc/supervisord.conf
ADD supervisor-worker.conf /etc/supervisor/conf.d/
CMD ["/usr/local/bin/supervisord", "-c", "/etc/supervisord.conf"]
docker-compose:
api:
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
restart: unless-stopped
container_name: project
volumes:
- .:/project
ports:
- "8000:8000"
network_mode: "host"
supervisord.conf
[supervisord]
nodaemon=true
[include]
files = /etc/supervisor/conf.d/*.conf
[supervisorctl]
[inet_http_server]
port=*:9001
username=root
password=root
So my problem is when I up the docker-compose project and other dependencies (postgresql, redis) works fine but supervisord didn't work. When I run "supervisord" command inside container it's working. But in startup, It don't work.

Related

Dockerized Django app and MySQL with docker-compose using .env

I would to run my Django project into a Docker container with its Database on another Docker container inside a Bebian
When i run my docker container, I have some errors. Like : Lost connection to MySQL server during query ([Errno 104] Connection reset by peer).
This command mysql > SET GLOBAL log_bin_trust_function_creators = 1 is very important because database's Django user create trigger.
Morever, I use a .env file used same for create DB image to store DB user and password. This path is settings/.env.
My code:
docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:8.0.29
container_name: db_mysql_container
environment:
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
command: ["--log_bin_trust_function_creators=1"]
ports:
- '3306:3306'
expose:
- '3306'
api:
build: .
container_name: django_container
command: bash -c "pip install -q -r requirements.txt &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/app
ports:
- '8000:8000'
depends_on:
- db
Dockerfile :
# syntax=docker/dockerfile:1
FROM python:3.9.14-buster
ENV PYTHONUNBUFFERED=1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
How to start my Django project ? Is possible to start only the DB container ?
What command i need execute and what changes i need to make, I'm novice with Docker ! So if you help me, please explains your commands and actions !
You can find this project on my GitHub
Thank !
To run dockerized django project.
Simply you can run below command:
docker-compose run projectname bash -c "python manage.py createsuperuser"
Above command is used for to create superuser

Docker image ran for Django but cannot access dev server url

Working on containerizing my server. I believe I successfully run build, when I run docker-compose my development server appears to run, but when I try to visit the associated dev server URL:
http://0.0.0.0:8000/
However, I get a page with the error:
This site can’t be reachedThe webpage at http://0.0.0.0:8000/ might be temporarily down or it may have moved permanently to a new web address.
These are the settings on my Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR C:/Users/15512/Desktop/django-project/peerplatform
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
EXPOSE 8000
CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000", "--settings=signup.settings"]
This is my docker-compose.yml file:
version: "3.8"
services:
redis:
restart: always
image: redis:latest
ports:
- "49153:6379"
pairprogramming_be:
restart: always
depends_on:
- redis
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
env_file:
- ./signup/.env
- ./payments/.env
- ./.env
build:
context: ./
dockerfile: Dockerfile
ports:
- "8000:8001"
container_name: "pairprogramming_be"
volumes:
- "C:/Users/15512/Desktop/django-project/peerplatform://pairprogramming_be"
working_dir:
"/C:/Users/15512/Desktop/django-project/peerplatform"
This is the .env file:
DEBUG=1
DJANGO_ALLOWED_HOSTS=0.0.0.0
FYI: the redis image runs successfully. This is what I have tried:
I tried changing the allowed hosts to localhost and 127.0.0.0.1
I tried running the command python manage.py runserver and eventually added 0.0.0.0:8000
When I run docker inspect --format '{{ .NetworkSettings.IPAddress }} pairprogramming_be I get a blank response/my docker container doesn't appear to have an IP Address
where is the 8001 port taken from? this is the internal (expected) listening port. Since you set your application (inside docker) to listen on 8000, you should map it from 8000 to anything else..
just change compose to:
ports:
- "8000:8000"

How to change chdir in python3 with comandline args

How to change chdir in python3 with comandline args like
python3 project_name/manage.py --chdir=/project_name
Actually I try to setup Docker to run flask app with next settings
version: '3'
services:
flask:
build: .
restart: always
container_name: 'project_name'
command: python3 /project_name/manage.py --chdir=/project_name
# command: gunicorn --bind 0.0.0.0:8000 --worker-class=gevent --workers=4 --chdir /flask --reload wsgi:app
volumes:
- .:/flask
ports:
- '8000:8000'
environment:
- PATH=/project_name:$PATH
And after all python says that it cannot find db file, I know why, because chdir is not root of project, chdir is a folder under the project dir
bash -c "cd /project_name && python3 manage.py"

Compose up container exited with code 0 and logs it with empty

I need to containerize a Django Web project with docker. I divided the project into dashboard, api-server and database. When I type docker-compose up, it print api-server exited with code 0 and api-server container Exited (0), and I type docker logs api-server, it return empty, but other container normal. I don't know how to check problem.
api-server directory structure is as follows
api-server
server/
Dockerfile
requirements.txt
start.sh
...
...
Some compose yml content is as follows
dashboard:
image: nginx:latest
container_name: nginx-dashboard
volumes:
- /nginx/nginx/default:/etc/nginx/conf.d/default.conf:ro
- /nginx/dist:/var/www/html:ro
ports:
- "80:80"
depends_on:
- api-server
api-server:
build: /api-server
container_name: api-server
volumes:
- /api-server:/webapps
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
container_name: Postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
ports:
- "5432:5432"
Some Dockerfile content of api-server is as follows
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /webapps
WORKDIR /webapps
RUN apt-get clean && apt-get update && apt-get upgrade -y && apt-get install -y python3-pip libpq-dev apt-utils
COPY ./requirements.txt /webapps/
RUN pip3 install -r /webapps/requirements.txt
COPY . /webapps/
CMD ["bash","-c","./start.sh"]
start.sh is as follows
#!/usr/bin/env bash
cd server/
python manage.py runserver 0.0.0.0:8000
type docker-compose up result as follows
root#VM:/home/test/Documents/ComposeTest# docker-compose up
Creating network "composetest_default" with the default driver
Creating Postgres ... done
Creating api-server ... done
Creating dashboard ... done
Attaching to Postgres, api-server, dashboard
Postgres | The files belonging to this database system will be owned by user "postgres".
Postgres | This user must also own the server process.
...
...
api-server exited with code 0
api-server exited with code 0
docker logs api-server is empty
I would very appreciate it if you guys can tell me how to check this problems, It is better to provide a solution.
You are already copying api-server to Dockerfile during build time which should work fine, but in Docker compose it all override all the pip packages and code.
volumes:
- /api-server:/webapps
Remove the volume from your Docker compose and it should work.
Second thing set permission to the bash script.
COPY . /webapps/
RUN chmod +x ./start.sh
Third thing, you do need to run python using bash as there is no thing in the bash that CMD can not perform so why not as a CMD?
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Running Wagtail site with Docker

I'm trying to convert an existing Wagtail site to run with Docker. I have created the image and then run the container, but I'm unable to connect in the browser window. Getting 0.0.0.0 didn’t send any data. ERR_EMPTY_RESPONSE.
Dockerfile:
FROM python:3.6
RUN mkdir /app
WORKDIR /app
COPY ./ /app
RUN pip install --no-cache-dir -r /app/requirements/base.txt
RUN mkdir -p -m 700 /app/static
RUN mkdir -p -m 700 /app/media
ENV DJANGO_SETTINGS_MODULE=mysite.settings.dev DJANGO_DEBUG=on
ENV SECRET_KEY=notsosecretkey
ENV DATABASE_URL=postgres://none
ENV SENDGRID_KEY=sendgridkey
EXPOSE 8080
RUN chmod +x /app/entrypoint.sh \
&& chmod +x /app/start-app.sh
RUN python manage.py collectstatic --noinput
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["/app/start-app.sh"]
docker-compose.yml:
version: '2'
services:
db:
environment:
POSTGRES_DB: app_db
POSTGRES_USER: app_user
POSTGRES_PASSWORD: changeme
restart: always
image: postgres:9.6
expose:
- "5432"
ports:
- "5432:5432"
app:
container_name: mysite_dev
build:
context: .
dockerfile: Dockerfile
depends_on:
- db
links:
- db:db
volumes:
- .:/app
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://app_user:changeme#db/app_db
command: python manage.py runserver 0.0.0.0:8080
entrypoint.sh:
#!/bin/sh
set -e
exec "$#"
start-app.sh:
#!/bin/sh
python manage.py runserver 0.0.0.0:8080
dev.py settings file:
from .base import *
DEBUG = True
for template_engine in TEMPLATES:
template_engine['OPTIONS']['debug'] = True
ALLOWED_HOSTS = (
'0.0.0.0',
)
DATABASES = {
'default': db_cache_url.config('DATABASE_URL', extra={'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'mysite', 'HOST': 'localhost', 'PORT': '5432'}),
}
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
WSGI_APPLICATION = 'mysite.wsgi.application'
try:
from .local import *
except ImportError:
pass
I run docker run -p 8080:8080 app:latest and it works when I run docker ps showing that it's at 0.0.0.0:8080->8080/tcp, but when I go to 0.0.0.0:8080 in the browser window, I get the error. If I remove the DATABASES setting, I see Django errors loading, but then I'm getting settings.DATABASES is improperly configured. Please supply the NAME value. so I think it needs to be there. What am I missing?

Categories