docker容器间如何通信
Docker containers can communicate with each other using various methods, such as linking, port mapping, and container networking.
Linking
Linking is the most basic method of communication between Docker containers, and is also the simplest to set up. When two containers are linked, they can communicate with each other using environmental variables.
To link two containers, we use the --link option when starting a container. For example, if we have a container running a MySQL database, we can link it to another container like so:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
docker run --name some-app --link some-mysql:mysql -d application-image
Some things to note here:
The first container is started with the --name some-mysql option. This gives the container a name that we can use to reference it later.
The second container is started with the --link some-mysql:mysql option. This links the two containers and creates an environmental variable in the second container called MYSQL_PORT that points to the first container.
The -e MYSQL_ROOT_PASSWORD=my-secret-pw option sets an environmental variable in the first container with the root password for the MySQL database. This is necessary so that the second container can connect to the database.
The -d option runs the containers in the background.
The mysql:tag image specifies which MySQL image to use.
The application-image image specifies which application image to use.
Once the containers are running, we can connect to the MySQL database from the second container using the following command:
mysql -u root -p -h mysql
This will connect to the MySQL database running in the first container as the root user. The -h mysql option tells the mysql command to connect to the hostname mysql, which is the name of the first container.
Port Mapping
Port mapping is another way to communicate between Docker containers. With port mapping, we can map a port on the host machine to a port in a Docker container. This allows us to access services running in a container from the host machine.
For example, if we have a container running a web server on port 80, we can map port 8080 on the host machine to port 80 in the container like so:
docker run -p 8080:80 --name some-nginx -d nginx
This will start a container running NGINX on port 80 and map port 8080 on the host machine to port 80 in the container. We can then access the web server from the host machine by going to http://localhost:8080.
Container Networking
Container networking is a relatively new feature in Docker that allows containers on the same host to communicate with each other without the need to expose ports to the host machine.
Container networking is implemented using a virtual network that is created when the Docker daemon starts. By default, all containers on the same host are attached to this virtual network.
We can use the docker network command to list the available networks:
docker network ls
This will list all of the available networks, including the default network.
We can use the docker network inspect command to inspect a particular network:
docker network inspect bridge
This will print out a lot of information about the bridge network.
If we want to create our own network, we can use the docker network create command:
docker network create my-network
This will create a new network called my-network.
We can then start containers and attach them to this network using the --net option:
docker run --net my-network --name some-nginx -d nginx
This will start a container running NGINX and attach it to the my-network network.
We can use the docker network connect and docker network disconnect commands to connect and disconnect containers from networks.
Conclusion
In this article, we've looked at three different ways to communicate between Docker containers: linking, port mapping, and container networking. Each of these methods has its own advantages and disadvantages, so it's important to choose the right one for your particular use case.
相关文章