I want to ssh or bash into a running docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg
Now I want to get something like this (go into the running container):
$ sudo docker run -t -i webserver
(or maybe 665b4a1e17b6
instead)
$ root@665b4a1e17b6:/#
However when I run the line above I get new CONTAINER ID:
$ root@42f1e37bd0e5:/#
I used Vagrant and I'd like to get a similar behaviour as vagrant ssh
.
The answer is Docker's
attach
command. So for my example above, the solution will be:For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use
attach
we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:or
From Docker 1.3 onwards:
Basically, if the Docker container was started using the
/bin/bash
command you can access it usingattach
. If not, then you need to execute the command to create a Bash instance inside the container usingexec
.Also to exit Bash without leaving Bash running in a rogue process:
Yep, it is that simple.
Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:
docker run -i -t --entrypoint /bin/bash <imageID>
Try this:
Source: https://docs.docker.com/engine/reference/commandline/run/
Based on @Timur's answer I've created the following handy script
Setup
Put
docker-ssh
file in your$PATH
with the following contentsNote: Some container do not contain
bash
, butash
,sh
etc. In these casesbash
shall be replaced in the above script.Usage
If you have only one running instance, simply run
Otherwise, provide it with a docker id parameter that you get from
docker ps
(first col)If your container doesn't have bash installed you could try sh:
Or look for shells in /bin first:
I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.
If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.
For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh
@jpetazzo has an awesome post about this subject. The short answer would be to use
nsenter
:P.S.: Don't forget to check the discussion in the comments of the post...
Cheers
You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.
This will be more "traditional" (ssh), instead of using an application-specific command like
docker attach
, and will eventually make it more 'portable' across systems and versions.Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:
UbuntuWithSSH-Docker
The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.