How to Connect to a Docker Container

Connecting to a running Docker container is helpful when you want to see what is happening inside the container. If the Docker container does not work as expected, you can attach to the container or get a shell to the container and run commands such as ps
or top. You can also enter the container to inspect files, troubleshoot services, or test commands before updating your Dockerfile
.
This tutorial explains how to attach to the container’s main running process and how to get a shell to a running container.
Quick Reference
For a printable quick reference, see the Docker cheatsheet .
| Task | Command |
|---|---|
| Get a shell in a running container | docker exec -it <container> /bin/bash |
| Run a single command in a container | docker exec <container> ls /var |
| Attach to the main process | docker attach <container> |
| Detach without stopping | Ctrl-p Ctrl-q |
| View container logs | docker logs <container> |
| Follow logs in real time | docker logs -f <container> |
| Shell via Docker Compose | docker compose exec <service> /bin/bash |
Get a Shell to a Container
The most common way to connect to a Docker container is to open an interactive shell inside it. The docker exec command
allows you to run commands inside a running container.
To get a Bash shell inside a running container, use the -it flags with docker exec:
docker exec -it my_container /bin/bashThe -i option stands for interactive, and -t tells Docker to allocate a pseudo TTY device. Your command prompt will change, indicating that you are now working on the container shell.
If the container does not have Bash installed, use sh instead:
docker exec -it my_container /bin/shFrom here, you can run commands in the same way as you would do on any other Linux server. For example, to see the current environment variables
, type env:
envWhen you are done, type exit to leave the container shell and return to your host terminal.
Run a single command
You do not need to open an interactive shell to run a single command. Pass the command directly after the container name:
docker exec my_container ls /varbackups cache lib local lock log mail opt run spool tmpAttach to a Container
Although it is possible to run multiple processes in a container, most Docker containers run only a single process. The command that is executed when starting a container is specified using the ENTRYPOINT and/or CMD instruction.
The docker attach command
allows you to attach your terminal to the running container. This is useful when you want to see what is written in the standard output in real time, or to control the process interactively.
To better understand how the attach command works, let us run
a new detached Nginx container using the official Nginx image:
docker container run --name my_nginx -d -p 8080:80 nginxThe -p 8080:80 option tells Docker to map port 80 in the container to port 8080 on the host machine.
List the containers to make sure the “my_nginx” container is running:
docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e1c4974a8d8 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp my_nginxAttach to the container using the container’s ID or name:
docker container attach my_nginxThe default command of the Nginx image is set to CMD ["nginx", "-g", "daemon off;"]. When you run the attach command, your terminal attaches to the nginx process.
Open 127.0.0.1:8080 in your browser and you can watch the output of the Nginx process in real time.
To detach from the container without stopping it, use the Ctrl-p Ctrl-q key combination. Pressing Ctrl-c stops the container.
If the running process you are attaching to accepts input, you can send instructions to it.
View Container Logs
To view the output of a container without attaching to it, use the docker logs command:
docker logs my_nginxTo follow the logs in real time, similar to tail -f, add the -f flag:
docker logs -f my_nginxTo see only the last 50 lines, use --tail:
docker logs --tail 50 my_nginxThe docker logs command is the preferred way to inspect container output, as it does not interfere with the running process.
Connect via Docker Compose
If you manage containers with Docker Compose
, use docker compose exec to get a shell to a running service:
docker compose exec my_service /bin/bashThis works the same way as docker exec but uses the service name defined in your docker-compose.yml file instead of the container name.
Troubleshooting
“OCI runtime exec failed: exec failed: unable to start container process”
This error usually means the shell you requested does not exist in the container image. Try /bin/sh instead of /bin/bash. Minimal images like Alpine use sh by default.
“Error response from daemon: container is not running”
You can only exec or attach to a running container. Start the container first with docker start <container>, then try again.
Container stops when you press Ctrl-c during attachCtrl-c sends a SIGINT to the main process, which usually stops the container. To detach without stopping, use the Ctrl-p Ctrl-q sequence instead.
FAQ
What is the difference between docker exec and docker attach?docker exec starts a new process inside the container, such as a shell or a command. docker attach connects your terminal to the container’s existing main process. Use exec when you want to run additional commands and attach when you want to see or interact with the main process output.
Can I connect to a stopped container?
No. Both docker exec and docker attach require the container to be running. Start the container first with docker start <container>, then connect to it.
How do I connect as a specific user?
Use the -u flag with docker exec. For example, to connect as root: docker exec -it -u root my_container /bin/bash.
How do I run a command in the background inside a container?
Use the -d (detached) flag with docker exec: docker exec -d my_container command. The command runs inside the container without attaching your terminal.
Conclusion
The docker exec and docker attach commands allow you to connect to a running container. Use docker exec -it <container> /bin/bash to get an interactive shell, and docker attach to connect to the main process. For Compose-based setups
, use docker compose exec instead. To learn how containers reach each other across networks, see the Docker networking guide
.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page