Home > Operation and Maintenance > Docker > What to do if linux docker port mapping cannot be accessed

What to do if linux docker port mapping cannot be accessed

PHPz
Release: 2023-04-25 09:26:53
Original
3885 people have browsed it

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:

  1. The service in the container is not listening to the 0.0.0.0 address

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.

  1. The host firewall blocks the port

If the host's firewall blocks the port mapped by Docker, then the Docker container service cannot be accessed from the host.

  1. The port on which the container is running is inconsistent with the mapped port

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.

  1. Docker container is not running correctly

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:

  1. Use 0.0 .0.0 address listening port

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');
});
Copy after login
  1. Check the host firewall configuration

In the Linux system, you can open the port through the following command:

iptables -I INPUT -p tcp --dport 映射的端口号 -j ACCEPT
Copy after login

For example, the command to open access permission for port 8080 is as follows:

sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
Copy after login
  1. Mapped port correctness issue

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
Copy after login

When running this command, you need to ensure that the Nginx service runs on port 80 by default.

  1. Check the running log of the Docker container

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template