N
N
No_name4512021-04-05 21:29:58
Django
No_name451, 2021-04-05 21:29:58

How to use multiple blocking commands in docker-compose?

Good afternoon.

I'm getting closer to docker-compose, but the question arose of how to execute a few commands to run a project on django and connect gunicorn.

The project uses: django, rqworker(redis), rqscheduler(redis), postgres.

On the local machine, I launch the project using 3 terminals (because each of them is blocking):

1. terminal:
<env>[email protected]: python manage.py rqworker
2. terminal:
<env>[email protected]: python manage.py rqscheduler
3. terminal:
<env>[email protected]: python manage.py runserver

Question 1:

What should docker-compose look like to run these 3 commands (if you know how to connect redis and postgres, please help with this too)?

Question 2:
There is a desire to connect gunicorn, but how to do it?


I would be very grateful if you could tell me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2021-04-06
@No_name451

We need to write a script that will play the role of ENTRYPOINT. Such a script can have very rich functionality, but it will be enough to solve the problem:

docker-entrypoint.sh
#!/bin/sh
set -e

if [ "x$DJANGO_MANAGEPY_MIGRATE" = 'xenable' ]; then
    python manage.py migrate --noinput
fi

if [ "x$DJANGO_MANAGEPY_COLLECTSTATIC" = 'xenable' ]; then
    python manage.py collectstatic --noinput
fi

if [ "x$DJANGO_MANAGEPY_LOADDATA" = 'xenable' ]; then
    python manage.py loaddata fixtures/initial_data.json
fi

if [ "x$DJANGO_MANAGEPY_PARALLELRUN" = 'xenable' ]; then
    python manage.py rqworker &
    python manage.py rqscheduler &
    python manage.py runserver &
fi

exec "[email protected]"

Write a Dockerfile on the basis of which the container will be launched and add ENTRYPOINT ["./docker-entrypoint.sh"]
to it. Among other things, instructions for installing gunicorn and other dependencies should be added to the Dockerfile.
You can run it all from
docker-compose.yml
version: "3.9"
   
services:
  db:
    container_name: mysql
    image: mysql:8.0.22
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - /data:/var/lib/mysql
    restart: always
    networks:
      - net

  django:
    container_name: django
    build:
        context: .
        args:
          USER_ID: ${USER_ID}
          GROUP_ID: ${GROUP_ID}
          USER_NAME: ${USER_NAME}
          GROUP_NAME: ${GROUP_NAME}
          DOMAIN_NAME: ${DOMAIN_NAME}
          RUN_DEPS: ${RUN_DEPS}
          BUILD_DEPS: ${BUILD_DEPS} 
    environment:
      DJANGO_MANAGEPY_MIGRATE: ${DJANGO_MANAGEPY_MIGRATE}
      DJANGO_MANAGEPY_COLLECTSTATIC: ${DJANGO_MANAGEPY_COLLECTSTATIC}
      DJANGO_MANAGEPY_COLLECTSTATIC: ${DJANGO_MANAGEPY_COLLECTSTATIC}
      DJANGO_MANAGEPY_LOADDATA: ${DJANGO_MANAGEPY_LOADDATA}
      DJANGO_MANAGEPY_PARALLELRUN: ${DJANGO_MANAGEPY_PARALLELRUN}
    command: gunicorn ${WSGI}.wsgi:application --user ${USER_NAME} --group ${GROUP_NAME} --name ${APP_NAME} --workers ${WORKERS} --max-requests ${MAX_REQ} --timeout ${TIMEOUT} --preload --bind=unix:/var/www/${USER_NAME}/data/tmp/${APP_NAME}.sock
    volumes:
      - /blabla:/blabla
    depends_on:
      - db
    restart: always
    networks:
      - net

networks:
  net:
    name: net

In command , enter the gunicorn launch command, first the docker-entrypoint.sh script will run , then gunicorn will start.
Other services are added to docker-compose.yml in the same way as I described adding MySQL.

D
Dmitry, 2021-04-05
@q2digger

docker-compose doesn't know commands
, it's a docker container launcher.
your task is to learn how to pack your applications into a container (in this case, three different containers), and then write a script for launching these three containers + postgres + radish and what else is needed ..
here is an excellent manual on the docker off site how to wrap a python application to container
https://docs.docker.com/language/python/build-images/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question