Home > Article > Operation and Maintenance > What to do if linux docker port mapping cannot be accessed
In recent years, Docker technology has developed rapidly in the field of cloud computing and has been widely used. Docker container technology not only helps developers deploy applications conveniently, but also quickly builds a repeatable deployment environment. However, when using Docker, you sometimes encounter some problems, such as Docker port mapping being inaccessible. This article will introduce the causes and solutions to this situation, hoping to help developers using Docker.
1. What is Docker port mapping
In a Docker container, in order to facilitate external access, it is often necessary to expose the service port inside the container to the host. At this time, you need to use the Docker port mapping function.
Docker port mapping can map a port in the container to a port on the host. For example, map port 80 in the container to port 8080 of the host, so that when accessing port 8080 on the host, you can access the port 80 service in the container.
2. Why Docker port mapping cannot be accessed
When using Docker, sometimes we will find that the services inside the container cannot be accessed. In this case, we need to check whether the Docker port mapping is set correctly. If Docker port mapping is not set correctly, it will result in inaccessibility.
Common reasons why Docker port mapping cannot be accessed include the following:
In the Docker container Internally, developers usually specify the address where the service listens. The default is 127.0.0.1, which means it can only be accessed locally. If the service listening address is not changed, even if the port is mapped on the host, the service in the container cannot be accessed.
If the host's firewall blocks the port mapped by Docker, then the Docker container service cannot be accessed from the host.
The service running in the Docker container may be running on a non-standard port. In this case, you need to Map during port mapping. If the mapped port is inconsistent with the service port running in the container, the service cannot be accessed.
If the Docker container is not running correctly, the mapped port cannot be accessed. You can troubleshoot the problem by viewing the Docker container running log.
3. How to solve the problem of inaccessible Docker port mapping
For the above reasons why Docker port mapping is inaccessible, the following solutions can be adopted:
Inside the Docker container, when monitoring the service port, you can use the 0.0.0.0 address, which means that you can accept requests from any IP address.
For example, in Node.js, the code to change the listening address from 127.0.0.1 to 0.0.0.0 is as follows:
app.listen(3000, '0.0.0.0', function() { console.log('Server listening on port 3000'); });
In the Linux system, you can open the port through the following command:
iptables -I INPUT -p tcp --dport 映射的端口号 -j ACCEPT
For example, the command to open access permission for port 8080 is as follows:
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
First, you can use the docker ps
command to check the running status of the container to ensure that the service is running. Secondly, when performing port mapping, you can check whether the service running port is the port to be mapped, for example:
docker run -p 8080:80 nginx
When running this command, you need to ensure that the Nginx service runs on port 80 by default.
You can use the docker logs
command to view the log information of the Docker container running process, and troubleshoot by viewing the log information Container running issues.
The above are some methods and tips to solve the problem of inaccessible Docker port mapping. I hope it will be helpful to everyone.
The above is the detailed content of What to do if linux docker port mapping cannot be accessed. For more information, please follow other related articles on the PHP Chinese website!