Implementation code of Dockerfile when two processes are to be started in docker container

  • 2021-09-20 21:56:35
  • OfStack

docker, which wants to do an cron timing task in the near future, is defined 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"]

Load the timer task run. cron with crontab in start. sh, then start crond:

/usr/bin/crontab /run.cron

/usr/sbin/crond

After docker build Dockerfile, the container was run by docker run name xxx-d, and it was found that the container exited after start. sh was executed, and the timing task could not be started at all. Various methods on the Internet said that nohup was used, there was an infinite loop, and there was a signal, which was found to be unreliable.

The mechanism of docker under 1 is analyzed. One docker container can only manage one process at the same time. After this process exits, the container exits. This does not mean that only one process can run in one container (which is too wasteful), but the last running process cannot exit.

In this case, start. sh is run at the start of the container. The default setting of crond is background run, which causes start. sh to finish running and the container exits with start. sh exiting.

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

This way start. sh does not exit, and the docker run-d runtime keeps the container running in the background.

start. sh Summary:

(1) When multiple daemons are running in a container, the preceding process is running in the background (or adding & ), otherwise the following services cannot be started

(2) The last daemon 1 in the container must run in foreground mode, otherwise start. sh exits, the container exits, and all services start for nothing


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

Where./start. sh script is as follows


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

The above is one lesson of docker mirror startup script.

Supplementary knowledge: processing when running multiple processes in Docker

Generally, the Docker container is suitable for running a single process, but many times we need to run multiple processes in the Docker container. There are two different ways to run the multi-process container: using the shell script or supervisor, both of which are simple and have their own merits and demerits, except for one notable detail. Here, we only talk about the script processing method.

Write a script multiple_thread. sh. The script function runs two python programs and saves the running results to log files. 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
#  Every interval 60 Seconds to check whether the process is running 
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

The next step is to make 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

Related articles: