Connecting to MYSQL docker container

Creating a MYSQL server container in Docker

Step 1 : Pull MySql image from docker hub. Following command will pull the MYSQL version 5.6 image.

docker pull mysql:5.6

Step 2: Run a container from this image. ‘-name’ gives a name to the container. ‘ -e’ specifies run time variables you need to set. Set the runtime variables for the MySQL container-

MYSQL_ROOT_PASSWORD=root

MYSQL_DATABASE=testdb

MYSQL_USER=test

MYSQL_PASSWORD=test

These options are self explanatory. ‘-d’ tells the docker to run the container in background.

docker run --name=mysql-dev-server -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=test -e MYSQL_PASSWORD=test -e MYSQL_DATABASE=testdb -d mysql

This will output a container id as shown below, which means that the container is started in the background properly.

Step 3: Then check the status of the container by issuing, ‘docker ps -a’ command

docker ps -a

Now you should be able to see that MySQL is running on port 3306.

Step 4: Find the IP of the container using following. Check out the “IPAddress” from the output, this will tell you the ip address.

docker inspect mysql-dev-server

Now you should be able to connect to MySQL using this ip address on port 3306.

Step 5: You can check the logs of the running container use the following command

docker logs mysql-dev-server

Isn’t it easy? You have created a MYSQL database server with just couple of commands.

 

Accessing MYSQL database through WEB Interface (phpMyAdmin)

Step 1: In order to link the MYSQL container to phpMyAdmin , you need to fire below docker command –

docker pull phpmyadmin/phpmyadmin

docker run --name myphpadmin -d --link mysql-dev-server:db -p 8080:80 phpmyadmin/phpmyadmin

First command will pull the phpmyadmin image and second will create the phpmyadmin container named ‘myphpadmin’ and link it to ‘mysql-dev-server’ container which we created earlier –

Step 2: Access the MYSQL database from phpMyAdmin

Now you can connect to this MYSQL database docker container from phpmyadmin . This will show up below page once you hit the URL (http://192.168.99.100:8080)

Note : 192.168.99.100 is the default IP of docker machine . This can be seen on the docker Toolbox console once it loaded. In case of windows please refer below screenshot to get the docker machine’s default IP address –

Once you hit the URL http://192.168.99.100:8080 , you can see phpmyAdmin login screen . You need to enter database details like username and password in order and click go –

Now you can access the testdb database which you have created using MYSQL docker image.

1 thought on “Connecting to MYSQL docker container”

Leave a Comment

Bitnami