Docker port redirection. Generally, a program connects to a service through a port. When a service runs inside a container, you can connect to the service by binding the IP address and port bound to the container. However, this IP address of the container is a local IP address, and the port of the Service running inside the container is inaccessible outside. The IP address of the docker container provides a simple method to access services in the container.
Allow non-local clients to access the internal services of the container. docker provides a method to bind the service port of the container to the IP address of the local system. In this way, you can easily communicate between multiple containers, docker provides a connection mechanism.
1. Automatically map the expose port to the local system.
You can use the-p parameter to automatically bind all the ports of the expose container to the local system. The ing port will be automatically selected in 49000-49900, you can use docker ps, docker inspect, or docker port to view the port mapped to that port.
2. Bind a container port to the local system
You can also use the-p parameter to bind a container port to the IP address of the local system. Use the following command in the docker run command:
Docker run-P [([If you do not specify the local host IP interface, it is bound to all available IP interfaces by default. If you do not provide the host port, it will be automatically selected for binding. TCP port is used by default, but you can also specify UDP port. The docker PORT command can be used to list the container port and the IP address and port of the local host for automatic ing.
3. connect to a container
A connection can also be established between two containers. Suppose we have two containers, one is server and the other is client. After the server is started, the client starts to connect to the server. Some environment variable parameters used for the connection are provided on the client. We noticed that the IP address of the server is not fixed after each startup, but some server information will be used in the client, because the client is started after the server, therefore, the client can still identify some server information. Let's take a look at the basic steps:
1. Start a server
Docker run -- expose 80 -- name server <image> <cmd>
2 client connection to server
Docker run -- name client -- Link server: linked-server <image> <cmd>
-- Link server: linked-server, indicating to connect to the server container and connect the server information to the linked-server variable.
3. view some server information on the client
Env | grep-I linked-Server
You will see some information about the server.
Please pay attention to more highlights: http://bbs.superwu.cn
Follow the Superman college QR code: 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/6D/3E/wKiom1Ve26vQGNsyAADAdZasjL0406.jpg "Title =" 文..jpg "alt =" wkiom1ve26vqgnsyaadzasjl0406.jpg "/>
Docker port redirection details