Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to be able to connect to the Docker API when using Vagrant? #2280

Closed
ucarion opened this issue Oct 18, 2013 · 6 comments · Fixed by #2357
Closed

Explain how to be able to connect to the Docker API when using Vagrant? #2280

ucarion opened this issue Oct 18, 2013 · 6 comments · Fixed by #2357

Comments

@ucarion
Copy link
Contributor

ucarion commented Oct 18, 2013

Sorry if this issue could be best addressed by using StackOverflow, but for the past few days I've been having trouble trying to set up Docker such that I can connect to the remote API from my host machine.

I imagine this is pretty trivial to set up, but I can't find where this is documented and I think it should be mentioned in the installation docs for using Docker with Vagrant since it's probably a common use-case.

If someone can explain how it's supposed to be done, I'll be more than happy to document this myself.

@pyotr777
Copy link

First, you have to edit Docker config file /etc/init/docker.conf (in Vagrant VM) like this:

description     "Docker daemon"

start on filesystem and started lxc-net
stop on runlevel [!2345]

respawn

script
    /usr/bin/docker -d -H=tcp://0.0.0.0:4243/
end script

Note the "-H=...", your Docker API will be available on port 4243.

Restart Docker :

stop docker
start docker 

My Vagrant VM starts with SSH port (22) forwarded to host 3222 port:

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 3222 (adapter 1)
...

To be able to reach Docker API from host machine, create SSH tunnel :

ssh -L 1024:localhost:4243 -p 3222 root@localhost

(Enter your vagrant user password -- "vagrant" by default.)
Now from your host machine you can reach your Docker API on port 1024 of localhost.

curl -X GET http://localhost:1024/containers/json?all=1 | python -mjson.tool

-- this will list Docker containers.

@ucarion
Copy link
Contributor Author

ucarion commented Oct 18, 2013

Hi @pyotr777 , thanks for helping me out!

I modified /etc/init/docker.conf as you suggested, and I restarted docker. I can connect to docker from within the Vagrant VM:

vagrant@precise64:~$ curl localhost:4243/version
{"Version":"0.6.3","GitCommit":"b0a49a3","GoVersion":"go1.1.2"}

When I start my vagrant VM I don't see any messages about port forwarding:

docker (master) ulyssecarion$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Resuming suspended VM...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
docker (master) ulyssecarion$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.8.0-31-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Oct 18 03:44:52 2013 from 10.0.2.2

So I assume that this means that SSH is on port 22? In any case, when I try to run the command:

vagrant@precise64:~$ ssh -L 1024:localhost:4243 -p 22 root@localhost
root@localhost's password: 
Permission denied, please try again.
root@localhost's password: 
Permission denied, please try again.
root@localhost's password: 
Permission denied (publickey,password).

(I'm entering "vagrant" as the password, and this problem happens whether or not I run this command as sudo.)

I think this is just because I have the wrong password for root? When I try to run su, the password "vagrant" still doesn't work. This is weird, because I don't recall changing any passwords anywhere.

Edit: never mind, I figured out how to change root's password to "vagrant" and I can run the ssh command. When I do that, this happens:

vagrant@precise64:~$ ssh -L 1024:localhost:4243 -p 22 root@localhost
root@localhost's password: 
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.8.0-31-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Oct 18 16:06:30 2013 from 127.0.0.1
root@precise64:~# 

Is this what's supposed to happen? It does seem that I can connect to both 1024 and 4243 from within this newly-created ssh terminal:

root@precise64:~# curl localhost:4243
404 page not found
root@precise64:~# curl localhost:1024
404 page not found

And when I try to connect from within the terminal I get after calling vagrant ssh, I can connect to 1024 as well.

But when I try to connect from my host plain old terminal:

~  ulyssecarion$ curl localhost:1024
curl: (7) couldn't connect to host
~  ulyssecarion$ curl localhost:4243
curl: (7) couldn't connect to host

What am I doing wrong?

@pyotr777
Copy link

I suppose by default vagrant forwards 22 port to 2222. In this case you have to run from you host machine this:

host machine:~$ ssh -L 1024:localhost:4243 -p 2222 vagrant@localhost

Is your Vagrant machine working on VirtualBox? If so, you can check forwarded port settings in VirtualBox GUI in Network > Adapter 1 > Advanced > Port Forwarding:
screen shot 2013-10-19 at 9 42 24 am

If your connect is successful, you'll have to open a new terminal window on your host machine to run more commands.

I forgot to mention, that after setting Docker Remote API on 4243 port, you will no longer be able to run commands like this:

sudo docker ps -a

Instead you'll have to connect to Docker on port 4243 like this:

sudo docker -H=tcp://localhost:4243 ps -a

@ucarion
Copy link
Contributor Author

ucarion commented Oct 19, 2013

@pyotr777 Yes, that worked! Thanks for your help. You're a godsend.

Do you think it'd be worthwhile to submit a PR that adds these details to the docs?

@pyotr777
Copy link

Strictly speaking, Vagrant VM is not part of the Docker, and neither is SSH tunneling.
But I agree, that the part of this thread explaining how to set up Docker remote API and how to access docker daemon after that could be included in the documentation, as now it is poorly documented (at least in what I can find here: http://docs.docker.io/en/latest/api/docker_remote_api_v1.6/).

@shykes
Copy link
Contributor

shykes commented Oct 23, 2013

Not in the docs, but a PR to a doc in contrib/vagrant/ would be useful.

@solomonstre
@docker

On Wed, Oct 23, 2013 at 8:46 AM, Peter notifications@github.com wrote:

Strictly speaking, Vagrant VM is not part of the Docker, and neither is SSH tunneling.

But I agree, that the part of this thread explaining how to set up Docker remote API and how to access docker daemon after that could be included in the documentation, as now it is poorly documented (at least in what I can find here: http://docs.docker.io/en/latest/api/docker_remote_api_v1.6/).

Reply to this email directly or view it on GitHub:
#2280 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants