Home>Article>Operation and Maintenance> Where does docker configure the port?

Where does docker configure the port?

coldplay.xixi
coldplay.xixi Original
2020-06-19 09:33:10 3861browse

Where does docker configure the port?

Where does docker configure the port?

Where docker configures the port:

1. Automatic mapping

# docker run -d -p 80 --name myweb 1311399350/myweb nginx -g "daemon off;"

- above p 80will randomly open a port on the docker host (you can use the docker port command to view it, or docker ps can also see it, here it is 32768) and map it to port 80 in the container.

2. Specify mapping

In addition to automatic mapping, you can also specify the mapping relationship, such as:

# docker run -d -p 80:80 --name myweb 1311399350/myweb nginx -g "daemon off;" # docker port myweb 80 0.0.0.0:80

It can be seen that the 80 of the host machine The port is mapped to port 80 of the container. There are pros and cons to such a designation. The advantage is that the port is known and needs to be used with care; the disadvantage is that multiple identical containers cannot be run and it is easy to conflict with the host application.

3. Expose the port specified by the EXPOSE directive in the dockerfile

We specify the port or port range exposed by the container in the dockerfile

EXPOSE 20010 EXPOSE 10011

Use capital letters The -P parameter exposes the port specified by the EXPOSE instruction in the dockerfile (the port in the container) to the local host and randomly binds it to the port of the local host.

# docker run -d -P --name myweb 1311399350/myweb nginx -g "daemon off;"

Use# docker port container containre-portto view the host port mapped by the container

# docker port myweb 80 0.0.0.0:32771

Recommended tutorial: "docker tutorial

The above is the detailed content of Where does docker configure the port?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn