Error starting userland proxy: Bind for 0.0.0.0:3306 failed: port is already allocated

Ran:

docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=eagle -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-server:5.7

Got:

2622be1263a3890a3f278028e7a494dd71f498b88baafac9b96e0202fc4686ff
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql (225cec8db389e7b636235016cf74e4111c9e16cefd62afdcd858da804f8e63d4): Error starting userland proxy: Bind for 0.0.0.0:3306 failed: port is already allocated.

It seems that thousands of people have had the same issue over the years, but nowhere have I found a solution that works to release the port that seems ‘stuck’ from a previous run.

Prior to the run command, I ran:

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2622be1263a3 mysql/mysql-server:5.7 “/entrypoint.sh mysq…” About a minute ago Created mysql

Then, I ran:

docker rm 2622be1263a3
2622be1263a3

Followed by:

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

A port scan shows port 3306 open from a previous docker run:

Open TCP Port: 3306 mysql

Please let me know if ANYONE has found a way to kill the process that is using that port.

lsof shows this:

sudo lsof -i TCP:3306
Password:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 41817 _mysql 19u IPv6 0x30a37353801560b5 0t0 TCP *:mysql (LISTEN)

Then this after “sudo kill -9 41817”:

sudo lsof -i TCP:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 45155 _mysql 19u IPv6 0x30a3735391aebf75 0t0 TCP *:mysql (LISTEN)

I just seems to create a new process for mysql with another PID ad infinitem.

Worth noting that Docker isn’t the only thing that can open listening ports, and if you have MySQL installed on your host, something like systemd could be restarting it and taking the port. ps on the host would help you find the parent process, and if it’s Docker-managed or coming from somewhere else.

Turns out that was exactly what was going on. I had installed mysql on my Mac host and when I uninstalled it from System Preferences, the problem with mysql in containers disappeared. I still don’t know though what I did to start mysql on the host with port 3306. To my knowledge, I never used the host application. Thank you for your response.