Docker

Restart Docker Container

Hello. In this tutorial, we will talk about Docker and container restart in docker.

1. What is Docker?

In the present world, Docker is an important term –

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 What is Docker used for?

It is used for –

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.2 Basic Docker terminology?

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates, and runs the Docker containers
  • Hub: A public developer’s registry to distribute their code
  • Repository: A collection of Docker-related images i.e. different versions of the same application

1.3 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

2. Restart the Docker container

Docker has a straightforward syntax for restarting one or more containers. A docker container is restarted because of the following reasons –

  • The main process gets terminated after successful tasks execution
  • The main process gets terminated abruptly when a container is stopped forcefully
  • When the docker container is stopped
  • When the docker is restarted
  • When the docker container consumes too much memory

The syntax is represented by the below syntax and it supports OPTIONS like --all, --time, and --quiet.

Command syntax

docker restart OPTIONS CONTAINER_NAME

If a case multiple containers are to be restarted one can use the below command.

Restart multiple containers at once

docker restart $(docker ps -a -q)

2.1 Docker restart policy

Docker also let the user set the restart policy upon exit or failure. The restart policy is represented by –

Command syntax

docker run --restart=RESTART_POLICY_NAME CONTAINER_NAME

The command supports the following restart policies.

Restart policy nameResult
noDo not automatically restart the container when the container exits. It is a default behavior
on-failure[:max-retries]Restart only if the container exists with a non-zero exit status
alwaysAlways restart the container regardless of the exit status
unless-stoppedAlways restart the container regardless of the exit status, including on daemon startup, except if the container was put into a stopped state before the docker daemon was stopped

3. Demo

Let us dive into a practical demo.

3.1 Restarting a stopped container

Let us restart a stopped httpd container and for that, we’ll use the restart command. In the below command, upbeat_lichterman is the container name.

Command 1

docker restart upbeat_lichterman

If everything goes well the container will be restarted.

 

Fig. 1: Restarting the container

3.2 Running a container via policy

Let us run a Redis container with a restart policy of always. This will ensure that the container will be automatically restarted upon exit of failure. Users can use docker ps to check if the restart policy is active or not.

Command 2

docker run --name demo-redis -d --restart=always -p 6379:6379 redis
Fig. 2: Running a container via the restart-policy

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share! Happy learning and do not forget to share!

4. Summary

In this tutorial, we learned about Docker and the restart action of a docker container. You can also download the commands used in this tutorial from the Downloads section.

5. Download the Project

This was a tutorial on the restart action of a docker container.

Download
You can download the full source code of this example here: Restart Docker Container

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button