使用docker报Error response from daemon: Cannot start container container-name: failed to create

Yesterday, while using Docker for containerized application development, I encountered a port conflict problem. When we try to start a new container, we may receive the following error message: Error response from daemon: Cannot start container container-name: failed to create endpoint endpoint-name on network network-name: Bind for 0.0.0.0:port failed: port is already allocated. This technical sharing will detail how to solve this problem.

problem analysis

In Docker, each container has an independent network namespace, and the ports inside the container and the host are isolated from each other. However, the exposed port must be bound to the host port so that the application running in the container can be accessed from the host. Port conflicts occur when we try to bind a container's port to an already occupied port on the host.

Solution

1. Find the process occupying the conflicting port

First find the process that is using the conflicting port and stop it or modify the port. In Linux systems, you can use the following command to find port occupancy:

sudo lsof -i :port

Replace in the command portwith the conflicting port number. After the command is executed, the process information that is using the port will be displayed. Based on the process information, determine which application or service is occupying the port.

2. Stop occupying the process or modify the port

If the conflicting port is occupied by your own application or service, you can try to stop the process or modify the port it listens on. For specific operation methods, please refer to the relevant documentation of the application or service.

If the conflicting port is occupied by other system processes, it needs to be handled with caution. Before stopping a system process, make sure you understand the function and impact of the process to avoid system failure or data loss.

3. Modify Docker container port mapping

If the port conflict is because the specified host port is already occupied when starting the Docker container, you can try to modify the container port mapping and map the container port to an unoccupied host port.

In the Docker run command, -pthe mapping relationship between the container port and the host port is specified through parameters. For example:

docker run -p host-port:container-port image-name

Replace " in" in the command host-portwith an unoccupied host port number, container-portwhich is the port number that the application inside the container listens to.

4. Check network configuration

If the above method still cannot solve the port conflict problem, we can also check the Docker network configuration to ensure that the network settings are correct.

Docker network settings can be viewed using the following command:

docker network inspect network-name

Replace in the command network-namewith the network name. After the command is executed, detailed network configuration information will be displayed. Check that settings such as gateway, subnet, and IP address are correct, and ensure that the container's network configuration is as expected.

5. Restart the Docker service

If none of the above methods work, you can try restarting the Docker service. In most cases, restarting the Docker service can help solve some network-related issues.

sudo service docker restart

Guess you like

Origin blog.csdn.net/javamendou/article/details/131726685