Bill
Bill

Reputation: 169

Access Docker container running in Vagrant

Hi I'm using vagrant and docker-compose to run a simple web app. It works fine on OSX, but when doing it in a vagrant box (ubuntu/trusty64) it won't work.

I don't want to use the docker provider for vagrant, since I'm testing this for another type of deployment. This should work since I'm doing the exact same thing as on my local machine.

Why can't I even curl localhost port 80 (or any port I've tried) on the app?

root@vagrant-ubuntu-trusty-64:/home/vagrant/slack-challenge# docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                  NAMES
03f4740887d7        slackchallenge_nginx   "/bin/sh -c 'servi..."   43 minutes ago      Up 12 minutes       0.0.0.0:8080->80/tcp   slackchallenge_nginx_1
ed80831b69a9        slackchallenge_fpm     "/usr/bin/supervisord"   43 minutes ago      Up 12 minutes       9000/tcp               slackchallenge_fpm_1
140ea815843b        mariadb                "docker-entrypoint..."   43 minutes ago      Up 12 minutes       3306/tcp               slackchallenge_db_1
root@vagrant-ubuntu-trusty-64:/home/vagrant/slack-challenge# curl localhost
curl: (7) Failed to connect to localhost port 80: Connection refused
root@vagrant-ubuntu-trusty-64:/home/vagrant/slack-challenge#

seems like something networky.

Upvotes: 1

Views: 4293

Answers (2)

programmerq
programmerq

Reputation: 6554

In your example, it looks like the only container that you are running that has a published report is as follows:

CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                  NAMES
03f4740887d7        slackchallenge_nginx   "/bin/sh -c 'servi..."   43 minutes ago      Up 12 minutes       0.0.0.0:8080->80/tcp   slackchallenge_nginx_1

Under the PORTS column, this indicates that the host's port 8080 is mapped to the container's port 80. In this set up, I would expect curl localhost:8080 to work instead of curl localhost because it is the host's port 8080 that is set to listen.

If you want the container's port 80 to be accessible on the host's port 80, then you would need to publish port 80 to port 80.

Upvotes: 0

Bill
Bill

Reputation: 169

yeah I just needed to enable port forwarding like so:

config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"

And now I can connect on my local port 80, which is actually inside my docker container running inside my vagrant vm, kind of confusing.

$ curl localhost:8080
Hello, world!

Upvotes: 6

Related Questions