Toolset to run Docker containers as systemd service on RedHat and other Linux systems
Roman Pavlyuk [email protected]
Docker is a the most prominent technology invented in the last years which makes sane execution of various applications very easy. Docker's philosphy is based on the principle: one container -- one application. But what if you'd like to run your "dockerized" application as SystemD service with all the benefits like starting it on boot? SystemDock is what you actually need then.
- SystemDock is mostly written in Python and uses some 3rd party libraries. Let's install them first:
sudo pip3 install docker yaml
- Checkout the source code:
git clone https://github.com/rpavlyuk/systemdock.git ./
- RedHat family users, use RPM installer to install the tool:
sudo make install-rpm clean
- Others, install the tool using "old school" way:
sudo make install clean
- Check the confguration file
/etc/systemdock/config.yaml
if the settings are valid for your distribution
- The tool has built-in help manual so check it out first:
systemdock --help
- List all services that are currently managed by SystemDock:
systemdock -a list
- Create SystemD service from existing Docker container. For example, let's create the one for Tomcat:
sudo systemdock -v -a add --name tomcat -d "tomcat:9.0"
- Check out file
/etc/systemdock/containers.d/[NAME]/config.yml
(in our example:/etc/systemdock/containers.d/tomcat/config.yml
) and add additional options like port forwarding and file mounts if needed. Seeexamples
for more information on options. - You can check if the service is listed in those that are managed by SystemDock:
systemdock -a list
- Additionally, you can get detailed information about the specific service (profile):
systemdock -a list -n tomcat
- Now, start dockerized Tomcat as systemd service. Note, that services are being created as "systemdock-[NAME]". In our example it will be
systemdock-tomcat
:
sudo systemctl start systemdock-tomcat
NOTE: As you know, Docker is pulling the image from repository when the container is run for the first time. So, be patient when launching the service for the first time.
- Check if the service is running:
sudo systemctl status systemdock-tomcat
- Enable the service to start on boot:
sudo systemctl enable systemdock-tomcat
- If required to stop the service:
sudo systemctl stop systemdock-tomcat
- To remove the service from SystemD:
sudo systemdock -v -a remove --name tomcat
- Test on other distros: Ubuntu, SUSE, etc
- Add more container options to the config file