SSH into a Docker Container and Run Commands
Ever found yourself staring at a running container, wishing you could just drop by with an SSH session like it was a little Linux box on your desk? You’re not alone. Below is the no‑frills guide that shows how to spin up an SSH server inside a container, hop in, and execute commands—without messing up your production image.
1. Why bother with SSH at all?
You might think docker exec solves everything, but there are times when you need a full terminal session: interactive debugging of services that expect a login shell, or connecting from another host to a container on a remote Docker host. In my own stack‑overflow fix‑ups I’ve seen developers run docker exec -it and then immediately forget to install the right utilities inside the container, leaving them with a shell that can’t resolve names or read /etc/hosts. SSH fixes that by giving you a “real” login environment.
2. Pick or build an image that supports SSH
Most official images (like Alpine) don’t ship OpenSSH server pre‑installed. Either start from ubuntu or add it yourself:
# Dockerfile snippet
FROM ubuntu:22.04
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server sudo
If you’re pulling an existing image, just check that /etc/ssh/sshd_config exists or add it with a docker run command.
3. Create the SSH user and set a password
# Inside the container (or Dockerfile)
RUN useradd -m -s /bin/bash devuser && \
echo "devuser:StrongPassword1" | chpasswd
By default, most containers run as root. Adding a non‑root user gives you a safer session and mirrors how production deployments usually look.
4. Open the SSH port in Docker
When launching the container, expose TCP 22:
docker run -d --name ssh-demo \
-p 2222:22 \
my-ssh-image
Mapping to 2222 keeps your host’s port 22 clear. The -d flag detaches so you can keep the shell open elsewhere.
5. Make sure the SSH daemon starts
Some images don’t automatically start services. Add a simple startup script:
# /usr/local/bin/entrypoint.sh #!/bin/bash /usr/sbin/sshd -D
Mark it as executable (chmod +x) and use it in your Dockerfile’s ENTRYPOINT.
6. Grab the container’s IP (optional)
If you’re SSH’ing from the host, you can just hit localhost:2222. From another machine on the same network, find the host’s IP:
docker-machine ip default # if using docker‑machine
Or check your Docker host’s network config.
7. SSH in and run commands
ssh -p 2222 devuser@localhost
You’ll be prompted for “StrongPassword1.” Once inside, everything works like a normal shell:
root@container:/# whoami devuser root@container:/# curl http://example.com
If you forget the password or need to reset it, just docker exec -it ssh-demo passwd devuser.
8. Clean up when finished
Docker’s great at pin‑pointing resource leaks. When you’re done:
docker stop ssh-demo docker rm ssh-demo
If you created a temporary image, prune it too: docker rmi my-ssh-image. No leftover SSH daemons running in the background.
9. Is SSH inside containers actually useful?
Honestly, if all you want is to run commands, docker exec is usually simpler and safer—it doesn’t require exposing another port or installing a service that could be abused. But when you need an interactive session that behaves like a real VM, especially across machines, the overhead pays off.
That’s it. No fancy tooling, just plain old SSH over Docker. Drop in, type away, and watch those bugs disappear faster than you can say “containerised.”