Docker uses CMD or ENTRYPOINT commands to start multiple services at the same time

Docker uses CMD or ENTRYPOINT commands to start multiple services at the same time

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:

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

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

CMD command param1 param2 (shell form)

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:

CMD [“sh”,”run.sh”]

or

CMD sh run.sh

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:
  • Detailed explanation of Dockerfile to create a custom Docker image and comparison of CMD and ENTRYPOINT instructions
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • Steps to deploy multiple tomcat services using DockerFile on Docker container
  • Installing the ping tool in a container built by Docker
  • Solve the problem of docker log mounting
  • Solve the problem that changes to the Docker MySQL container database do not take effect
  • The docker container directly runs to obtain the public IP operation through ping
  • Docker file storage path, get container startup command operation

<<:  The principles and defects of MySQL full-text indexing

>>:  Notes on using the blockquote tag

Recommend

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

VMware ESXi installation and use record (with download)

Table of contents 1. Install ESXi 2. Set up ESXi ...

JavaScript immediate execution function usage analysis

We know that in general, a function must be calle...

Solve the problem of MySql client exiting in seconds (my.ini not found)

Problem description (environment: windows7, MySql...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

Detailed explanation of Javascript Echarts air quality map effect

We need to first combine the air quality data wit...

How to use Docker to build a development environment (Windows and Mac)

Table of contents 1. Benefits of using Docker 2. ...

Method of dynamically loading geojson based on Vue+Openlayer

Load one or more features <template> <di...

JavaScript style object and CurrentStyle object case study

1. Style object The style object represents a sin...

How to allow all hosts to access mysql

1. Change the Host field value of a record in the...

Do you know how to use the flash wmode attribute in web pages?

When doing web development, you may encounter the...

Web page image optimization tools and usage tips sharing

As a basic element of a web page, images are one ...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...