Original: http://blog.csdn.net/anxpp/article/details/52495295
We use Docker to publish the components, usually can not be separated from the network, the following describes the Docker network infrastructure configuration. 1. Port mapping accessing container applications from outside
When you start a container, you cannot access the inside of the container through the network outside the container if you do not know the corresponding parameter.
Port mappings can be specified by the-p or-p parameters.
When-P is used, Docker maps a 49000~49900 port randomly to the open port inside the container.
-P maps to the specified port, supported formats:
IP Address: Host Port: Container port IP Address:: Container port Host port: Container port
map All interface addresses
Host port: Container port Format the command to map a local port to a container port is as follows:
Docker run-d-P 3306:3306 MySQL
At this point, the default will help top the address on all local interfaces, you can use the-p to help most ports.
map to specified port for specified address
IP Address: Host port: The parameters of the container port format are almost the same as the above, but more restrictive addresses, such as the need to specify specific address access (such as 127.0.0.1):
Docker run-d-P 127.0.0.1:3306:3306 MySQL
any port mapped to the specified address
IP Address:: Container port format parameters, you can bind any port localhost to the container's specified port, the local host will automatically assign a port:
Doccker run-d-P 127.0.0.1::3306 MySQL
You can also use UDP to mark UDP ports:
Doccker run-d-P 127.0.0.1:3386:3386/udp a mirror
To view the port mapping configuration
To view the port mapping configuration using the Docker Port command:
Docker Port Container ID 3306
The container has its own internal contacts and IP addresses:
Docker Inspect container ID
Then a lot of information is output.
2. The connection system of the container Interconnect container is another way that can interact with the application in the container in addition to the port mapping.
It creates a tunnel between the source and the receiving container, and the receiving container can see the information specified by the source container.
Custom container name The connection system executes according to the name of the container, so you need to customize a simpler and easier to distinguish container name.
Custom naming uses the--name parameter, which has been encountered in previous articles:
Docker run-d-P--name db MySQL
The name of the container must be unique.
When executing Docker run, if the--RM parameter is added, the container is deleted immediately after termination regret, but it cannot be used with the-D parameter.
Container Interconnect Use the--link parameter to allow a secure connection between containers to interact.
Here is an example:
First create a container for the database:
Docker run-d--name DB training/postgres
Then create the Web container:
Docker run-d-P--name web--link db:db training/webapp python app.py
At this point the DB container and the Web container add you to the interconnected relationship.
--link parameter:--link Name:alias,name is the name of the container to which you want to connect, and the alias is the nickname for this connection.
You can use Docker PS to view container information, where the names column shows the state of the interconnect.
Docker establishes a secure tunnel between two interconnected containers, and does not need their ports to be mapped to host hosts or to avoid exposure of ports.
Docker exposes connection information in two ways:
environment variable Update/etc/hosts file
Use the ENV command to view the environment variables for the Just Web container:
Docker run--rm--name web2--link db:db env
...
The environment variables at the beginning of the db_ are used by the Web container to connect to the DB container, prefixed by an uppercase connection alias.
In addition to environment variables, Docker also adds host information to the parent container's/etc/hosts file. Where the Web container takes its own ID as the default hostname and the DB container as the host name in DB.
For example, multiple web to DB containers, you can link multiple child containers to the parent container.
Summary Docker provides a graceful network configuration function through port mapping and container interconnection.