Ubuntu

Run Docker container as a service in Ubuntu

As we discuss deploying the Docker container as a service in Ubuntu, you may automatically start, stop, check the status, and otherwise manage a container as a systemd service by putting up a system-driven unit file on your host machine. The reasons for running systemd inside a machine are:

Many users would want to remove current multi-service applications from VMs and run them into containers for many services. They want these apps to be broken up into microservices; however, some folks cannot or do not have time. It makes sense to operate them as services run from systemd unit files.

System unit files: The majority of programs running within containers are made of VM or host code. These apps have a unit file prepared for the application and comprehend the execution of the program. It is better than hacking your own init services to launch the service using the supported way.

What is Docker?

Docker aims to help engineers and teams become more productive and less susceptible to mistakes. The establishment and implementation of new projects with Docker are easier and more time-efficient.

Consider a scenario in which your Windows operating system is installed, and your application should be deployed and tested in a separate operating system – Fedora, CentOS, and Ubuntu. What are you going to do? Do you want to put all this on your laptop? Is it extremely straightforward?

Consider a case where you have to use several Python versions of the application, say Python 3.2, Python 3.8, etc., and various webserver combinations such as NGINX and Apache. What are you going to do? It’s where Docker is going to save you.

Docker is a containerization platform that lets you bundle your program, and it is a Docker container that ensures that it functions smoothly in each environment.

So let’s say you use Ubuntu 20.02 and Python 3.2 to deploy/build your application. You may construct a docker image to execute code in a Docker container containing a multi-layer file.
A docker image is available for the correct running of your Project, with system libraries, tools, files, and any other dependencies.

Run Docker Container As Service

Let’s say your application’s name is ‘X.’ To run the application’s container as a service using systemd, create the following service file in /etc/systemd/system directory with names as
‘docker.some_name.service’ :

[Unit]
Description=X container
After=docker.service
Wants=network-online.target docker.socket
Requires=docker.socket

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a some_name
ExecStop=/usr/bin/docker stop -t 10 some_name

[Install]
WantedBy=multi-user.target

The file unit generates a new service and maps the start and stop docking instructions to the start and stop sequences of the service.

To enable service with system boot, use:

systemctl enable docker.some_name

Use the following commands to stop and start the service manually at any time:

sudo service docker.some_name stop
sudo service docker.some_name start

Conclusion

We showed an approach to deploy Docker containers as systemd services on Ubuntu in this article. It is a simple alternative to other tools for container orchestration. We can use Docker in various tasks related to the development and testing of our application. It enhances the testing phase of our application. We can use it to manage versions for the whole operating system of your application. Another use of Docker is to collaborate with a team for the operating system of your app. You can also execute your laptop code in the same environment as your server using Docker.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.