Quickly Spin Up MySQL w/ Docker Compose
- February 06, 2019
I’ve often needed to quickly spin up a local instance of MySQL. Docker Compose this makes it stupid easy. Rather than running a long, convoluted
docker command, I can configure an image just the way I want it while maintaining the ability to turn it easily turn it on and off as needed.
Getting Set Up
Inside a new directory, create a
data directory and
new-directory ├── data └── docker-compose.yml
Paste the following into your
version: '3' services: db: container_name: docker-local-mysql image: mysql:5.7.21 volumes: - "./data:/var/lib/mysql" restart: always ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: root
Managing a Container
To start the container, run
docker-compose up. To stop & remove the container, run
docker-compose down. For more information on the plethora of commands avaible to leverage your container, see here.
Whenever MySQL modifies any data within the container, it will persist locally inside your
./data directory, even after you stop and restart everything. This is configured by the
volumes: property in your
Running Commands Inside Container
To run any Bash commands inside the running container, use
docker-compose exec db bash.
Connecting w/ SequelPro or Similar Tool
Use the following values to connect to the running container.
Boom. With a single command, you have a running, persistent, self-contained MySQL instance ready for your development needs. Have a hot, helpful tip related to anything here? Share it!