Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audionerd/d7d77d9af080a7a87d9b to your computer and use it in GitHub Desktop.
Save audionerd/d7d77d9af080a7a87d9b to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

jareware commented Jul 7, 2014

Very informative, thank you!

@audionerd
Copy link
Author

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