Error response from daemon: ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use

Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

That was the nice surprise I got when I switched back to a project and wanted to start some docker containers using "docker-compose up -d"

The context:

  • Mac os
  • Laravel app built with Laradock including a Nginx docker image

How to fix

First check what is using the port 80 by running

sudo lsof -i:80

In my case I got:

COMMAND   PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME nginx   25348 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25349 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25350 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25352 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25353 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25354 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25355 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25356 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25357 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25358 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25359 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN] nginx   25360 gilbert_tchoi    6u  IPv4 0xXXXXXXXXXXXXXXXX      0t0  TCP localhost:http [LISTEN]

If you don't care, the brutal approach is taking all your PID and running them through

kill -9 myPID

Unfortunately, the list of Nginx came back in my case with other PID.

Random idea, let's check brew service by running brew services list

nginx          started root          /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Culprit found for the unkillable Nginx. To clean up this nginx service that was probably a leftover from another project, just run

brew services stop nginx

Now I can start my docker containers and get back to my work.

How can I start docker on Ubuntu 20.04, if it complains that address is already in use? I tried

docker-compose up -d Starting relaatiotietokannat_db_1 ... Starting relaatiotietokannat_db_1 ... error ERROR: for relaatiotietokannat_db_1 Cannot start service db: driver failed programming external connectivity on endpoint relaatiotietokannat_db_1 [02444b3d0c2841a4fe702cf2705fbd5a869e44e7f258ecfbcea764ead989fcb4]: Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint relaatiotietokannat_db_1 [02444b3d0c2841a4fe702cf2705fbd5a869e44e7f258ecfbcea764ead989fcb4]: Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use ERROR: Encountered errors while bringing up the project.

October 8, 2020

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, its 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.

  • 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”: 

Option 2

  • 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.

Option 3

  • 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

Option 4

  • If nginx is running globally this could be the reason too. Run:

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!

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.

Video liên quan

Chủ Đề