It’s a sunny day and you are starting your docker container when you get this message: “Cannot start container … bind: address already in use” … hmm, now what? Sometimes, when trying to start your docker instance, it’s possible to be stunted by another process using the same port. However, there are a few options you have in this situation. The ones I have tried and use when needed are listed here:
To quickly move forward with successfully starting your container, you could just kill whatever is using the port. I usually do that but first check what is using the port, and, if it is non-essential at this time, kill it.
In console type:
sudo lsof -i tcp:8080
You’ll see a prompt for your device password. Type it in and press enter.
Replace 8080 with whichever port you want. A list of currently running processes will be displayed for you (f.1).It will contain information about each process’s PID, which you will need next. Look at the PID of the process (highlighted in red). You need it to issue a kill command next.
Find the process that is obstructing your desired port, and ensure it is not something you need. If it is an essential process you will do better to use another option listed below.
In console, type this to stop the process(replace “PID” with the process ID you want to kill). This will stop the process and you can check that the process has stopped by running the command from step “a”:
sudo kill -9 PID
Change to another port by modifying the default port in your docker-compose.yml file. If the process using your desired port is, in fact, essential for your setup, you can always just change the port for your docker instance. In the example below, it was port 8080 that was being used. You can change it to another value like 8086, for example. Just remember to also adjust any other project settings linking to this particular port to the new value you select.
Restart the container instance after doing this for changes to take effect.
This issue can happen when for any reason your host reboots. In this instance, try restarting your apache server. Stopping apache2 service in the host can solve it.
sudo /etc/init.d/apache2 restart
sudo apachectl -k restart
If nginx is running globally this could be the reason too. Run:
sudo nginx -s stop
When in Doubt
Always refer to the official Docker documentation. The Docker team have created additional resources, both free and paid, to supplement their product, and additional community support. Community support is fantastic! It allows developers from all across the world to collaborate and connect.
If you are new to Docker, have a look at their 101 tutorial here, and their learning programs here.
Best of luck, and happy coding!