Skip to content

Instantly share code, notes, and snippets.

@furikake
Last active August 29, 2015 14:01
Show Gist options
  • Save furikake/da7ff131a2ae30b56840 to your computer and use it in GitHub Desktop.
Save furikake/da7ff131a2ae30b56840 to your computer and use it in GitHub Desktop.
#docker #vagrant

Port Fowarding with Vagrant and Docker Provider with a Vagrant Proxy for MAC OS X

Exposing services (ports) running in a Docker container to OSX with Vagrant

Hopefully this helps the next poor soul trying to do work this out.

File structure

- Dockerfile
- Vagrantfile
- vagrant-proxy/
  - Vagrantfile

Dockerfile

Your Dockerfile, the container in the example uses ports 8082 and 9292 so includes the following lines:

EXPOSE 9292
EXPOSE 8082

Vagrantfile

OS X can't run Docker natively so we need to either:

  • use boot2docker by commenting out the d.vagrant_vagrantfile line) and Vagrant will do this automagically.
  • spin up a Vagrant VM to run Docker in. This is handy if you need a particular OS or due to boot2docker bugs (setting the wrong nameserver for me)

Expose the 8082 and 9292 ports on the Docker container as 8081 and 9291 on the host using d.ports = ["8081:8082", "9291:9292"]

Make sure you instruct Vagrant to add docker build arguments using d.build_args. Example tags the build as "furikake/vagrant-docker-example" using d.build_args = ["-t", "furikake/vagrant-docker-example"]

The config.vm.define "app" do |v| line defines this Docker container as "app" in Vagrant.

Vagrant.configure(2) do |config|
  config.vm.define "app" do |v|
    v.vm.provider "docker" do |d|
      # If you're on OS X and you wish to use a docker host other than boot2docker
      d.vagrant_vagrantfile = "./vagrant-proxy/Vagrantfile"
      d.build_dir = "."
      d.build_args = ["-t", "furikake/vagrant-docker-example"]
      # Expose :8082 and :9292 to the vagrant-proxy guest
      d.ports = ["8081:8082", "9291:9292"]
    end
  end
end

vagrant-proxy/Vagrantfile

This is the Ubuntu Precise 64bit guest VM that Docker will run containers on.

Tell Vagrant to set up port forwarding for host ports 8080 and 9290 to guest ports 8081 and 9291 ports using config.vm.network "forwarded_port", guest: 8081, host: 8080 and config.vm.network "forwarded_port", guest: 9291, host: 9290

Vagrant.configure(2) do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"
  # config.vm.network "private_network", ip: "10.0.2.30"
  config.vm.network "forwarded_port", guest: 8081, host: 8080
  config.vm.network "forwarded_port", guest: 9291, host: 9290
  config.vm.provision "docker"
  config.vm.provision "shell", inline:
    "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
end

Provision the Container

Don't think you need to provide FORWARD_DOCKER_PORTS=1 but I'll leave it here until I confirm.

Run the following:

osx$ # export FORWARD_DOCKER_PORTS=1
osx$ vagrant up app --provider=docker

Checks

Connectivity to Container from OS X

This should open the 9292 port on the Docker container via the guest VM.

osx$ telnet localhost 9290

Connectivity to Container from guest VM

SSH onto the Vagrant Proxy VM first:

osx$ cd vagrant-proxy
osx$ vagrant ssh

This should open the 9292 port on the Docker container.

guest$ telnet localhost 9291

Running docker ps will tell you the Docker redirect ports

guest$ docker ps

CONTAINER ID        IMAGE                               COMMAND                CREATED             STATUS              PORTS                                                                  NAMES
480a5a4a179a        furikake/vagrant-docker-example:latest   /opt/something   8 hours ago         Up 8 hours          0.0.0.0:2222->22/tcp, 0.0.0.0:8081->8082/tcp, 0.0.0.0:9291->9292/tcp   docker_app_1401284679 

The 0.0.0.0:8081->8082/tcp line tells you that:

  • The guest is listening on all IPs on port 8081
  • The guest will redirect TCP traffic to 8082 port on the container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment