How to access containers in the external world? -Play Docker container technology every 5 minutes (37)
In the previous section, we learned how to access the external network of containers. Today we will discuss another direction: how to access the container from the external network?
The answer is:Port ing.
Docker maps the port that the container provides external services to a port of the host. The Internet accesses the container through this port. When the container is started, use the-p parameter to map the Port:
After the container is started, you can view the host ing port through docker ps or docker port. In the preceding example, port 80 of the httpd container is mapped to host 32773, so that you can access the web Service of the container through
In addition to ing dynamic ports, you can also specify a specific port mapped to the host in-p. For example, you can map port 80 to port 8080 of the host:
For each mapped port, the host starts a docker-proxy process to process the traffic to the container:
Take 0.0.0.0: 32773-> 80/tcp as an example to analyze the entire process:
Docker-proxy listens to port 32773 of the host.
When curl accesses 10.0.2.15: 32773, docker-proxy forwards it to container 172.17.0.2: 80.
The httpd container responds to the request and returns the result.
Summary of this ChapterIn this chapter, we first learned three types of Docker networks: none, host, and bridge, and discussed their different use cases. Then we practiced creating a custom network; finally, we discuss in detail how to implement communication between containers and external networks.
This chapter focuses on the container network in a single host. Cross-host network communication will be discussed in detail in later sections. In the next section, learn about Docker storage.