Do you want to SSH into a Docker container?
Perhaps you need to have a look around, run commands, or debug something.
Well, most Docker containers don’t run the ssh daemon. So, there’s no SSH service available to service your requests. (Sorry!)
Instead, if you need to be able to get inside the Docker container, you need
Getting a terminal inside a Docker container
A Docker container isn’t the same as a virtual machine.
It’s basically a single application which runs inside a very small isolated box.
So, if you want to get inside the box, you can’t use SSH, because it doesn’t exist in the container. You have to do something different.
To be able to run commands inside a Docker container when it’s running, use
docker exec to start a shell, like
First, you’ll need to get the ID or name of your container using
$ docker ps CONTAINER ID IMAGE .... NAMES 79f6e55215a6 localhost/node-json-server:latest .... sleepy_maxwell
Here I could use the ID (
79f6e55215a6) or the name (
Most containers will have
sh installed, so this example should work for most containers:
docker exec -it my-container-ID /bin/sh
Now you’ll get a terminal and you can do as you like.
Whenever you want to exit, just press Ctrl+C. The container will keep running.
Some super-lightweight images don’t even include a shell by default. So if
bash don’t exist, then check with whoever has provided your image.
How to remember the command
Keep forgetting the command? I find this mnemonic handy:
I want to execute something in the container
So give me an Interactive Terminal.
in (the container name) …
And be quiet about it (shhhh!)
docker exec -it <container-ID> /bin/sh
If you really do need SSH
If you have a use case where you really do need to run an SSH service inside your container, then:
Find an existing Docker image to use as a base in a Dockerfile
Extend the image by installing packages for SSH, and running an SSH daemon.
If you’re not sure whether you need SSH, then you should lean towards not having it.
It’s an additional security risk, it means you need to manage keys, and you should avoid going into a container to make edits or changes.
If you want SSH because you want to make changes to your app, stop the container, and start a new one, with the updated configuration. (Docker containers are considered to be disposable or throw-away.)