Docker – 无法从Mac主机访问容器

我似乎无法访问我的Mac上本地运行一个简单的Docker容器。 我可以从容器中运行curl localhost ,看到默认的Apache页面已经提供,但是我无法从我的机器的浏览器中find它。

我想知道如果我有一个VirtualBoxconfiguration问题或东西。 任何帮助诊断问题?

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 & 

开始…

我将用docker build -t test1 .构build镜像docker build -t test1 . – 这似乎运行良好。

在初始设置,我运行docker run -d -v $(pwd)/mysql:/tmp/mysql test1 /bin/bash -c "cp -rp /var/lib/mysql/* /tmp/mysql"

然后运行docker run -i -t -v $(pwd)/mysql:/var/lib/mysql -v $(pwd)/www:/var/www -p 8080:80 test1 /bin/bash启动实际情况。

一旦在terminal,我运行service apache2 start ,一切似乎运行良好。 如果我运行curl localhost我得到的默认页面就好了。

一切看起来都很棒,只是从主机上打不起来而已。 我应该能够导航到http://127.0.0.1:8080 ,并转发到端口80上的容器,对不对?

您需要将该端口从VM转发到主机。 docker的默认“随机”范围的例子是:(从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 

但是如果你想特意转发8080:

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

等等。如果你也使用8080来testing你自己的Mac上的东西,你可能想select一个不同的端口。

对于那些正在运行Docker1.8或更新版本的线程来说,Andy's和Muneeb的注释语句不再起作用(理论是相同的),因为boot2docker已经被弃用,并且被docker-machine取代,docker的VM名称也改变了[ 1]。

转发单个端口的Mac – > VM – >容器

如果您只是想使用默认networking选项将容器内的端口80转发到Mac上的端口8080,请执行以下操作:

a)使用EXPOSE 80中的EXPOSE --expose=80--expose=80 docker run --expose=80将端口80公开。 为UDP端口添加/udp

b)在docker run使用-p 8080:80将容器中的端口映射到VM。 为UDP端口添加/udp

c)告诉docker虚拟机(现在称为“默认”)在VM上的端口8080和Mac上的端口8080之间添加NAT映射: VBoxManage controlvm default natpf1 'port8080,tcp,,8080,,8080' 。 第一个端口在你的Mac上; 第二个端口在虚拟机上。 要删除规则,使用VBoxManage controlvm default natpf1 delete port8080 。 注意: VBoxManage controlvm更改正在运行的VM, modifyvm更改已停止的VM。

获取您的docker虚拟机的IP地址

新的语法是docker-machine ip default (默认是docker VM的名字)。

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

您正试图在您的Mac OS X的本地主机(127.0.0.1)上查看它。docker工具正在Virtualbox VM中运行。 尝试:

boot2docker ip

这会给你的虚拟机的IP地址,然后你可以连接到docker使用ip_address:端口例如,192.168.59.103:8080(如果192.168.59.103是分配给虚拟机的虚拟机的IP)

我刚写了一篇关于如何使用主机专用networking和TCP路由的教程,使其变得非常简单。 这样你就不必映射每个特定的端口。

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