I know this isn’t an actual docker way. But, I need to convert a custom distro into a container. So, far I was able to run the container and bash into the container and execute a script. As long as I left the console container terminates. Here is the process for the container creation from a Raspbx .img file.
After mounting the image:
mkdir rootfs raspbxfdisk -l -u=sectors /home/pi/raspbx-10-10-2020.img
mount -o loop,offset=272630784 /home/pi/raspbx-10-10-2020.img /rootfs
Then,
cp -a /rootfs/. /raspbx/
nano /raspbx/run/startup.sh
chmod +x /run/*
tar -C raspbx -c . | docker import - raspbx
After this I was able to run the container using:
docker run --name=raspbx -it --privileged --restart unless-stopped raspbx bash
cd /run
./startup.sh
At this point, script runs fine and I can access the webui. But, the container stops if I exit the shell.So, I tried
So, I tried
docker run --name=raspbx --net=macvlan_network --ip=192.168.188.27 -itd --privileged --restart no --entrypoint=/bin/bash raspbx
This time the script executes , but as soon as the container finishes executing the script it stops working. Same result with Dockerfile:
FROM raspbx
ENTRYPOINT ["sh", "/run/startup.sh"]
EXPOSE 80 3306 5060 5061 5160 5161 4569 10000-20000/udp
And
FROM raspbx
ENTRYPOINT ["bash", "/run/startup.sh"]
EXPOSE 80 3306 5060 5061 5160 5161 4569 10000-20000/udp
The script is:
#!/bin/bash -x
#https://docs.docker.com/engine/admin/multi-service_container/
/etc/init.d/mysql start
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start mysql: $status"
exit $status
fi
fwconsole start
if [ $status -ne 0 ]; then
echo "Failed to start fwconsole: $status"
exit $status
fi
#restore backup if exists
if [ -f /backup/new.tgz ]; then
echo "Restoring backup from /backup/new.tgz"
php /var/www/html/admin/modules/backup/bin/restore.php --items=all --restore=/backup/new.tgz
echo "Done"
fi
#restart freepbx to load everything fine after restoring backup
fwconsole stop
if [ $status -ne 0 ]; then
echo "Failed to stop fwconsole: $status"
exit $status
fi
fwconsole start
if [ $status -ne 0 ]; then
echo "Failed to start fwconsole: $status"
exit $status
fi
/etc/init.d/apache2 start
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start apache2: $status"
exit $status
fi
/run/backup.sh &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start backup.sh: $status"
exit $status
fi
/run/delete-old-recordings.sh &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start delete-old-recordings: $status"
exit $status
fi
while /bin/true; do
ps aux |grep mysqld |grep -q -v grep
MYSQL_STATUS=$?
ps aux |grep asterisk |grep -q -v grep
ASTERISK_STATUS=$?
ps aux |grep '/run/backup.sh' |grep -q -v grep
BACKUPSCRIPT_STATUS=$?
echo "Checking running processes..."
if [ $MYSQL_STATUS -ne 0 -o $ASTERISK_STATUS -ne 0 -o $BACKUPSCRIPT_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit -1
fi
echo "OK"
sleep 60
done
The only way I was able to keep it alive more then 24hrs is :
docker run --name=raspbx --net=macvlan_network --ip=192.168.188.27 -itd --privileged --restart no --entrypoint=/bin/bash raspbx -c 'sleep inf'
So, how do I keep the container keep alive, any help will be highly appreciated.