MySQL docker -- connection refused and port is closed

Hi all,

I am trying to get MySQL running in a container. Once it is running, I can not connect to MySql. Connections are refused.

FROM mysql:5.7.16


docker build -t yktoo-db-image -f “dockerfile” “MYPATH”

I then start docker by:
docker run -d -e MYSQL_ROOT_PASSWORD=root --name yktoo-db yktoo-db-image

Docker is running, but all connections to database is refused (i connect to host yktoo-db, not to localhost)
What I found:

  1. Port 3306 on yktoo-db is closed and this is, I believe, root of the problem. Verified by using nmap from host.
  2. I connected terminal to the container and verified ‘mysqld’ is running. I also tried to use local mysql commands to make sure it is actually operational.

I did try to add “EXPOSE 3306” into dockerfile, rebuild and have same result.

Any ideas?


Without a -p option, your container will be inaccessible except from other containers on the same Docker-internal network.

1 Like


-p does not seem to make much difference. I tried to add “-p 3306:3306”, but port 3306 is still closed in container


can you try and provide your full Dockerfile?


Here is a complete Docketfile:


Use MySQL 5.7 for the base image

FROM mysql:5.7.16
EXPOSE 3306 33060

Copy database initialisation scripts

COPY init.sql /docker-entrypoint-initdb.d/
COPY database.sql /db/

I guess scripts copied via COPY are not relevant to the problem.

I run container with:

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name yktoo-db yktoo-db-image

use or omition of -p option does not make any difference.


See the problem

EXPOSE 3306 33060

You have an extra “0”

I am having similar issue today when I tried.
My docker run command:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=testing -d mysql:latest -p 9999:3306

I tried with port 9999, not working