Dockerfile implementation code when starting two processes in a docker container

Dockerfile implementation code when starting two processes in a docker container

I want to make a docker for cron scheduled tasks recently, and define it as follows in Dockerfile

FROM library/alpine:latest
RUN apk --update add rsync openssh bash
VOLUME ["/data"]
ADD start.sh /
CMD ["/bin/bash","/start.sh"]

Use crontab to load the scheduled task run.cron in start.sh, and then start crond:

/usr/bin/crontab /run.cron

/usr/sbin/crond

After docker build Dockerfile, I used docker run –name xxx -d to run the container. I found that the container exited after start.sh was executed, and I could not start the scheduled task at all. I tried various methods online, including using nohup, an infinite loop, and using signals, but I found that none of them were reliable.

After analyzing the docker mechanism, a docker container can only manage one process at a time. When the process exits, the container also exits. This does not mean that only one process can run in a container at a time (that would be a waste), but the last running process cannot exit.

In this case, start.sh is run when the container is started. The default setting of crond is to run in the background, which causes start.sh to finish running and the container to exit when start.sh exits.

Therefore, in start.sh, crond should be forced to run in the foreground: crond -f.

This way start.sh will not exit, and docker run -d can keep the container running in the background.

Summary of start.sh:

(1) When running multiple daemon processes in a container, the first process must be run in the background (or add &), otherwise the subsequent services cannot be started

(2) The last daemon process in the container must be run in foreground mode, otherwise start.sh exits, the container exits, and all services are started in vain.

FROM ubuntu:latest

RUN mkdir -p "/usr/src/pdas" \
  mkdir -p "/usr/src/pdas/reload"

COPY bin.tar /usr/src/pdas
COPY config.tar /usr/src/pdas
COPY lib.tar /usr/src/pdas

WORKDIR /usr/src/pdas
RUN tar -xvf lib.tar && \
  tar -xvf bin.tar && \
  tar -xvf config.tar

ENV LD_LIBRARY_PATH /usr/src/pdas/lib/libxml/lib:/usr/src/pdas/lib/curl/lib:$LD_LIBRARY_PATH

WORKDIR /usr/src/pdas/bin
RUN chmod +x start.sh && \
  chmod +x f_recv && \
  chmod +x f_send

VOLUME /behb/diqu
VOLUME /var/log/pdas

ENTRYPOINT ./start.sh

The ./start.sh script is as follows

#!/bin/bash
./f_recv &
./f_send

The above are some thoughts on the Docker image startup script.

Additional knowledge: How to handle multiple processes in Docker

Usually, Docker containers are suitable for running a single process, but many times we need to run multiple processes in a Docker container. There are two different ways to run multi-process containers: using a shell script or using supervisor. Both methods are simple and have their own advantages and disadvantages, but there are some details worth noting. Here we only talk about the processing method using scripts.

Write a script multiple_thread.sh. The script runs two Python programs and saves the results to a log file. The script content is as follows

#!/bin/bash
# Start the first process
nohup python -u /tmp/thread1.py > /tmp/thread1.log 2>&1 &
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
echo "thread1 status..."
echo $PROCESS_1_STATUS
if [ $PROCESS_1_STATUS -ne 0 ]; then
echo "Failed to start my_first_process: $PROCESS_2_STATUS"
exit $PROCESS_1_STATUS
fi
sleep 5
# Start the second process
nohup python -u /tmp/thread2.py > /tmp/thread2.log 2>&1 &
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
echo "thread2 status..."
echo $PROCESS_2_STATUS
if [ $PROCESS_2_STATUS -ne 0 ]; then
echo "Failed to start my_second_process: $PROCESS_2_STATUS"
exit $PROCESS_2_STATUS
fi
# Check if the process is running every 60 seconds while sleep 60; do
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi

Next, create the Dockerfile:

FROM centos:latest
 
COPY thread1.py /tmp/thread1.py
COPY thread2.py /tmp/thread2.py
COPY multiple_thread.sh /tmp/multiple_thread.sh
 
CMD bash /tmp/multiple_thread.sh

The above Dockerfile implementation code for starting two processes in a Docker container is all I have to share with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker uses Dockerfile to create a container image that supports automatic startup of ssh service
  • Steps to deploy multiple tomcat services using DockerFile on Docker container
  • Running a Java Web project built with MyEclipse in a Dockerfile container in Docker
  • How to create a simple container using Dockerfile

<<:  innerHTML Application

>>:  Detailed introduction to JS basic concepts

Recommend

Detailed steps for developing WeChat mini-programs using Typescript

We don't need to elaborate too much on the ad...

About the "occupational disease" of designers

I always feel that designers are the most sensiti...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

6 ways to view the port numbers occupied by Linux processes

For Linux system administrators, it is crucial to...

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

Solution to BT Baota Panel php7.3 and php7.4 not supporting ZipArchive

The solution to the problem that the PHP7.3 versi...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

Solution to MySQL 8.0 cannot start 3534

MySQL 8.0 service cannot be started Recently enco...

A screenshot demo based on canvas in html

Written at the beginning I remember seeing a shar...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...

SSH port forwarding to achieve intranet penetration

The machines in our LAN can access the external n...

Steps to build a Docker image using Dockerfile

Dockerfile is a text file that contains instructi...

How to build pptpd service in Alibaba Cloud Ubuntu 16.04

1. To build a PPTP VPN, you need to open port 172...

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...

Analysis of the cause of docker error Exited (1) 4 minutes ago

Docker error 1. Check the cause docker logs nexus...