Quickly Spin MySQL With Docker Compose
0 CommentsLast Updated on February 24, 2020 by jt
In this tutorial, we’ll learn how to install MySQL using Docker-Compose. By using Docker-Compose, we can configure an image the way we want and can turn the image on and of as needed.
Setup
Create a new directory for your MySQL project. Inside of this directory, create a data
directory and a docker-compose.yml
file.
Project_Folder_For_Database |---data |___docker-compose.yml
Inside for the docker-compose.yml
file paste this:
version: '3.7' services: database: container_name: docker-local-mysql image: mysql volumes: - "./data:/var/lib/mysql" restart: always ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: 0123456789
Start the Container
To start the container run this command:
docker-compose up -d
Stop the Container
To stop the container run this command:
docker-compose down
Persisting Data
Whenever MySQL modifies any data it will be persisted locally inside of your data directory
.
Run Commands Inside of the Container
To run Bash commands run this command:
docker-compose exec db bash
This will give you shell access to the container.
Conclusion
In this short tutorial, we’ll learn how to spin up MySQL with Docker-Compose and how to get shell access
to the container.