Instantly share code, notes, and snippets.

Embed
What would you like to do?
Forwarding ports from a docker container with Vagrant 1.6
# docker-host-vm/Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
# web port
config.vm.network :forwarded_port, host: 8080, guest: 8080
config.vm.provision "docker"
end

Forwarding ports from a docker container with Vagrant 1.6

Vagrant 1.6 has a really nice feature which allows you to run a docker environment from any machine that can run Vagrant (even a Mac)

Behind the scenes, Vagrant creates a host VM which runs the docker containers.

The "gotcha" is: Vagrant won't automatically forward ports all the way back from the container->vagrant host VM->your mac. You have to do that step manually, by configuring your own host VM for Vagrant to use for hosting docker containers with. That host VM is told what ports to open.

Here's how I set up a local Wordpress development testing container.

Directory structure looked like this (see attached files in this gist):

├── Vagrantfile
├── docker-host-vm
│   └── Vagrantfile

vagrant up does the magic and Wordpress :80 on the Docker container is forwarded to 8080 on the docker-host-vm, which is forwarded to localhost:8080 on my Mac.

# Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "wp" do |app|
app.vm.provider "docker" do |d|
# via https://github.com/mitchellh/vagrant/issues/3728
d.vagrant_vagrantfile = "./docker-host-vm/Vagrantfile"
d.image = "tutum/wordpress"
d.ports = [ "8080:80" ]
end
end
end
@simono

This comment has been minimized.

simono commented Jul 2, 2014

You can also use this Docker Host, which is a bit more lightweight:
https://github.com/simono/vm-examples/blob/master/docker-host/Vagrantfile

@jareware

This comment has been minimized.

jareware commented Jul 7, 2014

Very informative, thank you!

@audionerd

This comment has been minimized.

Owner

audionerd commented Jul 7, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment