How to SSH into a Docker container
In most cases, you don't need SSH to get a shell inside a Docker container.
Docker containers provide isolated environments for running applications, ensuring consistency and portability. When you work with containers, you often need to get inside one: to run a command, check a configuration file, or troubleshoot a problem as it happens.
In most cases, you don't need SSH to get a shell inside a Docker container. Docker has built-in commands that open a shell in a running container, without installing anything extra or opening a port. This article covers those commands first, then shows how to reach containers on a remote server, and finally how to add an SSH server to a container safely for the cases where you really need one.
Why you need to reach your containers#
Accessing containers is essential for managing and troubleshooting containerized applications. From inside a container, you can inspect its files and environment, check whether it can reach other services, and run commands that help you find and fix errors.
You can also make quick changes, like editing a configuration file or installing a debugging tool, without rebuilding the image. Keep in mind that these changes are lost when the container is replaced, so anything permanent belongs in your Dockerfile.
Docker provides several built-in ways to reach a container:
docker execdocker rundocker attachdocker compose execdocker compose run
Each one has its own use case, so let's go through them.
How to use docker exec#
docker exec runs a command inside a container that's already running. It's the closest thing to "SSHing into" a container and the method you'll use most often.
First, find the container's name or ID by listing the running containers:
docker psThe output shows each running container:
CONTAINER ID IMAGE STATUS NAMES
f25ccfa2fe43 nginx:alpine Up 2 minutes webThen open a shell inside it, using its name or ID:
docker exec -it web shThe -i flag keeps the input open so you can type commands, and -t allocates a terminal. Without them, the shell exits immediately, or Docker reports the input device is not a TTY. Type exit to leave the shell. The container keeps running.
This example uses sh because many small images, such as those based on Alpine Linux, don't include Bash. If you try bash in one of them, Docker reports exec: "bash": executable file not found in $PATH. Images based on Debian or Ubuntu usually include Bash, so you can use docker exec -it web bash with them.
You can also run a single command without opening a shell:
docker exec web ls /usr/share/nginx/htmlThis prints the result and returns you to your own terminal:
50x.html
index.htmlNote that docker exec only works on a running container. If the container has stopped, docker ps -a lists it, and you can restart it with docker start first.
How to use docker run#
docker run creates and starts a new container from an image. With -it, it opens an interactive shell in the new container right away:
docker run -it --rm debian:trixie-slim bashThe --rm flag removes the container when you exit, which is useful for throwaway shells. This is handy when you want to explore an image, test a command in a clean environment, or debug a problem without touching your running containers.
The difference from docker exec is important: docker run always starts a new container, so it doesn't give you access to a container that's already running.
How to use docker attach#
docker attach connects your terminal to the main process of a running container, the one the container was started with. You see its output as it happens, and if the container was started with -it, you can type into it.
docker attach webIt doesn't open a new shell, so it's not a replacement for docker exec. Be careful when you leave: pressing Ctrl+C sends a stop signal to the main process, which usually stops the container. To detach without stopping it, press Ctrl+P followed by Ctrl+Q, which only works if the container was started with -it.
If you only want to watch a container's output, docker logs -f web is a safer choice, because it can't stop the container.
How to use Docker Compose#
Docker Compose lets you define and run multi-container applications from a YAML file. It has its own versions of exec and run that use the service names from that file instead of container names.
To open a shell in the running container of a service called app, use:
docker compose exec app shdocker compose exec allocates a terminal by default, so you don't need the -it flags.
To start a new one-off container for that service and open a shell in it, use:
docker compose run --rm app shUse the docker compose command (Compose V2), which comes with current versions of Docker. The older standalone docker-compose command (Compose V1) stopped receiving updates in 2023.
How to access containers on a remote server#
If your containers run on a remote server, you don't need an SSH server inside each container. SSH into the server itself, then use docker exec as usual:
ssh [email protected]
docker exec -it web shYou can also run Docker commands from your own machine against the remote server by creating a Docker context that connects over SSH:
docker context create my-server --docker "host=ssh://[email protected]"Then pass the context to any Docker command:
docker --context my-server exec -it web shThis uses your existing SSH access to the server, so you don't open any extra ports.
How to add an SSH server to your Docker container#
Sometimes you need a real SSH connection to a container, for example, for a tool that only supports SSH, like an SFTP client. In that case, you can install an SSH server in the image.
Do it carefully. An SSH server is one more service that can be attacked, and Docker runs one main process per container, so a container whose main process is sshd isn't running your application. Use this setup for a dedicated debugging or file-transfer container, not as a habit for every container.
Build an image with an SSH server#
The following Dockerfile installs OpenSSH, creates a non-root user called dev, turns off password and root logins, and allows only key-based logins:
FROM debian:trixie-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssh-server \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /run/sshd
# Create a non-root user to log in as
RUN useradd --create-home --shell /bin/bash dev
# Allow key-based logins only, and never as root
RUN printf 'PasswordAuthentication no\nKbdInteractiveAuthentication no\nPermitRootLogin no\n' \
> /etc/ssh/sshd_config.d/hardening.conf
# Add your public key
COPY --chown=dev:dev --chmod=600 authorized_keys /home/dev/.ssh/authorized_keys
RUN chmod 700 /home/dev/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D", "-e"]Here's what each part does:
/run/sshdis the directory the SSH server needs to start.- The
hardening.conffile turns off password logins and root logins, so the only way in is with an SSH key. COPYadds your public key as thedevuser'sauthorized_keysfile.sshd -D -eruns the SSH server in the foreground as the container's main process and sends its logs to Docker, so they show up indocker logs.
Run the container and connect#
Before you build the image, create a key pair for it in the same folder as the Dockerfile, and copy the public key to authorized_keys:
ssh-keygen -t ed25519 -f ./docker_ssh_key
cp docker_ssh_key.pub authorized_keysBuild the image and start a container from it:
docker build -t ssh-container .
docker run -d --name ssh-container -p 127.0.0.1:2222:22 ssh-containerThe -p 127.0.0.1:2222:22 option maps port 22 in the container to port 2222 on your machine. The 127.0.0.1 part makes the port reachable only from your own machine. Without it, Docker publishes the port on all network interfaces, so anyone who can reach your machine can try to log in.
Connect with your private key:
ssh -i ./docker_ssh_key -p 2222 dev@localhostYou're now logged in to the container as dev. Logging in as root, or with a password, is refused with Permission denied (publickey).
Keep two things in mind with this setup:
- The SSH host keys are generated when the image is built, so everyone with a copy of the image has them. Don't push this image to a public registry.
- You can't add an SSH server to a running container and connect to it from outside, because Docker only publishes ports when a container starts. For a running container, use
docker execinstead.
How to access your containers on Sevalla#
If you deploy your application on Sevalla, you don't need SSH to reach it either. Each application in the Sevalla dashboard has a Web terminal tab that opens a shell in a running process, right in your browser. sh is always available, and bash works if your image includes it.
Summary#
In most cases, docker exec -it <container> sh is all you need to get a shell in a running container. Use docker run to start a new container with a shell, docker attach to connect to a container's main process, and docker compose exec when you work with Docker Compose. For containers on a remote server, SSH into the server or use a Docker context, rather than running an SSH server in every container.
When you do need an SSH server in a container, use key-based logins, a non-root user, and a port published only on 127.0.0.1.
Need a host for the projects you build with Docker? Sevalla's Application Hosting deploys your app from a Dockerfile or straight from your Git repository.