Bootstrap FreeKB - Docker - Restart a container using the docker restart command
Docker - Restart a container using the docker restart command

Updated:   |  Docker articles

This assumes you have installed Docker on LinuxDocker is running and that you have created a container.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

The docker restart command is used to restart a container. In this example, the container named my-container will be restarted.

docker restart my-container

 

The docker ps command can be used to determine if the container was successfully restarted. The "Created" output should remain the same, but the "Status" output should return the time that the container was restarted. If the status of the container is "Exited", use the docker logs command to determine why the container has exited.

docker ps

CONTAINER ID     IMAGE              COMMAND                 CREATED       STATUS        PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  8 days ago    Up 6 minutes            my-container

 

Or, the -a or --attach option can be used to print stdout and stderr to the console.

docker restart --attach my-container

 

It is also important to recognize that if you do not use the --restart option when creating or starting a Docker container using the docker run command, the Docker container will NOT start up if the Docker server or daemon is restarted or if the host server is restarted. For this reason, it is almost always required to include the --restart option when creating or starting a Docker container using the docker run command. The restart options are:

  • no
  • on-failure:[max-retries]
  • always - The Docker container will be started if the Docker server or daemon is restarted or if the host server is restarted
  • unless-stopped - The Docker container will be started if the Docker server or daemon is restarted or if the host server is restarted as long as the Docker container was not manually stopped
docker run --restart unless-stopped my-container

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 708d07 in the box below so that we can be sure you are a human.