How To Attach To A Running Windows Container

In this blog post, I’ll show you how to attach or connect to a running Windows Container using Docker Client.

Sometime when running application on Containers there is a need to connect to a running container after it was created or while it’s running.

Docker gives us two options to connect to existing running Containers like Docker Attach and Docker Exec.

Both do the same things as you will see soon, but you might favor one over the other.

In my case, I like to use docker exec.

While connecting to a running Container we could view ongoing output, error or Information about things running in the Container.

How Does It Work?

In the example below, I’ll create a Windows Nano Server Container and exit it while keeping it running.

Docker run -it microsoft/nanoserver powershell.exe

If I type hostname you will see that I’m inside the container

I’ll exit the Container and keep it running using

Ctrl+PQ

I’ll note the Container name and use It in the next command to connect to it.

In my case the container ID is:

82e08c546fd5

As you can see below, I’m connected to the Container.

docker attach 82e08c546fd5

Docker Exec

I could also use the Exec command to connect to a running container using as shown below:

docker exec -it 82e08c546fd5 powershell

Docker exec will start a new PowerShell process which the attach will connect the existing running session that was left when exited.


Posted

in

by