Docker – cannot access container from Mac Host

I can’t seem to access a simple Docker container running locally on my mac. I can run curl localhost from the container and see that the default Apache page is served up, but I can’t hit it from my machine’s browser.

I’m wondering if I have a VirtualBox config issue or something. Any help diagnosing the problem?

  • Docker trusted build doesn't show up in search
  • Oracle Java in Docker Container cannot resolve hostname after /etc/nsswitch.conf changed
  • Has anyone been able to establish a WinRM remote powershell session to a Windows Docker container?
  • How to configure a Jenkinsfile to build docker image and push it to a private registry
  • Docker on Ubuntu can't saturate CPU
  • Can I use docker engine with the latest pycharm 2016.3.2 on windows 10 pro
  • Dockerfile

    # Build the image of ubuntu 12.04 LTS
    from ubuntu:precise
    
    # Run apt-get update
    run apt-get -y update
    
    # Install LAMP
    run DEBIAN_FRONTEND=noninteractive apt-get -y install lamp-server^
    run apt-get -y install vim-tiny curl wget
    
    # Put custom scripts in the container and give proper permissions to them
    add ./startup.sh /usr/local/bin/startup.sh
    run chmod 755 /usr/local/bin/startup.sh
    
    add site.vhost /etc/apache2/sites-available/site
    run a2ensite site
    
    # Expose port 80 to the host machine
    expose 80
    

    site.vhost

    <VirtualHost *:80>
    
        ServerAdmin webmaster@localhost
    
        DocumentRoot /var/www
        <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            #Order allow,deny allow from all
        </Directory>
    
        # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg.
        LogLevel warn
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    startup.sh

    #!/bin/bash
    
    a2dissite default
    apache2ctl graceful
    /usr/bin/mysqld_safe &
    

    To start…

    I’ll build the image with docker build -t test1 . – which seems to run fine.

    On initial setup, I run docker run -d -v $(pwd)/mysql:/tmp/mysql test1 /bin/bash -c "cp -rp /var/lib/mysql/* /tmp/mysql" to setup MySQL.

    Then I run docker run -i -t -v $(pwd)/mysql:/var/lib/mysql -v $(pwd)/www:/var/www -p 8080:80 test1 /bin/bash to start the actual instance.

    Once at the terminal, I run service apache2 start and everything seems to be running great. If I run curl localhost I get the default page just fine.

    Everything seems great, it’s just hitting it from the Host that doesn’t work. I should be able to navigate to http://127.0.0.1:8080 and have that forward to the container on port 80, right?

  • exporting DOCKER_HOST in .bashrc produces a different result to the same command in the terminal
  • Start two servers in one docker container
  • Kafka with Docker dynamic advertised_host_name
  • Docker hangs on the RUN command OSX
  • gcloud docker pull fails with Untar exit status 2 unexpected fault address
  • Is it possible to run lxc based containers inside a docker based parent container?
  • 4 Solutions collect form web for “Docker – cannot access container from Mac Host”

    You’ll need to forward the port from the VM to the host. An example for the default “random” range from docker is:
    (from http://docs.docker.io/en/latest/installation/mac/#forwarding-vm-port-range-to-host)

    # vm must be powered off
    for i in {49000..49900}; do
     VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i";
     VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i";
    done
    

    But if you want to forward specifically 8080:

    VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8080,tcp,,8080,,8080"
    

    etc. You might want to pick a different port if you also use 8080 for testing things on your Mac itself.

    For those looking at this thread who are running Docker 1.8 or newer, the syntax from Andy’s and Muneeb’s comments no longer work (the theory is the same), since boot2docker has been deprecated and replaced with docker-machine and the docker VM name changed [1].

    Forwarding a single port Mac -> VM -> container

    If you just want to forward port 80 inside a container using the default network options to port 8080 on your Mac, do this:

    a) Expose port 80 either with an EXPOSE 80 in the Dockerfile or --expose=80 in docker run. Add /udp for UDP ports.

    b) Map the ports from the container to the VM with -p 8080:80 in docker run. Add /udp for UDP ports.

    c) Tell the docker VM (now called “default”) to add a NAT mapping between port 8080 on the VM and port 8080 on your Mac:
    VBoxManage controlvm default natpf1 'port8080,tcp,,8080,,8080'. The first port is on your Mac; the second port is on the VM.
    To delete the rule use VBoxManage controlvm default natpf1 delete port8080. Note: VBoxManage controlvm changes a running VM, modifyvm changes a VM that is stopped.

    Getting the IP address of your docker VM

    New syntax is docker-machine ip default (default is the name of docker VM).

    [1] https://docs.docker.com/installation/mac/

    You’re trying to view it on localhost (127.0.0.1) which is your Mac OS X. The docker is running inside a Virtualbox VM. Try:

    boot2docker ip

    That’ll give you the IP address of the VM and then you can connect to docker using ip_address:port e.g., 192.168.59.103:8080 (if 192.168.59.103 was the IP assigned to the Virtualbox VM)

    I just wrote a tutorial of how to use a host-only network and TCP routing to make this pretty easy. This way you don’t have to map every specific port.

    http://ispyker.blogspot.com/2014/04/accessing-docker-container-private.html

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.