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
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
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!
Subscribe to Blog updates
Thanks, we've got you!
The ReSharper 2023.3 Release Candidate Is Now Available
Get a preview of all the latest features and improvements set to be shipped with the next major ReSharper and .NET tools releases by downloading the Release Candidate builds that have just landed. Here are the major improvements in each of the new builds: The ReSharper 2023.3 Release Can…
The Rider 2023.3 Release Candidate Is Now Available
There’s only a few days left before the next major release for Rider! Don’t feel like waiting? Get a sneak peek of Rider 2023.3 now by downloading the Release Candidate build that has just landed on our website. The Release Candidate build contains all of the notable changes that we’ve introduc…
Primary Constructors – Using C# 12 in Rider and ReSharper
Welcome to our series, where we take a closer look at the C# 12 language features and how ReSharper and Rider make it easy for you to adopt them in your codebase. If you haven’t yet, download the latest .NET 8 SDK and update your project files! In this series, we are looking at: Primary …
Visualize Entity Framework Relationships and Additional Query Analysis in ReSharper 2023.3
A lot of teams are using Entity Framework or EF Core to work with their database. As an Object-Relational Mapper (ORM), it bridges objects in code to a relational database model, so that as a developer you don’t have to worry too much about the actual database. We all know: that’s not entirely tr…