With the popularity and development of container technology, Docker has become a leader in containerization technology and an indispensable tool for many developers and operation and maintenance personnel. In practical applications, we often need to expose the services in Docker to the external network for access. In this case, we need to use Docker's network configuration and port mapping.
1. Docker network configuration
There are four types of Docker networks: bridge network, host network, none network and custom network. When using Docker for service development and deployment, we usually use custom network types, which provide better control over communication between containers.
Docker’s custom network configuration usually includes network name, network driver, subnet IP and gateway, etc. Among them, network drivers include bridge, overlay, macvlan and other types to choose from. If multiple containers need to access each other, they must be in the same custom network so that the containers can communicate with each other through container names or container IDs.
Next, we will introduce how to create a custom network in Docker and how to add the service container to the specified network.
We can create a custom network by specifying the network name when creating the container through the Docker command line tool. The specific command is:
docker network create --driver bridge <network_name>
where <network_name>
is the name of the network to be created.
If you need to add the container to the specified network, you can use the --network option of the docker run command. The specific command is:
docker run --name <container_name> --network <network_name> <image_name>
Among them, <container_name>
is the container name, <network_name>
is the name of the network to be added, < image_name>
is the name of the container image.
2. Docker port mapping
The services in the Docker container can only be accessed inside the container by default. If you need to allow the external network to access the service through the IP address and port number, you need to do this. Port Mapping. There are usually two ways of port mapping for Docker: static port mapping and dynamic port mapping.
Static port mapping is usually set when starting the container. We can bind them together using the
docker run -p <host_port>:<container_port> <image_name>
For example, map the 8080 port of the host to the 80 port in the container:
docker run -p 8080:80 nginx
In this case, we can access http: //localhost:8080
to access services in the container.
Dynamic port mapping is usually performed by automatically allocating an unoccupied port on the host. We can use the -P or -p
docker run -P <image_name>
or:
docker run -p <container_port> <image_name>
For example, use the -P option to start a Tomcat container, and Docker will automatically allocate an unoccupied port on the host for mapping:
docker run -d -P tomcat
In this case, we can use the docker ps -a
command to view the mapped port number, and then access the service through the browser by accessing the host IP address and the assigned port number.
3. Conclusion
Through network configuration and port mapping, we can expose the services in the Docker container to the external network for access. It should be noted that security must be paid attention to when performing Docker network configuration and port mapping to avoid exposing services in the container to the external network environment, thereby avoiding security risks.
The above is the detailed content of How to access services in docker from the external network. For more information, please follow other related articles on the PHP Chinese website!