In this tutorial, you will learn how to start MySQL in Docker Container. Without further ado, let’s head straight to the topic!
Note that this tutorial assumes that Docker is already installed in your system.
To learn more about Docker, please check Docker Tutorials page.
Step 1: Pull MySQL Docker Image
The first step is to pull the MySQL Docker image. Here we are pulling the latest image. You can also specify a particular version.
docker pull mysql/mysql-server:latest

Step 2: Docker Run Command
Next, you will run the docker run command.
docker run -d -p 3306:3306 --name mysql-docker-container -e MYSQL_ROOT_PASSWORD=sergey -e MYSQL_DATABASE=photo_app -e MYSQL_USER=sergey -e MYSQL_PASSWORD=sergey mysql/mysql-server:latest
Now let’s deconstruct the command.
- docker run command first creates a writeable container layer over the specified image i.e. mysql/mysql-server:latest and then starts it using the specified command.
- -d prints the container ID and runs the container in the background.
- -p publishes a container’s port(s) to the host.
- –name sets the name of the container i.e. mysql-docker-container.
- -e sets environment variables. Here MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD have been set.
- With the environment variables, we have changed the root user password, named a database that will be created when the image starts up, created a user that will be granted superuser permissions for the database created, and set the password of that user.

Step 3: Docker exec Command
Next, you will run the docker exec command.
sudo docker exec -it mysql-docker-container bash
Now let’s deconstruct the command above.
- The docker exec command runs a new command in an already running container.
- This command creates a new interactive bash shell in the mysql-docker-container through -it that tells Docker to allot a pseudo-TTY connected to the container’s stdin.

In the bash shell, enter the following command to invoke MySQL with the root user.
mysql -u root -p
Next, you will enter the password which you had set when running the docker run command. In our case, the password of the root user is set to sergey.

Running SQL commands
Let’s have a look at the users of the system database i.e. mysql with the help of the following query.
SELECT user FROM mysql.user;
In the output, we can also see the user sergey created during the docker run command execution.

Now let’s have a look at the databases with the following query.
show databases;
Here we can see the photo_app database created while running the docker run command.

Creating a New User
Now let’s create a new user by running the following SQL statements.
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'helloworld'; GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost'; FLUSH PRIVILEGES;

Now if we again run the select command on the user table, we can see that new_user has also been added to the table of user.

Finally, let’s log in to MySQL with the new_user with the password helloworld.

Conclusion
With this, we have come to the end of our tutorial. In this tutorial, we learned how to start MySQL in Docker Container and also executed SQL commands inside MySQL Docker Container.
Stay tuned for some more informative tutorials coming ahead and feel free to leave any feedback in the comments section.