How to Start a Windows Container and Keep it Running

Not a lot of people know it but by default, if you start a Windows Container without any running services docker will shut the container down.

Because Docker if very efficient when it comes to resources management once it detects that nothing is running on the Container the container will be turned off.

To show you how it works, I will start with an example of the behaviour and then give you a solution to bypass this.

In my case, sometimes I need to test all kind of things on the OS and need to run a basic windows image and run a few things inside the OS.

Example

Below you can see that I am starting a new container using the cmdlet below

docker run -d --name nanof1 microsoft/nanoserver

And after I am running docker ps the container is not running however when I run docker ps -a the container can be seen as exited.

`Solution

The solution here will be to add the ping command that will keep the ping process running in the background and as a result, the container will keep running.

docker run -d --name nanof1 microsoft/nanoserver ping -t localhost

If I want to see what is going on inside the Container I can connect to the container using the line below

Docker attach nanof1

Connect

To connect and use or run other commands inside the container I can use the command below which will start a new terminal session using PowerShell inside the container.

Docker exec -it nanof1 powershell

Leave a Reply

Your email address will not be published. Required fields are marked *