Here’s a cheat sheet to run MySQL in a Docker container in your Windows or Mac laptop in a few minutes. In this brief how-to you will:
- Install Docker Desktop in your laptop
- Download official Oracle MySQL image or Docker Inc. image
- Start the container
- Administer it
- Start a MySQL session and start a Linux session
Download and install Docker
You can download Docker Desktop for Windows or Mac from https://docs.docker.com/desktop/. Install like any other application and you’re ready to move to the next step. As you can read from the docs, Docker Desktop will install several things like Docker Compose or Kubernetes. We’ll use both in advanced examples in future posts.
Pull MySQL Server image
Now you can pull the MySQL Server image. You have two options. You can download the image maintained by Oracle MySQL (find the description here https://dev.mysql.com/doc/refman/8.0/en/linux-installation-docker.html)
docker pull mysql/mysql-server:latest
Or you can download the image maintained by Docker Inc. You can find a description together with examples here: https://hub.docker.com/_/mysql
docker pull mysql
The differences? Oracle MySQL image runs over Oracle Linux, whereas Docker Inc. image runs over Debian. And if you have a MySQL subscription, you will get Support for the Oracle MySQL Docker image if in trouble.
List Docker images
You can list the brand new MySQL image here. Pay attention to the IMAGE ID field, you will need it (I have downloaded both images, Oracle MySQL and Docker Inc. in the following output).
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 5c62e459e087 6 days ago 556MB
mysql/mysql-server latest 1504607f1ce7 6 weeks ago 391MB
Start a MySQL image in a Docker container
You can start the container, and choose a:
- A name for the container (
mysql_cnt
in the example) - The root password
- The image you want to launch
docker run --name=mysql_cnt -e MYSQL_ROOT_PASSWORD="Password1*" --restart on-failure -d 5c62e459e087
Find additional information to configure the instance in the corresponding image documentation, shared before.
Verify that the container has started normally
Make sure the container is up.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30f6e1462b9e 5c62e459e087 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp mysql_cnt
Troubleshooting
If the container has not started, you can check logs. In this case, all is working.
docker logs mysql_cnt
[...]
2021-06-28T13:46:43.873232Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Start and stop the container
Stop your container:
docker stop mysql_cnt
mysql_cnt
Verify it’s stopped:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30f6e1462b9e 5c62e459e087 "docker-entrypoint.s…" 5 minutes ago Exited (0) 1 second ago mysql_cnt
And restart it:
docker start mysql_cnt
mysql_cnt
Remove the container
As easy as stopping it and running:
docker rm mysql_cnt
mysql_cnt
Get a mysql command line client session
In order to authenticate, you need to have the mysql command line client installed, or even better, MySQL Shell:
docker exec -it mysql_cnt mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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>
Create a bash session
In case you would like to install things or edit the my.cnf
:
docker exec -it mysql_cnt bash
root@10b5e2727ef3:/#
Note that if you installed a Debian based release, you will be able to install stuff like:
apt-get update
apt-get install vim
If you installed the Oracle MySQL image, it comes with Oracle Linux installed. Then use:
microdnf update
microdnf install vim
You are now able to setup Docker in your laptop, download your favorite MySQL image, start it in a container and stop it and also capable of installing packages in the Linux distribution. You can configure and optimize MySQL as usual, using SET PERSIST
(recommended over editing the configuration file manually, when possible).
If you are curious and would like to explore running MySQL containerized services, you may want to read the official documentation. You will find several hints to use networking or taking backups.
The post MySQL in a Docker Container cheat sheet appeared first on mortensi.
Planet MySQL