Quickly Spin Up MySQL with Docker Compose
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
Paste the following into your
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!
Alex MacArthur is a software engineer working for Dave Ramsey in
Soli Deo gloria.
Get irregular emails about new posts or projects.No spam. Unsubscribe whenever.