How to map docker ports using vagrant 1.6 and the docker provider

I’m currently trying to map my docker container ports from the container to the host (boot2docker). The end goal is to map those ports to my physical machine, but one step at a time.

My Vagrantfile currently looks like:

  • Cannot connect to PostgreSQL in Docker through PHP
  • Same IP address for multiple Bluemix Docker containers
  • gunicorn fails to launch python-eve
  • Getting Mac address from Docker Container
  • How to optimize HAProxy running in docker
  • Simulation “docker build -f” with ansible module “docker_image”
  • Vagrant.configure("2") do |config|
    
        config.vm.define "app1" do |a|
            a.vm.provider "docker" do |d|
                d.build_dir = "dockers/app1"
                d.name = "app1"
                d.ports << "8080:8080"
                d.ports << "8443:8443"
                d.volumes << "/vagrant/data/app1:/var/app1"
            end
        end
    
        config.vm.define "app2" do |a|
            a.vm.provider "docker" do |d|
                d.build_dir = "dockers/app2"
                d.name = "app2"
                d.ports << "8081:8081"
                d.link("app1:app1")
            end
        end
    end
    

    When I run vagrant up app1 --provider=docker the container spins up correctly, however when I do a docker ps I can see that ports have not been mapped.

    0.0.0.0:2222->22/tcp, 8080/tcp, 8443/tcp
    

    I am using VirtualBox, so I have used it GUI to port forward my physical machines 8080 to the hosts (boot2docker) 8080.

  • Were are the docker Remote API logs stored?
  • Running Visual Studio Remote Debugger in Windows Container (Docker managed)
  • Docker commands via php shell_exec
  • Using customized AMI in Elastic Beanstalk (Multi-container Docker) via awsebcli
  • How to convert a Vagrant box to a Docker image
  • ASP.NET Core Docker port forwarding on Ubuntu 16.10
  • One Solution collect form web for “How to map docker ports using vagrant 1.6 and the docker provider”

    Your configuration should work on Linux, but if you’re using a Virtualbox (I assume you’re on Mac or Windows), then you want a Vagrantfile for your VM to get it to your host.

    Vagrant.configure("2") do |config|
    
      config.vm.box = "busybox"
      config.vm.provider "virtualbox" do |v|
        v.memory = 768
        v.cpus = 2
      end
    
      config.vm.network :forwarded_port,
        guest: 8080, host: 8080
    
    end
    

    Let’s pretend that’s in host-vm/Vagrantfile relative to your current Vagrantfile. So your current Vagrantfile should look like:

    Vagrant.configure("2") do |config|
    
        config.vm.define "app1" do |a|
            a.vm.provider "docker" do |d|
                d.vagrant_vagrantfile = "host-vm/Vagrantfile"
                d.build_dir = "dockers/app1"
                d.name = "app1"
                d.ports = ["8080:8080"]
                d.ports = ["8443:8443"]
                d.create_args = ["-v", "/vagrant/data/app1:/var/app1"]
            end
        end
    
        config.vm.define "app2" do |a|
            a.vm.provider "docker" do |d|
                d.vagrant_vagrantfile = "host-vm/Vagrantfile"
                d.build_dir = "dockers/app2"
                d.name = "app2"
                d.ports = ["8081:8081"]
                d.link("app1:app1")
            end
        end
    end
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.