2 Ways to start Docker Container automatically on Boot in Linux

Containerization is not a new technology anymore, millions of apps using it and in this, Docker has played quite an essential role in providing a platform for managing and deploying Conatiner-based applications. Developers can use Docker to create custom application images with all dependencies required in a single package. So, that users can easily install them in a single command without manually configuring each and every element.

However, after creating a container running with your application on Docker, what if your system got rebooted? In that situation, I am sure you would like Docker to start the application with a system boot that is serving hundreds of people automatically, right??

If so, then this tutorial revolves around that, here we learn how to configure your Linux system to start a Docker container on system boot using SystemD but automatically.

Before following this tutorial, I am assuming that you already have Docker installed on your Linux system.

1: Open the command Terminal

Yes, you need to access the command terminal of your Linux system to create a service file that will automatically restart the Docker container. So, if you have installed Docker on some remote server then use SSH and access the server whereas the GUI desktop user can directly start the Terminal app from the applications menu.

Note: you must have sudo rights.

2: Check Docker is installed

Ofcourse to automatically run the container, Docker must be installed on our system. To check Docker’s service is enabled and running, use the given command:

sudo systemctl status docker

If it is not enabled then use:

sudo systemctl enable docker

Whereas to start the service:

sudo systemctl start docker
Check Docker is installed

3. Restart Container using the Docker option

There are two ways to restart the Docker container automatically with the system boot. One is using Docker’s own command and the other with the help of systemd unit file, Here we discuss both ways.

#1st Way: Using Docker command – recommended one

While creating a container you can use –restart unless-stopped parameter which tells the Docker to always restart the container unless it is not stopped manually.

Note: Replace container-nam with your container.

docker run -d --restart unless-stopped container-name

Whereas, if you already have an active container then you can update its policy and make it restart automatically with the system boot.

docker update --restart unless-stopped container-name

Even you can update the policy of all existing containers to make them restart automatically by using the given command:

 docker update --restart unless-stopped $(docker ps -q)

#2nd way: Usingh SystemD Unit file

4: Create a Systemd unit file

Now, in your terminal switch to the directory where the system service files are saved, generally.

cd /etc/systemd/system

Next, create a new service file, here is the command to do that:

sudo nano mycontainer.service

You can change the name of the service with whatever you want, for example, here we are giving it mycontainer you can define some other to easily identify the service file.

As the file opens, paste the following code into it.

[Unit]
Description=My Docker Container
Requires=docker.service
After=docker.service

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

[Install]
WantedBy=multi-user.target

Replace “linuxshout” with the container name that you want to start and run automatically with the system boot.

Save the file by pressing Ctrl+X, type Y and then hit Enter key.

Well, if you don’t know the name of your Application container then run:

docker ps -a

At the end of the result, you will see the container name of the particular Docker Image you have used to create it.

Find Docker container name

5: Start & Enable Container Service

Now, let’s start and enable the service file which you have created for your container so that it can get started automatically with system boot.

Note: Replace “mycontainer” with your container service file name that you have created.

sudo systemctl enable mycontainer.service
sudo systemctl start mycontainer.service

Check status:

systemctl status mycontainer.service --no-pager -l
Start Enable Docker Container Service

6: Verify the container starts automatically

If you want to confirm whether your container starts automatically or not, then simply reboot your computer or system.

sudo reboot

After that, once you are again online, run the given command to see whether the container is active or not.

sudo docker ps

So, this was the quickest way to create a service file on a Linux system that will automatically start your Docker Container on system reboot. You can also customize the service to give specific instructions if required.

source : https://linux.how2shout.com/how-to-start-docker-container-automatically-on-boot-in-linux/

Posted on: October 18, 2023, by :  | 82 views
https://serang.ut.ac.id/css/css/slot88/ https://tinjut.bagkeu.dikdasmen.kemdikbud.go.id/slot-maxwin/ https://dpm.polinema.ac.id/slot-gacor/ https://akademik.ft.unm.ac.id/slot-dana/ https://ppdb.probolinggokab.go.id/slot-5000/ https://bkad.sulselprov.go.id/assets/ https://ojs.balidwipa.ac.id/docs/slot-gacor/ http://korpri.pekalongankab.go.id/api/slot-gacor/ https://elang.umpp.ac.id/foto/farmasi/-/asset/ http://rsud-kelet.jatengprov.go.id/wp-content/-/asset/ https://kusdhianto-fe.staff.ugm.ac.id/slot88/ http://ppdb.probolinggokab.go.id/judi-bola/ https://bapenda.labuhanbatukab.go.id/racikan-sbobet/ http://rsud-kelet.jatengprov.go.id/wp-content/-/data/ https://agenda.riau.go.id/-/judi-bola/ https://balapan.padang.go.id/sbobet88/ http://jdih.wakatobikab.go.id/sbobet88/ http://kph.menlhk.go.id/sbobet88/ https://bkad.sulselprov.go.id/data/ https://dpm.polinema.ac.id/slot-gacor/ https://dinkes.jemberkab.go.id/storage/attachments/