Image of construction site

How to keep containers running

Q. I created a new container however as soon as its created its status is stopped, why?

A. Consider running the following command to create a new container:

docker run -d --name demo1 microsoft/windowsservercore

As soon as the container is created it will stopped which can be shown with

docker ps -a

The reason for this is the docker model does not follow the same model as a traditional Windows instance. Docker cares about the processes that run in the containers and as soon as the process that the Docker container was created to run exits then the container hosting the process is automatically stopped. One solution is to make sure you specify a process that constantly runs, for example:

docker run -d --name demo1 microsoft/windowsservercore ping -t localhost

[Container World  delivers real-world case studies from the cloud-native ecosystem, hands-on technical education, the best speakers and cutting-edge startups under one roof. Get your ticket.]

This works as the container is created to run a never ending ping to itself (although is wasteful with resources). A better option than this but using the same approach is Microsoft has an actual script available at that runs while the service it is monitoring stays running which will keep the container running even when detached, for example it could be configured to monitor the IIS service in a container.

The other option is to rewrite your application to run as a synchronous process instead of a background service and make that the process that the container runs and the container stays running while the application is executing.

You can also use docker start to run a container that is stopped however this is not a good solution as it requires multiple steps to create and keep the container running and really goes against the lifecycle model of a container.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.