Basic Network Configuration

Although Docker can “open more” containers according to the image, and each container does not affect each other, it does not mean that containers are completely broken between containers. Docker provides network interconnection functions of mapping container ports to host hosts and container ports to other containers when running images, so that containers can communicate with host hosts and between containers.

Access the container application externally

If no parameter is specified when the container is started, network applications and services in the container cannot be accessed from outside the container. When the container is running some network applications that need to be accessed from outside, you can specify the port mapping using the -p or -p parameter. When the -p flag is used, Docker will map a port from 49,000 to 49900 randomly to the open network port inside the container:

docker run -d -p [mirror ID or TAG]
Copy the code

Use -p (lowercase) to specify the port to be mapped, and only one container can be bound to a specified port. Support the format of the IP: hostPort: containerPort | IP: : containerPort | hostPort: containerPort.

Map all interface addresses

Use hostPort:containerPort to map the local port 5000 to the containerPort 5000:

docker run -d -p 5000:5000 training/webapp python app.py
Copy the code

By default, all addresses on all local interfaces are bound. Multiple ports can be bound with the -p flag multiple times:

docker run -d -p 5000:5000 -p 3000:80 training/webapp python app.py
Copy the code

Maps to the specified port at the specified address

Can use IP: hostPort: containerPort format specified mapping using a specific address, such as 127.0.0.1 localhost address:

docker run -d-p 127.0.0.1:55:5000 training/ webApp Python app.pyCopy the code

It can also be the IP address of another internal container.

Maps to any port at the specified address

If you use IP ::containerPort to bind any port of localhost to port 5000, the host will automatically assign a port:

docker run -d-p 127.0.0.1::5000 training/ webApp Python app.pyCopy the code

Udp flags can also be used to specify udp ports:

docker run -d-p 127.0.0.1:500:5000 /udp training/ webApp Python app.pyCopy the code

View the port mapping configuration

Use docker port to view the current mapped port configuration, and also view the bound address:

docker port nostalgic_morse 5000
Copy the code

The container has its own internal network and IP address (use Docker inspect+ container ID to get all variable values).

Container interconnection Enables communication between containers

The container connection system is another way to interact with applications in the container in addition to port mapping. It creates a tunnel between the source and the receiving container, which can see the information specified by the source container.

Custom container naming

The connection system is performed by the name of the container. So first you need to customize a memorable container name. Although the system assigns a name by default when creating a container, custom named containers have two benefits:

  • Custom names are easier to remember
  • This can be a useful parameter point when connecting to other containers, such as a Web container to a DB container.

Use the –name tag to give the container a custom name:

docker run -d -p --name web training/webapp python app.py
Copy the code

Docker ps can be used to view the name, or Docker inspect can be used to view the name of the container:

docker inspect -f "{{name}}" [mirror ID]
Copy the code

The container name is unique. If you have already named a container named Web, you must use docker rm command to delete the container before creating a new container with the name web.

The container of interconnected

Using the –link argument allows containers to safely interact with each other.

The –link argument format is –link name:alias, where name is the name of the container to link to and alias is the alias of the connection.

Let’s create a new database container:

docker run -d --name db training/postgres
Copy the code

Then create a Web container and connect it to the DB container:

docker run -d -p --name web --link db:db training/webapp python app.py
Copy the code

At this point the DB container and the Web container can communicate with each other. You can use Docker PS to see the container’s connections.

Using the –link parameter allows Docker containers to communicate with each other through a secure tunnel, rather than through an open port that is exposed to the external network.

View connection information for the exposed container

  • Environment variables: Use the env command to view the container’s environment variables
docker run --name web --link db:db training/webapp env
Copy the code
  • /etc/hosts file: When the link parameter is used, Docker adds host information to the /etc/hosts file of the parent container. The following is the hosts file for the parent container Web
docker run -t -i --link db:db training/webapp /bin/bash
root@aed84ee21bd3:/opt/webapp# cat /etc/hosts127.17.0.7 aed84ee21bde... 172.17.0.5 dbCopy the code

The first is the host information for the Web container, which defaults to its own ID. The second is the IP and host name of the DB container.


Read more

Docker series:

  1. Docker image, container common command
  2. Docker repository common command
  3. Common network configuration commands for Docker containers