Skip to content

Instantly share code, notes, and snippets.

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| = "hashicorp/precise64"
# web port :forwarded_port, host: 8080, guest: 8080
config.vm.provision "docker"

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
d.vagrant_vagrantfile = "./docker-host-vm/Vagrantfile"
d.image = "tutum/wordpress"
d.ports = [ "8080:80" ]
Copy link

simono commented Jul 2, 2014

You can also use this Docker Host, which is a bit more lightweight:

Copy link

jareware commented Jul 7, 2014

Very informative, thank you!

Copy link

audionerd commented Jul 7, 2014

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