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.
Related content:
- 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
Prerequisites
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
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.
| |
In the above command:
- The
-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 port3306is mapped to host port3306. That means the service can be accessed throughlocalhost:3306- The
-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. - The
--userargument is used to run the container with an arbitrary user (non root user). This happens if you need to runmysqldwith a specific UID/GID. - The
-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:
| |
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.
| |
If you need to clean up the container when not in use, you can stop and remove the container using this command:
| |
Using the docker-compose tool
We can achieve the same functionality with docker-compose. [Docker Compose][1] is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application 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 environment variables.
Here is how we would use docker-compose to achieve the functionality above. Save this as docker-compose.yaml:
| |
Now bring up the containers:
| |
The commands:
upbrings up the container-din a detached mode
Verify the container processes using the ps command:
| |
To login to the container and login to mysql, use this:
| |
Conclusion
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 docker-compose.