

The second run shows the container has only been up for 19 seconds. $ sudo docker psĨ320e96172e4 testing_restarts "/bin/sh -c '/bin/bas" About a minute ago Up 19 seconds If we run docker ps again, we will see something interesting. This time we can see that the container is up and running but only for 21 seconds. $ sudo docker psĨ320e96172e4 testing_restarts "/bin/sh -c '/bin/bas" About a minute ago Up 21 seconds Let's see what effect this has on our container by executing a docker ps again. In the above command, we specified that Docker should apply the always restart policy to this container via the -restart flag. To understand restart policies better, let's see what happens when we use the always restart policy with this same container. It's possible to automatically restart crashed containers by specifying a restart policy when initiating the container. This means that, by default, if an application that is running within a container crashes, the container stops and that container will remain stopped until someone or something restarts it. With the docker ps results, we can see that when an application within a Docker container exits, that container is also stopped.

$ sudo docker ps -aĬONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESĪ35bb16634a0 testing_restarts "/bin/sh -c '/bin/bas" 9 minutes ago Exited (1) 8 minutes ago Let's take a look at running and non-running containers by using the -a flag. The reason for this is because docker ps by default only shows running containers. The docker ps command doesn't show any running containers. $ sudo docker psĬONTAINER ID IMAGE COMMAND CREATED STATUS PORTS Let's check the status of that container by running docker ps. $ sudo docker run -d -name testing_restarts testing_restartsĪ35bb16634a029039c8b34dddba41854e9a5b95222d32e3ca5624787c4c8914aįrom the above, it appears that Docker was able to start a container named testing_restarts. We can now start a container using the testing_restarts image by executing docker run. This build command created a Docker image with a tagged name of testing_restarts. Removing intermediate container 01e6f5e12c3f Removing intermediate container 5199db00ba76 Sending build context to Docker daemon 3.072 kB With the Dockerfile defined, we can now build our custom container using the docker build command. The final line tells Docker to execute the crash.sh script when the container is started. It will also add the crash.sh script into the / directory of the container. The above Dockerfile will build a container based on the latest ubuntu:14.04 image. The Dockerfile will contain the following three lines: FROM ubuntu:14.04 In order to build a custom container, we first need to create a simple Dockerfile. In order to run this script within a container, we'll need to build a custom Docker container which includes the crash.sh script.
#Sudo service docker start code#
The above script is simple when started, it will sleep for 30 seconds, and then it will exit with an exit code of 1 indicating an error. To facilitate this, we'll create a Docker container that executes a simple bash script named crash.sh. What Happens When an Application Crashes?īefore we get started with Docker's restart policy, let's understand a bit more about how Docker behaves when an application crashes.

#Sudo service docker start how to#
In today's article, we'll discuss how to use Docker's restart policy to automatically restart containers and avoid those late-night notifications. Getting a notification that Docker containers are down in production is one of the worst ways to spend your night.
