如何 ssh 到 Docker-machine VirtualBox 实例?

Docker-machine 版本0.2.0 Docker 版本1.6.2

我用的是 Docker-machine 使用 VirtualBox 创建机器。一切工作正常,但我想 ssh 进入机器本身,我找不到如何做到这一点的说明。我可以连接到 ssh 端口:

ssh $(docker-machine ip dev)

但是我不知道使用什么样的用户名/密码/身份文件。

77859 次浏览

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.

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.

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

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

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.

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

We can also ssh into the docker via following command -

docker exec -it /bin/sh

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

exemple : ssh docker@192.168.99.X pw:tcuser