A Docker container is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, a runtime, system tools, libraries, and settings.
While working the Docker, you might face an error like the “port is already allocated”. Let’s see what this error is and how to fix it.
bind for 0.0.0.0:4222 failed: port is already allocated
The error bind for 0.0.0.0:4222 failed: port is already allocated occurs when port 4222 is already in use by another process and cannot be bound to by the current process.
This error arises if two services run on the same port, 4222.
How to fix bind for 0.0.0.0:4222 failed: port is already allocated
To fix the bind for 0.0.0.0:4222 failed: port is already allocated error, try stopping the other service or application that is using port 4222, or you can configure the current service to use a different port number.
If this error occurs while using Docker, ensure that the previous container you launched is killed before launching a new one that uses the same port.
To kill the docker container, use the below commands.
docker rm -f <container-name>
To check the current containers running, use the
docker container ls command.
You can also stop the container using the docker stop <container-id> command.
If you are still facing the same error, then the problem is that the docker proxy is still utilizing ports even though there are no containers currently running.
Use the following commands to resolve the issue.
sudo service docker stop sudo rm -f /var/lib/docker/network/files/local-kv.db
If you are using Ubuntu, prepend “sudo” on every command because it requires administrative permissions.
I hope this helps!