Detailed explanation of common commands for network configuration of containers in Docker

Detailed explanation of common commands for network configuration of containers in Docker

Basic network configuration

Although Docker can "open multiple" containers based on images, and each container does not affect each other, it does not mean that the containers are completely separated from each other. When running an image, Docker provides a network interconnection function that maps container ports to the host host and container ports to another container, allowing containers to communicate with the host host and with each other.

### Accessing container applications from outside

When starting a container, if you do not specify the corresponding parameters, network applications and services in the container cannot be accessed through the network outside the container. When running some network applications in the container that need to be accessed externally, you can specify port mapping using the -P or -p parameter. When using the -P flag, Docker will randomly map a port between 49000 and 49900 to an open network port inside the container:

docker run -d -p [mirror ID or TAG]

Use -p (lowercase) to specify the port to be mapped, and only one container can be bound to a specified port. The supported formats are ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort.

Map all interface addresses

Use hostPort:containerPort to map the local port 5000 to the container's port 5000:

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

By default, all addresses on all local interfaces are bound. To bind multiple ports, use the -p flag multiple times:

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

Maps to the specified port of the specified address

You can use the ip:hostPort:containerPort format to specify a mapping using a specific address, such as the localhost address 127.0.0.1:

docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

It can also be the IP address of other internal containers.

Map to any port of the specified address

Use ip::containerPort to bind any port of localhost to port 5000 of the container. The local host will automatically assign a port:

docker run -d -p 127.0.0.1::5000 training/webapp python app.py

You can also use the udp flag to specify the UDP port:

docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

View the mapped port configuration

Use docker port to view the currently mapped port configuration and the bound address:

docker port nostalgic_morse 5000

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's 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 receiving containers, and the receiving container can see the information specified by the source container.

Custom container naming

The linking system is implemented based on the container name. Therefore, you first need to customize a memorable container name.

Although the system assigns a name by default when you create a container, custom naming of containers has two benefits:

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

Use the --name flag to give the container a custom name:

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

Use docker ps to view the name, or use docker inspect to view the name of the container:

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

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

Container interconnection

Use the --link parameter to allow containers to interact safely with each other.

The --link parameter format is --link name:alias, where name is the name of the container to be linked and alias is the alias of this connection.

For example, let's create a new database container:

docker run -d --name db training/postgres

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

At this time, the db container and the web container can communicate with each other. You can use docker ps to view the container's connections.

Using the --link parameter allows Docker to communicate between two containers through a secure tunnel without opening ports, thus avoiding exposing the ports to the external network.

View connection information of public containers

Environment variables: Use the env command to view the environment variables of the container

docker run --name web --link db:db training/webapp env

/etc/hosts file: When using the link parameter, Docker adds the host information to the /etc/hosts file of the parent container. Below is the hosts file of the parent container web

docker run -t -i --link db:db training/webapp /bin/bash
root@aed84ee21bd3:/opt/webapp# cat /etc/hosts
127.17.0.7 aed84ee21bde
...
172.17.0.5db

The first is the host information of the web container, which uses its own id as the host name by default. The second is the ip and hostname of the db container.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of common commands in Docker repository
  • Summary of common commands in Dockerfile
  • Docker common commands summary (practical version)
  • Summary of Docker's commonly used commands for clearing container images
  • Detailed explanation of common Docker commands

<<:  How to deal with garbled characters in Mysql database

>>:  Detailed explanation of MySQL transaction processing usage and example code

Recommend

Docker installation and configuration steps for RabbitMQ

Table of contents Single-machine deployment Onlin...

Let’s talk about the symbol data type in ES6 in detail

Table of contents Symbol Data Type The reason why...

Detailed explanation of putting common nginx commands into shell scripts

1. Create a folder to store nginx shell scripts /...

How to set and get the number of Mysql connections

Get the number of connections --- Get the maximum...

Detailed tutorial for installing mysql5.7.18 on centos7.3

1 Check the Linux distribution version [root@type...

Analysis of the usage of Xmeter API interface testing tool

XMeter API provides a one-stop online interface t...

How to make vue long list load quickly

Table of contents background Main content 1. Comp...

Several common CSS layouts (summary)

Summary This article will introduce the following...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

JavaScript closure details

Table of contents 1. What is a closure? 2. The ro...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

javascript implements web version of pinball game

The web pinball game implemented using javeScript...

Detailed explanation of the pitfalls of mixing MySQL order by and limit

In MySQL, we often use order by for sorting and l...