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.
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
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
To run any Bash commands inside the running container, use
docker-compose exec db bash.
Use the following values to connect to the running container.
I’ve put this Compose setup and a few others into a repository for easy access. At the time I’m writing this, it includes MySQL, Postgres, and Mongo. Feel free to use it as needed, as well as contrbute any other configurations you’ve found useful.
Boom. With a single command, you have a running, persistent, self-contained MySQL instance ready for your development needs. Have a helpful tip related to anything here? Share it!