Docker – How to Connect to a MySQL Running Container

1. What we want to achieve:

We used the official mysql image from Docker Hub (, and created a running database container.

Now we need to connect to the MySQL database using MySQL command line client, to do some database operation.


2. Solution1:

Bash into the running container and run MySQL client.

2.1 Bash into the running MySQL container:

$ docker exec -t -i <container_name> /bin/bash

2.2 Run MySQL client:

$ mysql -u “<useranme>” -p


3. Solution2:

Use MySQl Command Line Client.

Reference: MySQL Image Official Doc

In the official guide, it states:

The following command starts another mysql container instance and runs the mysql command line client against your original mysql container, allowing you to execute SQL statements against your database instance:

$ docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

The variables here are really confusing.  Here is a more readable version of this command:

$ docker run -it --link <container_name>:<image_name> --rm <image_name> sh -c 'exec mysql -h"<container_ip_address>" -P"<mysql_port_number>" -u"<mysql_username>" -p"<mysql_password>"'

Now, lets use an example to explain how to find all the variables.


3.1 Get container_name, image_name, mysql_port_number:

$ docker ps


We can get:

container_name: romuiweb_db_1

image_name: mysql

container_port_number: 3306


3.2 Get container_ip_address:

$ docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id>



3.3 Get mysql_username, mysql_password:

When we start the mysql image, we set environment variables to decide the username and password.

username is default to root.

password is set by MYSQL_ROOT_PASSWORD, and we can find it in docker-compose.yml file:


So we can get:

mysql_username: root,

mysql_password: password.


3.4 Create the real command:

Now we have all the values of the variables. We can construct the real command:

$ docker run -it –link romuiweb_db_1:mysql –rm mysql sh -c ‘exec mysql -h”″ -P”3306″ -u”root” -p”password”‘


3.5 Run the command, gain access to MySQL:



If it works, skip the following problem solving part.


3.6 Problem Solving:

(In this problem, we are not using romuiweb_db_1 we used earlier. We switch to docker_db_1. The Ip Address is different now)

docker: Error reponse from daemon: Cannot link to <container_name>, as it does not belong to the default work.

$ docker run -it –link docker_db_1:mysql –rm mysql sh -c ‘exec mysql -h”″ -P”3306″ -u”root” -p”password”‘


This means that we are not running the mysql connect container on the same network the mysql container is currently running on. We need to connect it to the same network use –net option. Reference:

Step1: Find out which network mysql container is running on:

$ docker inspect docker_db_1

Find the “Networks”:


As we can see from the screenshot above, the network docker_db_1 is running on is “docker_default”. We need to connect to this network. So we change out command to this:

$ docker run -it –net=docker_default –link docker_db_1:mysql –rm mysql sh -c ‘exec mysql -h”″ -P”3306″ -u”root” -p”password”‘

After adding the network argument, we successfully connected:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s