In this guide, we want to teach you to Install MySQL server in a Docker Container on Debian 12 Bookworm. By running MySQL in a Docker container you will get a lot of benefits such as improving your development speed and easy maintenance. Here you can follow the steps below to install Docker on your Debian 12 create a Docker Container for MySQL and run it.
Steps To Install MySQL in Docker Container on Debian 12
Before you start to set up a Docker MySQL container, you need some requirements. Let’s see what we need.
First, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow this guide on Initial Server Setup with Debian 12 Bookworm.
Then, you must have Docker running on your server. For this purpose, you can visit this guide on Install Docker CE on Debian 12 Bookworm.
Now follow the steps below to complete this guide.
Step 1 – Pull MySQL Docker Image on Debian 12
At this point, you can easily pull the MySQL image from Docker Hub on your server. The official image that is available for MySQL in Docker Hub is mysql. To download the MySQL image on Debian 12, run the command below:
docker pull mysql
When your download is completed, you will get the following output:
Output Using default tag: latest latest: Pulling from library/mysql bc377bce3181: Pull complete 80bab949ab51: Pull complete 73682200afb7: Pull complete d1c32d486523: Pull complete 54341582c90c: Pull complete 7490cd8f4d9b: Pull complete de967683cb3b: Pull complete 39564f901a1e: Pull complete c95e6efa291a: Pull complete 8366d05afd7c: Pull complete Digest: sha256:85ab57eb4a48ada2a341dcf7d96733ce2f370fffb8e8e216991b106e50fa6434 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
You can verify your MySQL image download is successfully completed with the command below:
Output REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 8da80fe49fcf 34 hours ago 577MB
Step 2 – Start and Run MySQL COntainer on Debian 12
At this point, you must create a container for your MySQL image and start your container on Debian 12.
You can use the docker run command to create and run your MySQL container:
docker run --name mysql -p 3306:3306 -v mysql_volume:/var/lib/mysql/ -d -e "MYSQL_ROOT_PASSWORD=password" mysql
The options used in the command mean:
- The –name is used for your container name.
- -p option is used for port mapping.
- The -v attach the volume data for your container.
- -d will start and run the container in detached mode.
- The -e option is used for setting a password for your MySQL.
When you run the command, you will get the following output:
Then, verify your container is up and running with the following command:
Output CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c245750272d mysql "docker-entrypoint.s…" 44 seconds ago Up 43 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
Step 3 – Connect To MySQL Docker Container
At this point, you can easily connect to your MySQL shell by using the following commands:
docker exec -it mysql bash
This will change your shell prompt, then, run the command below to access your MySQL shell:
mysql -u root -p
Enter the password you have generated for MySQL in the previous step and press enter. You should see:
bash-4.4# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.1.0 MySQL Community Server - GPL Copyright (c) 2000, 2023, 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>
That’s it, you are done. From there you can easily work with your MySQL docker container.
At this point, you have learned to Install MySQL in a Docker Container on Debian 12 by pulling the latest MySQL image and running the container on your server by using the Docker Run command. Also, you can easily connect to your MySQL shell.
Hope you enjoy it. You may be interested in these articles: