Machine: How to set up SSH tunnel using docker-machine

Created on 25 Aug 2015  路  9Comments  路  Source: docker/machine

Using boot2docker, I could set up an ssh tunnel to allow me to access running containers on localhost using the following command:

boot2docker ssh -L 5432:localhost:5432

However, if I try a similar command using docker-machine (with the machine name added) I get an error complaining about incorrect usage. What should I be doing? This is what I see:

docker-machine ssh dev -L 5432:localhost:5432
Incorrect Usage.

Usage: docker-machine ssh [arg...]

Log into or run a command on a machine with SSH.

Description:
   Arguments are [machine-name] [command]

Most helpful comment

As @rsslldnphy I also used the boot2docker ssh -L 5432:localhost:5432 syntax before upgrading to docker-machine. It is annoying that this feature does not work anymore, but as a workaround the following should work:

``` r, engine='bash', count_lines
machine=default #replace default if you use another name for the docker machine
ssh -i ~/.docker/machine/machines/$machine/id_rsa \
docker@$(docker-machine ip $machine) \
-L 5432:localhost:5432

or in the shorter version:

``` r, engine='bash', count_lines
machine=default; ssh -i ~/.docker/machine/machines/$machine/id_rsa docker@$(docker-machine ip $machine) -L 5432:localhost:5432

Hope this helps somebody and really hope this could be reimplemented into docker-machine ssh

All 9 comments

same here :(

i'm using a mac... but use the docker CLI if you need access to a container. SSH is only used for accessing the host AFAIK

install the docker cli for mac and use the docker command directly from your terminal

# curl -L https://get.docker.com/builds/Darwin/x86_64/docker-latest > /usr/local/bin/docker
# chmod +x /usr/local/bin/docker
# curl -L https://github.com/docker/machine/releases/download/v0.4.0/docker-machine_darwin-amd64 > /usr/local/bin/docker-machine
# chmod +x /usr/local/bin/docker-machine

then you can create a new docker-machine

# docker-machine create --driver virtualbox dev
# docker-machine env dev
# eval "$(docker-machine env dev)"
# docker run -ti busybox

then you will be in the shell. or you can run docker exec just like normal from the docker cli.

You can at least still reach the containers that have been run with -p/-P by using the docker-machine ip for that particular host. (postgresql://postgres:@<docker-machine ip docker-vm>:5432)
I wonder if there is another way we could refer to them as running on localhost, as it seems to be how my tests have been set up (expecting everything locally). Maybe I just have to specify the internal IP of the vm host every time I want to test, but it would be useful for it to work like boot2docker did in this respect.

As @rsslldnphy I also used the boot2docker ssh -L 5432:localhost:5432 syntax before upgrading to docker-machine. It is annoying that this feature does not work anymore, but as a workaround the following should work:

``` r, engine='bash', count_lines
machine=default #replace default if you use another name for the docker machine
ssh -i ~/.docker/machine/machines/$machine/id_rsa \
docker@$(docker-machine ip $machine) \
-L 5432:localhost:5432

or in the shorter version:

``` r, engine='bash', count_lines
machine=default; ssh -i ~/.docker/machine/machines/$machine/id_rsa docker@$(docker-machine ip $machine) -L 5432:localhost:5432

Hope this helps somebody and really hope this could be reimplemented into docker-machine ssh

Thank you, @emilingerslev, that's really helpful.

Glad I could help @rsslldnphy !

FYI all, https://github.com/docker/machine/pull/1791 will fix this when merged

Closing as #1791 is merged. Look for it in the next release!

This is actually not enough, since there is no reliable way to get the SSH port number. I filed #1981.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moander picture moander  路  5Comments

masaeedu picture masaeedu  路  4Comments

diver-sity picture diver-sity  路  4Comments

huseyinbabal picture huseyinbabal  路  4Comments

hananeglit picture hananeglit  路  3Comments