MySQL 8 is a widely used open source relational database. Running MySQL in Docker (or with Docker Compose) lets you use it locally without installing it on the host, run multiple versions side by side, and keep data in a host volume so it survives container restarts. This guide shows how to run MySQL 8 with Docker using both docker run and a docker-compose.yaml with environment variables for root password, user, and database.
In this guide you’ll:
- Run the official
mysql:8image withdocker run, mapping port 3306 and mounting a data volume - Set up MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE via environment variables
- Run the same setup with Docker Compose using a
docker-compose.yaml - Connect to MySQL inside the container and verify the version
Related database-in-Docker guides:
- How to run MariaDB with Docker and Docker Compose
- How to run MS SQL 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’ll use the docker run command with the official MySQL image. We use mysql:8 here (MySQL 8.x); you can pin a specific version (e.g. mysql:8.0.32) for production.
| |
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 sets environment variables. The official MySQL image uses them to create the root password, a database, and an optional user (MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE).
To check that our container is running as expected, use the docker ps command:
| |
The container is running with the name my-mysql. To open a shell inside it and connect to MySQL with the user we created, use docker exec and then run mysql with the credentials you set via environment variables:
| |
If you need to clean up the container when not in use, you can stop and remove the container using this command:
| |
Using Docker Compose
You can run the same MySQL 8 setup with Docker Compose: define the service in a YAML file (image, ports, volumes, environment) and start it with docker compose up -d or docker-compose up -d.
Save the following as docker-compose.yaml in a directory (e.g. where you use ~/apps/mysql for data):
| |
From that directory, start MySQL:
| |
The commands:
upbrings up the container-din a detached mode
Check that the container is running:
| |
To open a shell in the container and connect to MySQL:
| |
Frequently Asked Questions (FAQ)
What is MySQL 8?
MySQL 8 is the current major version of MySQL, the open source relational database from Oracle. It includes improved performance, JSON support, window functions, and better security. The official MySQL Docker image supports MySQL 8 and 9.
What port does MySQL use in Docker?
MySQL listens on TCP port 3306 inside the container (and optionally 33060 for X Protocol). Map it to the host with -p 3306:3306 (or in Compose: ports: - "3306:3306") so you can connect from the host or other containers via localhost:3306.
How do I persist MySQL data in Docker?
Mount a host directory or named volume to /var/lib/mysql inside the container. For example: -v ~/apps/mysql:/var/lib/mysql with docker run, or in Compose: volumes: - mysql_data:/var/lib/mysql with a named volume. Without a volume, data is lost when the container is removed.
Can I use the same volume for docker run and Docker Compose?
Yes. If you created data with docker run using -v ~/apps/mysql:/var/lib/mysql, use the same host path in your Compose file so the Compose stack uses that existing data. Ensure only one container is using the volume at a time.
MySQL 8 vs MariaDB in Docker?
Both use the same client protocol and similar environment variables (MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_DATABASE). For a MySQL-compatible alternative in Docker, see how to run MariaDB with Docker and Docker Compose.
Conclusion
You ran MySQL 8 in Docker using both docker run (with port 3306, volume, and environment variables) and Docker Compose with a docker-compose.yaml. Data is stored on the host so it survives restarts. For other databases in Docker, see MariaDB, PostgreSQL 14, or Redis 6.