run telegram bot inside a docker container with django app - django

I have a Django application and using python manage.py name_bot_job, I start the bot, but when I add this task to the docker-compose.yml file and make build, my bot just refuses to start, how can I do it with best practices?
entrypoint: ["/bin/sh","-c"]
command:
- |
python manage.py collectstatic --no-input --clear
python manage.py migrate --noinput
python manage.py createsuperuserwithpassword --username $${DJANGO_SUPERUSER_USERNAME} --password $${DJANGO_SUPERUSER_PASSWORD} --email $${DJANGO_SUPERUSER_EMAIL} --preserve
gunicorn job_filter_app.wsgi:application --bind 0.0.0.0:8000
python manage.py poolbot

Related

run initial commands in a docker-compose service

I followed this tutorial to run my django web-app locally, apart for the web-app the only service is a postgres db.
I wrote a simple script entrypoint.sh to automate the initial operations needed by a django app, like migrate, makemigrations, collectstatic, createsuperuser;
Everything works fine, except that entrypoint.sh runs everytime I use docker-compose up, performing initial operations that should only run once.
How can I set up my Dockerfile or docker-compose.yml so that entrypoint.sh is run just the first time and not everytime I docker-compose down and then docker-compose up again?
Dockerfile
# importing base image
FROM python:3.9
# updating docker host or host machine
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
# changing current working directory to /usr/src/app
WORKDIR /usr/src/app
# copying requirement.txt file to present working directory
COPY requirements.txt ./
# installing dependency in container
RUN pip install -r requirements.txt
# copying all the files to present working directory
COPY . .
# informing Docker that the container listens on the
# specified network ports at runtime i.e 8000.
EXPOSE 8000
ENTRYPOINT ["./entrypoint.sh"]
docker-compose.yml
version: '3.7'
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
app:
build: ./
command: gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000
volumes:
- ./data/:/usr/src/app/data/
- ./media/:/usr/src/app/media/
ports:
- 8000:8000
- 5432:5432
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- DJANGO_SUPERUSER_EMAIL=admin#email.it
- DJANGO_SUPERUSER_USERNAME=admin#email.it
- DJANGO_SUPERUSER_PASSWORD=passadmin
depends_on:
- db
entrypoint.sh
#!/bin/bash
python3 manage.py migrate;
python3 manage.py makemigrations;
python3 manage.py migrate;
python3 manage.py collectstatic --clear;
python3 manage.py createsuperuser --no-input;
gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;
RECAP
In the directory where my Dockerfile and docker-compose.yml file are:
sudo docker-compose build app
sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created
sudo docker-compose down
sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created AGAIN. I'm trying to avoid this.
I'm new to docker-compose and any help is really appreciated thanks.
A dirty but simple way would be to ignore the error of the createsuperuser command by changing it to python3 manage.py createsuperuser --no-input || true;.
This might even be the solution you prefer, because if the variables for docker-compose change, a new superuser would be created with the changed values.
#!/bin/bash
python3 manage.py migrate;
python3 manage.py makemigrations;
python3 manage.py migrate;
python3 manage.py collectstatic --clear;
python3 manage.py createsuperuser --no-input || true;
gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;

Docker container not running on Linux

In our project we use docker to keep our development environments similar. When running it on macOS it works, however on Linux (currently using Manjaro) it states that the container doesn't exist:
docker-compose -f docker-compose.yml exec web python manage.py migrate --noinput
Error response from daemon: Container 43f405eac00706b22dc075cbcaf33dbbf595971be3ce1955034302cb1284aa5b is not running
make: *** [Makefile:9: build] Error 1
This is the makefile for our project:
build:
make down
docker-compose -f docker-compose.yml up -d --build
docker-compose -f docker-compose.yml exec web python manage.py migrate --noinput
docker-compose -f docker-compose.yml exec web python manage.py createsuperuser --noinput --email name#testmail.org
docker-compose -f docker-compose.yml exec web python manage.py startup
We tried different systems and different commands, but it always fails when trying to execute the second command.

Cannot run Django management command using Docker exec

I've got a Django server running nicely in a Docker container called loc-vol-web.
When I try to run Django management commands straight from the host CLI, it just doesn't work:
>> docker exec -it loc-vol-web "python /app/src/manage.py migrate"
OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"python /app/src/manage.py migrate\": stat python /app/src/manage.py migrate: no such file or directory": unknown
However, all of the following work fine:
>> docker exec -it loc-vol-web "python"
Python 3.7.6 (default, Jan 3 2020, 23:35:31)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>> docker exec -it loc-vol-web "/bin/bash"
some_user#ce1b1c2ac208:/app$ python /app/src/manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
some_user#ce1b1c2ac208:/app$
I am not sure why I can't just run manage.py.
The problem is the way you run docker:
docker exec -it loc-vol-web "python /app/src/manage.py migrate"
It is taking everything in double quotes as the command.
It should be run without double quotes:
docker exec -it loc-vol-web python /app/src/manage.py migrate

Execute migrations in production but they are not created in my server

I try to create the migrations with the command but not copy the files
sudo docker-compose -f production.yml run --rm django python manage.py makemigrations

deploying mezzanine 3.0 on heroku

does anyone have the proper guide to deploy mezzanine on heroku? I couldn't get gunicorn to work.
My procfile is
web: python manage.py collectstatic --noinput; python manage.py run_gunicorn -b 0.0.0.0:$PORT
I saw there's fabfile.py which assist with gunicorn deployment. Could i utilize it for heroku? And how?