88

docker-machine version 0.2.0 docker version 1.6.2

I'm using docker-machine to create a machine using VirtualBox. Everything works fine but I'd like to ssh into the machine itself and I find no instructions on how to do this. I can connect to the ssh port:

ssh $(docker-machine ip dev)

But I've no idea what username / password / identity file to use.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Roy Truelove
  • 22,016
  • 18
  • 111
  • 153
  • 4
    If you -1 put a comment and explain what is wrong with this question and allow the asker to amend. – Roy Truelove May 19 '15 at 15:57
  • the recommanded docker way to connect inside a container is `docker exec -it my_container /bin/bash` see the doc https://docs.docker.com/reference/commandline/cli/#exec – user2915097 May 19 '15 at 16:00
  • @user2915097 The question was to ssh into the machine itself not a docker container – Roy Truelove May 19 '15 at 16:35
  • 1
    A lot of Docker questions get downvoted if they have no programming content. Arguably, this question should be on SF, but it's a judgement call really. – Adrian Mouat May 19 '15 at 16:35

8 Answers8

129

You can log into docker-machine hosts by just running

docker-machine ssh default

(Using the "default" host here)

The identity files should be stored under ~/.docker/machine/machines. If you want to log into a container (as opposed to the host), use docker exec as suggested by user2915097.

Patryk
  • 22,602
  • 44
  • 128
  • 244
Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • So i have another PC (different from the one used to setup the remote host) and i want to be able to access it via docker-machine on this PC (just like i am doing from the one used to provision). How do i go about that? – mahoriR May 17 '19 at 06:23
73

if you really need to do it via ssh, this is working with docker 1.8.2

init docker:

eval "$(docker-machine env default)"

get the IP from your default docker machine:

docker-machine ip default

this prints something like this out: 192.168.99.100

ssh docker@192.168.99.100

password is tcuser but you can also use the identity file, see other answer

timaschew
  • 16,254
  • 6
  • 61
  • 78
  • 9
    I am very curious about how you got to know that the password is ```tcuser```. Would you reveal your secret, please? :-) – fmquaglia Jan 19 '16 at 23:00
  • 2
    found somewhere else, but you can see it also here: https://github.com/docker/machine/search?utf8=%E2%9C%93&q=tcuser – timaschew Jan 21 '16 at 09:33
25

Finally, I found an answer :

I'm on Windows with Docker Toolbox (Docker Machine).

If I docker-machine -D ssh default, I find that the SSH parameters should be :

Host : localhost
Port : 51701
User : docker
Key : .docker\machine\machines\default\id_rsa

When I change my Putty/MobaXterm settings to match, voila, I can SSH into the container.

  • Hey, thank you. Found your answer on top of results. Saved me from hours of frustration. +1 – f123 Sep 13 '15 at 12:35
  • 1
    To add, in my case SSH port was 58361. All the required details are printed when using -D flag. `Using SSH client type: external {[-o BatchMode=yes -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o ConnectionAttempts=3 -o ConnectTimeout=10 -o ControlMaster=no -o ControlPath=none docker@127.0.0.1 -o IdentitiesOnly=yes -i C:\Users\\.docker\machine\machines\default\id_rsa -p 58361] C:\Program Files\Git\usr\bin\ssh.exe}` – rohitmohta Jun 19 '16 at 02:57
  • Where I can get it from `docker-machine inspect`? It printed non local address in `Driver->IPAddress` variable – falko May 11 '19 at 21:28
12

For the hackers out there, here is a script that will ssh into the 'active' docker-machine. This also gives you the values for the ssh_key, ssh_port, and the ssh_user, making it possible to do things like rsync between the localhost and the VM.

#!/bin/bash
docker_machine_name=$(docker-machine active)
docker_ssh_user=$(docker-machine inspect $docker_machine_name --format={{.Driver.SSHUser}})
docker_ssh_key=$(docker-machine inspect $docker_machine_name --format={{.Driver.SSHKeyPath}})
docker_ssh_port=$(docker-machine inspect $docker_machine_name --format={{.Driver.SSHPort}})

ssh -i $docker_ssh_key -p $docker_ssh_port $docker_ssh_user@localhost

You can copy and paste that into your terminal, line for line, and it will work. Or, make the script into a function, and feed it the name as an argument.

robert
  • 1,402
  • 1
  • 15
  • 21
5

If for some reason you'd rather use the ssh command rather than docker-machine ssh, you can do

ssh `docker-machine ip machine_name` -ldocker -i ~/.docker/machine/machines/machine_name/id_rsa
ejoubaud
  • 5,013
  • 6
  • 37
  • 39
2

For mac OX,the machine and its keys are located here (make sure you have the keys in there, something like the below:

~/project/dev/docker_notes za$ ls /Users/za/.docker/machine/machines/default/
.DS_Store        ca.pem           config.json      disk.vmdk        id_rsa.pub       server-key.pem
boot2docker.iso  cert.pem         default/         id_rsa           key.pem          server.pem

1) list available vms.

> ~/project/dev/docker_notes za$ docker-machine ls
>     NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
>     default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.0

In my case, the name of the machine is default. So, just

~/project/dev/docker_notes za$ docker-machine ssh default


                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.0, build HEAD : 32ee7e9 - Wed Apr 13 20:06:49 UTC 2016
Docker version 1.11.0, build 4dc5990
docker@default:~$ vi 
.ash_history  .ashrc        .docker/      .local/       .profile      .ssh/         log.log
docker@default:~$ ls
log.log

As you can see, I am able to ssh into docker-machine/instance.

docker@default:~$ uname -a
Linux default 4.1.19-boot2docker #1 SMP Thu Apr 7 02:41:05 UTC 2016 x86_64 GNU/Linux

You can also follow this > howto - docker

z atef
  • 7,138
  • 3
  • 55
  • 50
0

you can use use the ordinary synatx used on your terminal login :docker && pasword:tcuser

exemple : ssh docker@192.168.99.X pw:tcuser

DEVOPS
  • 1
-1

We can also ssh into the docker via following command -

docker exec -it /bin/sh

jitenagarwal19
  • 308
  • 1
  • 9