In this tutorial, you’ll learn how to connect to MySQL running in docker container from localhost. You’ll start by pulling in an image of MySQL from Docker hub.

From the command prompt, type in the following command to pull the MySQL docker image.

// Execute in terminal
docker pull mysql

Once you have the MySQL docker image, you need to run the image to create an instance of MySQL.

// Execute in terminal
docker run -p 3307:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

-p option is used to port the MySQL running in container at port 3306 to port 3307 in my local machine.

-d option is used to run the container in detach mode.

-e option is used to pass the MySQL password.

Connect To MySQL Running In Docker Container

Now the MySQL running in container is available in my localhost at 127.0.0.1:3307. You can use host as 127.0.0.1 and port as 3307 to login MySQL running in container.

// Login to MySQL from terminal
mysql --host=127.0.0.1 --port=3307 -u root

Wrapping It Up

In this tutorial, you’ll learnt how to connect to MySQL running in Docker container from localhost.

Do let us know thoughts or suggestions in the comments below.