March 23, 2024

Running MySQL in a Docker Container


Getting Started

This is a going to be a short and speedy tutorial on using MySQL inside a docker container. This also will help my brother get MySQL working on his laptop as the official documentation was a little much to start with.

Install Docker

Pretty simple explanation, but you need to install Docker for this to work. You can use this link if you are using macOS.

Pull MySQL Docker Image

docker pull mysql
bash

By running this command, you will see some loading bars which is downloading the official MySQL Docker Image which contains all the necessary dependencies and software to spin up an instance of MySQL.

Start/Run the Container

docker run --name test-mysql-container -e MYSQL_ROOT_PASSWORD=password -d -p 3306:3306 mysql
shell

By running this command, you will effectively be starting a Docker container (an instantiation of an Image). To break down what this command is doing, you are starting a mysql image, with parameters --name test-mysql-container, -e MYSQL_ROOT_PASSWORD=password, -d, and -p 3306:3306.

The --name is pretty obvious, it is just giving the container a custom name to help you keep track of it (in case you have other containers running).

The password is important as it is specifying the root MySQL password (you will use this later). In a local environment, this isn't as important so it's okay just using a basic password like password :).

-d means this is a detached container, effectively means that you don't have to keep the terminal running to keep the container running.

And finally, -p 3306:3306 is the port. This maps your host machine http://localhost:3306 to the 3306 port on the container, allowing you to connect to the container from your local computer.

Validate the Container is Running

docker ps
shell

should return a value like:


CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                               NAMES
40e7f3d06253   mysql     "docker-entrypoint.s…"   6 seconds ago   Up 5 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   test-mysql-container
text

This validates that the container started correctly.

Opening MySQL Container

Once the image is pulled and the container is started, you can open the container and get into the MySQL server. You can do this by:

docker exec -it test-mysql-container bash
shell

This will allow you to now interact with the container we have created.

Opening MySQL Terminal

Once you are operating inside the container, you can connect to the MySQL terminal:

mysql -u root -p
shell

This will prompt you for the password, you can use password like we specified above.

Once the password is entered, you should see the welcome message:

Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Complete šŸŽ‰

Now, using the terminal you have just opened. You can go ahead and create new databases, create tables, and query them!

Here is a basic example:

Create a database:

CREATE DATABASE test_db;
sql

Use the newly created database:

USE test_db;
sql

Create a new database table:

CREATE TABLE people (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
);
sql

Add data to the newly created table:

INSERT INTO people (name) VALUES ('Will Sather');
sql

Finally, query said data from the table.

SELECT * FROM people;
sql

And that it's it! You now have MySQL running inside of Docker, super easy setup and a reproducible way to practice your MySQL.