docker uses the CMD or ENTRYPOINT command to start multiple services at the same time

  • 2021-09-16 08:32:31
  • OfStack

Requirements: celery is introduced in django. How to start celery service when starting django project

Start with ENTRYPOINT command

1. Write an Dockerfile file


 FROM centos:7
 RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
 ENV LC_ALL zh_CN.UTF-8
 COPY ./hrms $CODE_DIR/hrms/
 COPY ./run $CODE_DIR/run/
 RUN chmod a+x $CODE_DIR/run/*
 RUN pip3 install -r $CODE_DIR/hrms/requirements.txt
 EXPOSE 8080
 WORKDIR /opt/hrms/hrms/

Don't look at the above, the key is to look at the following command


 # Start 1 For individual services CMD You can 
 # CMD ["python3.5", "/opt/hrms/hrms/manage.py", "runserver", "0.0.0.0:8080"] 
 
 # When starting multiple services, you can use the CMD  Execute 1 Scripts in which multiple services are started 
 CMD source /opt/hrms/run/entrypoint.sh
 
 # When starting multiple services, you can also use the ENTRYPOINT Execute 1 Scripts in which multiple services are started 
 ENTRYPOINT ["/opt/hrms/run/entrypoint.sh"] 

The difference between CMD and ENTRYPOINT is that CMD command can be overwritten by command command in docker-compose. yml file. Once command is specified, CMD command will no longer be executed, while ENTRYPOINT can never be overwritten.

So here we can do this:

Start a script with CMD, and then start multiple services in the script, such as django, celery, etc. When you only want to do database migration, you can execute python manage. ES40migrate in docker file docker-compose.

2. entrypoint. sh script file


#!/bin/bash
 # Start django
 python3.5 /opt/hrms/hrms/manage.py runserver 0.0.0.0:8080 & 
 
 # Start worker
 celery worker -A celery_tasks.main -l info -f /opt/hrms/logs/celery.log & # Note here that the log location should write an absolute path 
 
 # Start beat
 celery beat -A celery_tasks.main -l info

Note: The first two services 1 must run in the background, that is, add a & The last 1 service is to be run from the front desk.

Otherwise, if all of them run from the previous station, only the first service will start; If all of them are run backstage, the container will exit when the last service execution is completed.

Supplementary knowledge: Use of Dockerfile CMD

Three formats of CMD:

CMD ["executable", "param1", "param2"] (exec form, preferred format)

CMD ["param1", "param2"] (as default parameter for ENTRYPOINT)

CMD command param1 param2 (shell form)

Precautions:

The above exec form will be parsed into an JSON Array, which means that you must use double quotation marks instead of single quotation marks.

exec form does not invoke the command-line interpreter (command shell).

For example, HOME is not substituted in CMD ["echo", "HOME"]. If shell is used, it should be: CMD ["sh", "-c", "echo $HOME"]

There should be only one CMD in one Dockfile, and if there are more than one, only the last one will be executed

Examples of format use:

CMD ["sh", "run. sh"]

Or

CMD sh run.sh


Related articles: