在Docker for Mac中将容器映射到除localhost外的主机名 [英] Map container to hostname other than localhost in Docker for Mac
问题描述
我正在创建一个Nginx容器,我想在 http://api
上进行本地访问.使用Docker Machine,我假设运行 docker-machine create default
和 docker-machine ip default
来接收IP并将我的主机文件编辑为如下内容:
#docker-machine ip default->192.168.99.100
192.168.99.100 API
应将对 api \
的请求映射到Docker Machine IP并提供我的内容.
两件事使我感到困惑:
- 我通过Mac App启动Docker,可以创建Nginx容器并访问
http://localhost
上的内容.但是,运行docker-machine ls
不会返回任何机器.这令人困惑,因为我认为Docker必须在VM上运行. - 从头开始并启动Docker Machine,然后旋转容器似乎没有任何效果.换句话说,我仍然可以访问
http://localhost
上的内容,但不能访问http://api
我不想在 http://localhost
上访问我的容器,而是想在 http://api
上访问它.我该怎么做?
我正在为Mac 17.12和Docker Machine 0.14使用Docker.
根据您的问题:
我想访问的不是
第二件事::编辑主机
etc/hosts
文件并添加api 192.168.43.8 [您的IP]
这就是您在浏览器中看到的方式.
I am creating an Nginx container that I would like to access locally at
http://api
. Using Docker Machine, I assumed runningdocker-machine create default
anddocker-machine ip default
to receive the IP and editing my hosts file to something like this:
# docker-machine ip default --> 192.168.99.100
192.168.99.100 api
should map requests to
api\
to the Docker Machine IP and serve my content.Two things are confusing me:
- I launch Docker through the Mac App and can create Nginx containers and access content at
http://localhost
. However, runningdocker-machine ls
returns no machines. This is confusing because I thought Docker had to run on a VM.- Starting from scratch and starting Docker Machine, then spinning up containers seems to have no effect. In other words, I still can access content at
http://localhost
but nothttp://api
Instead of accessing my container at
http://localhost
I want to access it athttp://api
. How do I do this?I'm using Docker for Mac 17.12 and Docker Machine 0.14.
解决方案On the base of your this question:
Instead of accessing my container at http://localhost I want to access it at http://api. How do I do this?
Your docker run command:
docker run -it --rm --name test --add-host api:192.168.43.8 -p 80:80 apachehttpd
1st Thing: The -
-add-host
flag add value to /etc/hosts in your container/etc/hosts
so http://api will also response inside the container if ping inside that container. This is how will ping response inside container2nd Thing: Edit your host
etc/hosts
file and addapi 192.168.43.8 [your ip]
This is how you can see in Browser.
这篇关于在Docker for Mac中将容器映射到除localhost外的主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!