Requirement: Celery is introduced in Django. When starting the Django project, how to start the Celery service as well? Start using the ENTRYPOINT command 1. Write the Dockerfile 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/ You don't need to read the above, the key is to look at the following command #Start a service with CMD# CMD ["python3.5", "/opt/hrms/hrms/manage.py", "runserver", "0.0.0.0:8080"] #When starting multiple services, you can use CMD to execute a script and start multiple services in the script CMD source /opt/hrms/run/entrypoint.sh #When starting multiple services, you can also use ENTRYPOINT to execute a script and start multiple services in the script ENTRYPOINT ["/opt/hrms/run/entrypoint.sh"] The difference between CMD and ENTRYPOINT is that the CMD command can be overwritten by the command command in the docker-compose.yml file. Once the command is specified, the CMD command will no longer be executed, while ENTRYPOINT can never be overwritten. So here we can do this: Use CMD to start a script, 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.py migrate in the command in the docker-compose.yml file, so that the CMD command will not be executed and only the database migration will be executed. 2. entrypoint.sh script file #!/bin/bash #Start Django python3.5 /opt/hrms/hrms/manage.py runserver 0.0.0.0:8080 & #Start the worker celery worker -A celery_tasks.main -l info -f /opt/hrms/logs/celery.log & #Note that the log location must be written in an absolute path#Start beat celery beat -A celery_tasks.main -l info Note: The first two services must be run in the background, that is, add an & after them, and the last service must be run in the foreground. Otherwise, if all are run in the foreground, only the first service will start; if all are run in the background, the container will exit when the last service is executed. Additional knowledge: Use of Dockerfile CMD Three formats of CMD:
Note: The exec form above will be parsed into a JSON Array, which means you must use double quotes instead of single quotes. The exec form does not call the command shell. For example, in CMD [ "echo", "HOME"], the HOME variable will not be replaced. If you want to use the shell, it should be like this: CMD [ "sh", "-c", "echo $HOME" ] There should be only one CMD in a Dockerfile. If there are multiple, only the last one will be executed. Examples of format usage:
or
The above article about how to use CMD or ENTRYPOINT command to start multiple services at the same time in docker is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: The principles and defects of MySQL full-text indexing
>>: Notes on using the blockquote tag
Table of contents Preface Active withdrawal Excep...
Docker installation (Alibaba Cloud Server) Docker...
Without further ado, I will post the code for you...
1. Overview MySQL version: 5.6.21 Download addres...
We usually have a scanning box when we open the c...
Table of contents Understanding Prototypes Unders...
1. Install MySQL software Download and install My...
Table of contents Various ways to merge objects (...
Table of contents Preface Global parameter persis...
1. Cause The official cerbot is too annoying. It ...
MySQL 8.0 for Windows v8.0.11 official free versi...
This article shares the specific code for impleme...
Doccer Introduction: Docker is a container-relate...
Preface Recently, I have been busy dealing with s...
Table of contents Preface Create a bridge network...