Sometimes the best way to troubleshoot and understand how a Docker Container works is by exploring the Container. While there are a couple of ways to explore, one of the ways really found useful is docker exec. Basically, docker exec lets you run commands inside a Container, and for a Linux Container, we can use docker exec to bash in and run shell commands.

But to bash in, of course, you need to have the Container in a running state. Here this wiki has explained how you can setup your own Container, specifically a Linux Container with ASP.NET Core MVC Application running inside.

So let’s proceed.

Currently, we have no Containers running.
image
docker ps -a
And we have the following set of Images.
image
docker images
Here the dockerwebapp is the Image that we are planning on creating and running a Container from. We can run the Container by issuing  docker run.
# Runs in the foreground
# Even if pressed Ctrl+C, Container is still running
PS C:\WINDOWS\system32> docker run dockerwebapp
 
# Runs in the background/detached
PS C:\WINDOWS\system32> docker run -d dockerwebapp
 
# Runs in the foreground
# If pressed Ctrl+C, Container will stop running
# Container will be displayed in docker ps -a
PS C:\WINDOWS\system32> docker run -it dockerwebapp
 
# Runs in the foreground
# If pressed Ctrl+C, Container will stop running and will get deleted
PS C:\WINDOWS\system32> docker run -it –-rm dockerwebapp
In this case, we are going to run docker run –d dockerwebapp as we want the Container to be running in the background, so for next docker commands, We don’t want to open up another PowerShell window.
image
docker run -d dockerwebapp
Now we can see that container is running, it’s time to explore the image.
PS C:\WINDOWS\system32> docker exec -i -t blissful_roentgen /bin/bash
 
# -i -t can be use together like follows
PS C:\WINDOWS\system32> docker exec -it blissful_roentgen /bin/bash
image
docker exec
You can see that we are now in a Linux shell and we can use shell commands to explore the image. For instance to see the file system.
image
ls
Hope this helps.

Happy Coding.

Has other Languages:

 

Spanish (es-MX) - https://social.technet.microsoft.com/wiki/contents/articles/52172.ejecutar-y-explorar-contenedores-con-docker-linux-es-mx.aspx