How to Share a MySQL DB with Multiple Docker Containers
| devops - Shawn Powers

How to Share a MySQL DB with Multiple Docker Containers

The absolute best way to learn something is to do it. In driver’s education, students drive cars. When a pitcher learns a curveball, she does it by throwing pitches over and over. Heck, on Sunday, July 27, 1980, at 2:52 PM, I finally learned to ride a bicycle without training wheels by succeeding after countless trials and errors.

Admittedly, it’s weird for me to know the exact moment I learned to ride a bike. But as it happened, the first time my mom let go of the bike seat and rode by myself, the Sharpsburg Earthquake hit Kentucky. It was a big enough quake that in Detroit, the ground shook hard enough to knock me over. I’m not saying my 5-year-old feat of skill was enough to make the whole earth shake… but *something* caused it. It might have been me. You don’t know!

Anyway, Docker is just such a skill. The best way to learn how to manage dockerized containers is to use Docker in your environment. The best way to learn how to get containers to work together is to make containers work together. The perfect way to learn container interaction is to create a central MySQL Docker container and have other applications connect to it for their database needs.

Setting up a Docker Network: Sharing Resources

Docker is still a relatively new technology. Yes, it’s several years old, but as it’s matured, the “best practice” way of doing things has changed. For example, historically, the way to allow Docker containers to connect was to use the “–link” argument. It worked well but had some limitations. For example, if a container restarted, it would sever the link. If you find an old google search result explaining how to use “–link” to bind containers together, it’s not that the article is wrong, it’s just that it’s outdated.

As of this writing, the “correct” way to allow containers to talk behind the scenes is to set up a network that only exists to the Docker containers. It will enable them to speak as though there were on a local, private network. Setting this up accomplishes the same thing as the “–link” command did, but it is much more useful.

Private Networks, Public Names

The nice thing about Docker networks is that they exist apart from the containers themselves. That’s why a container restarting doesn’t affect the private network at all. If you give a container access to the network, it can communicate with all the other containers connected to that network. As long as they’re running, they’re available.

Once you connect a container to an internal network, you can reference other containers using the “–name” as the hostname — no need to worry about IP addresses, especially since containers get ephemeral addresses when they start. Thankfully, regardless of their IP address, their hostname is always valid, and Docker handles all the mapping behind the scenes.

How to Share MySQL Among Docker Containers

Back to the original point, a shared MySQL container is the perfect example of how to utilize Docker networks. Many apps require a database for their data, including a database server into every dockerized app. It makes much more sense just to share a centralized database container. In this video, I show how to do just that:

Docker Networks are Just Like Riding a Bike

The MySQL example is undoubtedly an obvious place to take advantage of Docker networks. Once you’re comfortable with the idea of containers talking to each other, however, it opens up tons of different possibilities. Want to have a centralized reverse proxy that connects to containers without the need to expose their ports to your network at all? Want authentication between containers, but don’t want to uncover a potential attack vector to your more extensive network? Docker is very flexible, and once you start using its less common features, you begin to understand the incredible potentials they offer.

The best thing about Docker is that you design containers to be disposable. If you mess up, just delete the container, brush off your knees, and get right back on that bicycle. If there’s an earthquake, well, I don’t know. You might need to reboot!



Ultimate Systems Administration Cert Guide

A 158-page guide to every Microsoft, VMware, Citrix, AWS, Google, and Linux certification, and how they fit into your career.

I have read and understood the privacy policy, and am able to consent to it.