Docker Community Forums

Share and learn in the Docker community.

MySQL docker -- connection refused and port is closed

(Rudolfl) #1

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?


(David Maze) #2

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

(Rudolfl) #3


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


(Martin Terp) #4

can you try and provide your full Dockerfile?

(Rudolfl) #5


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”