.NET Tools

Connecting to a Running Docker Container Shell

When using Docker in your development workflow, it is sometimes necessary to connect to a running Docker container to perform critical tasks or troubleshoot issues. For example, you may want to explore the file system, look at processes running in the container, tail a log file in the container, and more.

You can connect to a running Docker container in many ways: using the docker attach command, using docker exec, or (surprise!) with the click of a button in JetBrains Rider! Let’s have a look.

Attaching to a running Docker container

One way to attach to a running Docker container is by using the docker attach command. This command will attach the container’s standard input, output, and error streams to your local terminal.

To use this command, you’ll need to find out the container id first, which can be found using the command-line (docker ps, then copy the container id), or by using the context menu in the Rider Services tool window:

You can then run docker attach <container-id> in the terminal and be greeted with a shell prompt, log output, or… nothing!

Unfortunately, attaching to an already-running Docker container will only be valid if it’s running a shell as its entry point. This is because most container images will run a different executable, and attaching to such a container will usually result in being able to see the container’s output – not getting access to an interactive shell.

Start a new shell in a Docker container

Using docker exec is a more successful approach with most container images. With this command, you can start a new process in the container and interact with it. Additionally, docker exec is a bit easier as you can use the container name instead of the container id.

If you know a specific shell, like sh or bash is available in the container, you can connect to it with a one-liner. Note you’ll also need to specify the -it switches to make sure you can interact with the running process in the container:

docker exec -it <container-name> /bin/bash

You can use docker exec to run other commands as well. For example, if you want to run tail to look at the latest entries in a given file – or run any other command for that matter – you don’t need to use an interactive shell. Instead, you can run it with docker exec directly:

docker exec <container-name> tail /var/log/messages

Connect to a running Docker container using Rider

Another way to connect to a running Docker container’s shell is from inside JetBrains Rider. After connecting to Docker, the Services tool window gives you access to all functionality around Docker. You can start/stop containers, explore images, networks, and volumes, inspect a container’s environment variables, expose ports, and much more.

After selecting your container in the tree on the left-hand side, clicking the Terminal button will open a terminal inside the container and lets you work with it interactively. You can now run any command in the Docker container and look at the results.


Sometimes, you will need to connect to a terminal in a running Docker container and interact with it. Whether it is to try out the commands that will, later on, go in your Dockerfile, or to troubleshoot the environment in which your application will be running, there are several tools you can rely on to do so.

In this post, we’ve seen the docker attach command, the docker exec command, and the Services tool window in JetBrains Rider. Which approach is your favorite? Let us know in the comments!

image description