Connecting Containers in Compose

How to Connect to a MySQL Docker Container Without Using a Static IP Address

Quinn Vissak
Capital One Tech

--

TL;DR — Simplify your code with Docker Compose!

If you are dealing with more than one container in the same application, chances are they need to talk to each other, but have static IP addresses. The code for this looks terrible and requires at least a basic understanding of subnets. Static IP addresses also impede your ability to run more than one instance of your application on the same host.

To address this problem, Docker Compose (and vanilla Docker) by default uses an embedded DNS server to communicate within networks. It allows one to use the service name in the docker-compose.yml file as the domain in the connection details of your other containers. This amazing feature is not limited to MySQL either — it works with any containerized service.

One might think that using Docker Compose for only two services is overkill — especially if parallelization on the same host is not a requirement — but its simplicity will enhance your code quality, make it easier to scale, and simplify adding additional services later on.

--

--

Quinn Vissak
Capital One Tech

Sr SDE @ Amazon — passionate about utilizing the best software practices to increase efficiency.