link是过时的了,尽量不要用。
dns内部集成,也可以用外部。
expose只是用于记录,并不真的。
publish是否起作用,也要看情况,是否被占用端口。
--------------------------------------
Embedded DNS server
Docker daemon runs an embedded DNS server which provides DNS resolution among containers connected to the same user-defined network, so that these containers can resolve container names to IP addresses. If the embedded DNS server is unable to resolve the request, it will be forwarded to any external DNS servers configured for the container. To facilitate this when the container is created, only the embedded DNS server reachable at 127.0.0.11
will be listed in the container’s resolv.conf
file. For more information on embedded DNS server on user-defined networks, see embedded DNS server in user-defined networks
Exposing and publishing ports
In Docker networking, there are two different mechanisms that directly involve network ports: exposing and publishing ports. This applies to the default bridge network and user-defined bridge networks.
- You expose ports using the
EXPOSE
keyword in the Dockerfile or the--expose
flag todocker run
. Exposing ports is a way of documenting which ports are used, but does not actually map or open any ports. Exposing ports is optional. - You publish ports using the
PUBLISH
keyword in the Dockerfile or the--publish
flag todocker run
. This tells Docker which ports to open on the container’s network interface. When a port is published, it is mapped to an available high-order port (higher than30000
) on the host machine, unless you specify the port to map to on the host machine at runtime. You cannot specify the port to map to on the host machine in the Dockerfile, because there is no way to guarantee that the port will be available on the host machine where you run the image.This example publishes port 80 in the container to a random high port (in this case,
32768
) on the host machine.$ docker run -it -p 80 nginx $ docker ps 64879472feea nginx "nginx -g ‘daemon ..." 43 hours ago Up About a minute 443/tcp, 0.0.0.0:32768->80/tcp blissful_mclean
The next example specifies that port 80 should be mapped to port 8080 on the host machine. It will fail if port 8080 is not available.
$ docker run -it -p 80:8080 nginx $ docker ps b9788c7adca3 nginx "nginx -g ‘daemon ..." 43 hours ago Up 3 seconds 80/tcp, 443/tcp, 0.0.0.0:80->8080/tcp goofy_brahmagupta
Links
Before Docker included user-defined networks, you could use the Docker --link
feature to allow a container to resolve another container’s name to an IP address, and also give it access to the linked container’s environment variables. Where possible, you should avoid using the legacy --link
flag.
When you create links, they behave differently when you use the default bridge
network or when you use user-defined bridge networks. For more information, see Legacy Links for link feature in default bridge
network and the linking containers in user-defined networks for links functionality in user-defined networks.