MySQL Docker Image Example

This article will teach you how to use MySQL server using docker. We don’t have to install the MySQL server on our local machine if the docker is installed already.




Read More:

Download and Install Docker


First, we need to download docker desktop and install it on our system. Docker Desktop is available for all platforms. Go ahead, download and install docker desktop on your system.

Once you have installed it, you can verify whether docker is installed correctly or not by executing the following command,

$ docker -v

This would display the docker version if you installed it correctly.

$ docker version 20.10.14, build a224086

Next, we need to start the docker daemon on your system.

$ sudo systemctl start docker

Alternatively, you can start docker by clicking on the docker icon. This will take a few seconds to start the docker daemon.  Once it is started, it will show the message Docker desktop is running.

Development Steps


Below are the development steps to use the MySQL server using docker

  • Pull the MySQL docker image
  • Start the docker container
  • Access the container using Shell
  • Login to the MySQL database

Pull the MySQL docker image


Docker hub is similar to Github where you can find the dockerized images, we can pull the images and run them on the container. Let’s pull the MySQL docker image by running the below command –

$ docker pull mysql:5.7

Once the image is pulled, you can verify it by running the below command –

$ docker images

Screenshot-2022-05-25-at-4-36-23-PM

Start the docker container


Next, we need to start the docker container to run the MySQL image. We will use the docker run command to start the container

$ docker run -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.7

Screenshot-2022-05-25-at-4-37-42-PM

We need to pass a few options to the command,  -e option used to provide the environment variables. We will set the password for the root user using the MYSQL_ROOT_PASSWORD variable, -d option used to run the container in detached mode.




Access the container using Shell


Once the container is started, we can use docker exec -it command, it allows us to run commands inside a Docker container. We use the bash option to access the container using the bash shell.

$ docker exec -it 62a shell

Screenshot-2022-05-25-at-4-38-16-PM

Login to the MySQL database


Now we can access the MySQL server by running the below command –

$ mysql -uroot -p12345

Screenshot-2022-05-25-at-4-38-50-PM

We need to use -u option to provide the username, by default it will be root and  -p option takes the password, we need to provide the password that we used to set while running the MySQL container.

Screenshot-2022-05-25-at-4-39-28-PM

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply