In this guide we are going to explore how to run Mysql 8 locally with docker and docker compose. This can be helpful if you want to run Mysql 8 locally without installing it in your machine or if you want to run multiple versions of Mysql seamlessly.
- How to run MsSQL Server 2019 with Docker and Docker-Compose
- Running Postgresql 14 with Docker and Docker-Compose
- How to run Redis 6 with Docker and Docker-Compose
- How to run Cassandra 4 with Docker and Docker-Compose
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- Basic knowledge of the command line
Table of content
- Using the
- Using the
1. Using the docker run command
We are going to use the
docker run command to achieve our goal. The version of mysql that we want is
mysql:8.0 – the latest version of mysql image.
❯ docker run -d \ --name my-mysql \ -p 3306:3306 \ -v ~/apps/mysql:/var/lib/mysql \ --user 1000:1000 \ -e MYSQL_ROOT_PASSWORD=S3cret \ -e MYSQL_PASSWORD=An0thrS3crt \ -e MYSQL_USER=citizix_user \ -e MYSQL_DATABASE=citizix_db \ mysql:8 Unable to find image 'mysql:8' locally 8: Pulling from library/mysql b380bbd43752: Pull complete f23cbf2ecc5d: Pull complete 30cfc6c29c0a: Pull complete b38609286cbe: Pull complete 8211d9e66cd6: Pull complete 2313f9eeca4a: Pull complete 7eb487d00da0: Pull complete 4d7421c8152e: Pull complete 77f3d8811a28: Pull complete cce755338cba: Pull complete 69b753046b9f: Pull complete b2e64b0ab53c: Pull complete Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87 Status: Downloaded newer image for mysql:8 38dc3037177eab4a56366b14a4acfdbb6ae98770e03d509046e05aa2a1ce21d0
In the above command:
-dinstructs docker container to run as a detached process. It run container in background and print container ID
-pis for port mapping. We are instructing the container to expose the container port externally. Container port
3306is mapped to host port
3306. That means the service can be accessed through
-vdirective is used to mount volumes. In our case we are mounting the container volume
/var/lib/mysqlto host path
~/apps/mysql. Containers are ephemeral devices that will contain its data for the time it is running. Once a container is stopped, its data is lost. Mounting volumes ensures that the data is added to a host path that can be reused when the container is restarted.
--userargument is used to run the container with an arbitrary user (non root user). This happens if you need to run
mysqldwith a specific UID/GID.
-eargument is for the environment variables. The supplied environment variables will be used to set up a Mysql user, password and a database.
To check that our container is running as expected, use the
docker ps command:
➜ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1d77a05bd18 mysql:8 "docker-entrypoint.s…" 30 seconds ago Up 29 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp my-mysql
In my case the container is running as
my-mysql the name we gave it. We can login to the container using the
docker exec command while executing
/bin/bash interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.
➜ docker exec -it my-mysql /bin/bash [email protected]:/$ mysql -u citizix_user -pAn0thrS3crt mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.27 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> select version(); +-----------+ | version() | +-----------+ | 8.0.27 | +-----------+ 1 row in set (0.00 sec) mysql>
If you need to clean up the container when not in use, you can stop and remove the container using this command:
❯ docker stop my-mysql my-mysql ➜ docker rm my-mysql my-mysql
2. Using the docker-compose tool
We can achieve the same functionality with
docker-compose. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
Docker Compose allows you to define the service (Mysql in our case) with properties like the
image to use,
ports to expose,
volumes to mount and
Here is how we would use
docker-compose to achieve the functionality above. Save this as
version: '3.9' services: mysql: image: mysql:8 ports: - 3306:3306 volumes: - ~/apps/mysql:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=S3cret - MYSQL_PASSWORD=An0thrS3crt - MYSQL_USER=citizix_user - MYSQL_DATABASE=citizix_db
Now bring up the containers:
➜ docker-compose up -d Creating tmp_mysql_1 ... done
upbrings up the container
-din a detached mode
Verify the container processes using the ps command:
❯ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------- tmp_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp
To login to the container and login to mysql, use this:
➜ docker-compose exec mysql /bin/bash [email protected]:/# mysql -u citizix_user -pAn0thrS3crt mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.27 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> select version(); +-----------+ | version() | +-----------+ | 8.0.27 | +-----------+ 1 row in set (0.00 sec) mysql>
In this guide we managed to run Mysql 8 as a container in our system, we explored using the
docker run command while passing the required arguments an alternative approach of simplifying the process with