Starting two application after container is started

Hello guys,
would you be able to tell me what I’m doing wrong in this approach?
I’m trying to use script that should take care of starting two daemons (OpenVPN and statping).

Dockerfile:

FROM statping/statping:latest
 RUN apk add --no-cache openvpn openrc \
 RUN mkdir -p /dev/net \
 && mknod /dev/net/tun c 10 200 \
 && chmod 666 /dev/net/tun \
 && rm -rf /var/cache/apk/*

 COPY monitoring.ovpn /etc/openvpn/
 COPY exec.sh /root/
 RUN chmod 755 /root/exec.sh

 ENTRYPOINT ["/root/exec.sh"]

Content of exec.sh file (it should start OpenVPN and Statping daemons):

#!/bin/sh -x
set -m
/usr/sbin/openvpn /etc/openvpn/monitoring.ovpn &
/usr/local/bin/statping

Then building the new image:

# docker build --no-cache --tag statping_vpn .
Sending build context to Docker daemon   7.41MB
Step 1/7 : FROM statping/statping:latest
 ---> fe159e7795ae
Step 2/7 : RUN apk add --no-cache openvpn openrc
 ---> Running in 28ae31766c51
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/13) Installing openrc (0.42.1-r12)
Executing openrc-0.42.1-r12.post-install
(2/13) Installing libbz2 (1.0.8-r1)
(3/13) Installing fts (1.2.7-r1)
(4/13) Installing xz-libs (5.2.5-r0)
(5/13) Installing libelf (0.179-r0)
(6/13) Installing libmnl (1.0.4-r0)
(7/13) Installing libnftnl-libs (1.1.6-r0)
(8/13) Installing iptables (1.8.4-r2)
(9/13) Installing iptables-openrc (1.8.4-r2)
(10/13) Installing iproute2 (5.6.0-r0)
Executing iproute2-5.6.0-r0.post-install
(11/13) Installing lzo (2.10-r2)
(12/13) Installing openvpn (2.4.11-r0)
Executing openvpn-2.4.11-r0.pre-install
(13/13) Installing openvpn-openrc (2.4.11-r0)
Executing busybox-1.31.1-r19.trigger
OK: 18 MiB in 35 packages
Removing intermediate container 28ae31766c51
 ---> 8554d92a7580
Step 3/7 : RUN mkdir -p /dev/net  && mknod /dev/net/tun c 10 200  && chmod 666 /dev/net/tun  && rm -rf /var/cache/apk/*
 ---> Running in 6a1f54ccdec4
Removing intermediate container 6a1f54ccdec4
 ---> 9b60601cd117
Step 4/7 : COPY monitoring.ovpn /etc/openvpn/
 ---> fc4c026a0951
Step 5/7 : COPY exec.sh /root/
 ---> 6d2210624a82
Step 6/7 : RUN chmod 755 /root/exec.sh
 ---> Running in ce2a455524f5
Removing intermediate container ce2a455524f5
 ---> 0f0bad1d8f90
Step 7/7 : ENTRYPOINT ["/root/exec.sh"]
 ---> Running in 9eca26d51084
Removing intermediate container 9eca26d51084
 ---> f14ac0995f73
Successfully built f14ac0995f73
Successfully tagged statping_vpn:latest

When I try to run image:

# docker run -d --cap-add=NET_ADMIN --device=/dev/net/tun statping_vpn
67f84b2fcde05475b4dcfd14349b839e2ea775aa6044187a665d844f1e871f30
# docker ps -a | grep statping_vpn
67f84b2fcde0        statping_vpn                "/root/exec.sh"          2 minutes ago       Exited (0) 2 minutes ago                                gifted_panini

Is there something wrong with this approach how to start multiple applications?

Many thanks,
Stan

Good morning Stan,

it is possible to run two commands within a container - and it should work the way you have tried.
But there is a caveat. A container ends/terminates as soon as the command itself ends (or the /root/exec.sh is not executable at all). So if your /usr/local/bin/statping ends the whole container ends/terminates even if there is a process running in the background.
The other way round it might also be a problem: if the background-process (/usr/sbin/openvpn in this case) ends the container is still running and Docker doesn’t get any clue that there might be a problem (and that the container needs to be restarted). To avoid this second problem you could either add some healthcheck (see Compose file version 3 reference | Docker Documentation) or put the two commands into separate containers.

Maybe docker logs <containername|containerid> prints some helpful information why the container has ended?