Link to Existing MySQL container from Docker Compose

Docker Containers

About

I’ve been creating multiple docker-compose.yml file.  I’m starting to have a long list of containers for each WordPress project that I create.  Each time I create a new docker-compose.yml for each WordPress project, 2 containers are created (WordPress and a MySQL container).

What I want to achieve is this:

  • A separate container for each WordPress environment.
  • Have a single MySQL container that will be the centralization for all WordPress environments.

I’ve originally created a post about creating a docker-compose.yml for a WordPress dev environment.  But that creates a WordPress + MySQL paired network and container setup.

Solution

Here’s an example of the solution I’ve created.  Essentially, I’ve added an “external_links” section that references my dev DB called “db-mysql”

In my “docker-compose.yml”, under the WordPress service, I’ve added:


network_mode: bridge

This will stop docker-compose from creating a new network. To learn more about it, click here to visit the official documentation from Docker.

References

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: