Categories: 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.

Sponsored

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’ :

Sponsored
[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.

Ubuntu Server Admin

Recent Posts

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

2 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

2 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

3 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

4 days ago

Bringing automation to open source 5G software at Ubuntu Summit 2024

In today’s massive private mobile network (PMN) market, one of the most common approaches to…

5 days ago

Ubuntu Weekly Newsletter Issue 865

Welcome to the Ubuntu Weekly Newsletter, Issue 865 for the week of November 3 –…

7 days ago