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

Let's talk in detail about how the NodeJS process exits

Table of contents Preface Active withdrawal Excep...

How to migrate sqlite to mysql script

Without further ado, I will post the code for you...

Detailed steps for installing and configuring mysql 5.6.21

1. Overview MySQL version: 5.6.21 Download addres...

Example code for implementing a QR code scanning box with CSS

We usually have a scanning box when we open the c...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

Detailed explanation of global parameter persistence in MySQL 8 new features

Table of contents Preface Global parameter persis...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

Introduction to new features of MySQL 8.0.11

MySQL 8.0 for Windows v8.0.11 official free versi...

JavaScript to implement dynamic digital clock

This article shares the specific code for impleme...

Docker Getting Started Installation Tutorial (Beginner Edition)

Doccer Introduction: Docker is a container-relate...

Summary of some common writing methods that cause MySQL index failure

Preface Recently, I have been busy dealing with s...

Steps to create a CentOS container through Docker

Table of contents Preface Create a bridge network...