paint-brush
Running Multiple Docker Containers With Custom Config Filesby@abrar
1,583 reads
1,583 reads

Running Multiple Docker Containers With Custom Config Files

by abrarMarch 12th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Many of us using Docker to make our lives easier have ran into the issue of conflicting ports due to running more than 1 docker container. It’s easy to get bogged down into changing config files and squander an entire afternoon. I intend to write this article as a reminder to my future self whenever I need to set up multiple instances of docker container on a single machine. The idea to tackle this issue, lies in creating a separate config file that ONLY serves your dev environment and put it into Gitignore.

Company Mentioned

Mention Thumbnail
featured image - Running Multiple Docker Containers With Custom Config Files
abrar HackerNoon profile picture

Many of us using Docker to make our lives easier have ran into the issue of conflicting ports due to running more than 1 docker container. It’s easy to get bogged down into changing config files and squander an entire afternoon. I intend to write this article as a reminder to my future self whenever I need to set up multiple instances of docker container on a single machine.

My development environment consists of — A Linux machine and two separate Laravel projects to run in distinct containers.

The files to modify

There are essentially 2 files which require modification:

  • .env
  • docker-compose.yml

Both of these files are located at the root directory of the Laravel projects.

The format of PORTs in Docker config

Before we proceed let’s refresh the format of the ports as defined in the docker-compose.yml.

The format is: <External>:<Internal>

This is super important to remember.

So, if you see the MySQL port defined as 33060:3306, you immediately know that 33060 is the external port that the container exposes to the outside world. Whereas 3306 is the internal port used inside the container itself.

Now, let’s get into business!

Modifying the docker-compose.yml file

Initially both of my projects came with a docker-compose.yml file containing the ports as follows:

 mysql: 
        ports: '3306:3306'
  appid:
        ports: '8000:80'
  redis:
        ports: '6379: 6379'

Notice that both the projects will attempt to run on the same ports since both of them have the exact same ports exposed (defined as external).

Now, in order to solve this issue, you need to change one of the project’s config file and update the exposed ports to different ones. But another issue pops-up. It is likely you will be collaborating with multiple developers on the same project. A config change pushed by you might not be on par with their development environment. You are left in a conundrum!

The idea to tackle this issue, lies in creating a separate config file that ONLY serves your dev environment and put it into .gitignore

The steps goes like this:

  • Create a docker-compose-dev.yml
  • Add it in .gitignore
  • Update it’s contents with PORTs of your choice and not conflicting with other docker containers running

My docker-compose-dev.yml looked like this:

   mysql: 
        ports: '33060:3306'
    appid:
        ports: '8001:80'
    redis:
        ports: '6378: 6379'

Notice that, the internal ports on the right are still the same. Cause other containers on the machine don’t care about any internal ports running in any other container. Thus it is safe to change the external ports only!

Modifying the .env file

The key thing to remember here is:

the .env file should always contain the external ports as defined in the docker-compose.yml

Keeping that in mind we update our .env file and add the external ports to it.

The modified file should have the following ports defined:

APP_URL=http://localhost:8001
DB_PORT=33060
REDIS_PORT=6378

Considering the case where a separate application is running on the 8000 port of localhost.

Wrapping it up

To wrap up everything, we need to follow just 1 rule while running our docker-compose cmds.

> docker-compose -f docker-compose-dev.yml build/run/<other docker cmds>

The key thing to remember here is, appending this line:

docker-compose -f <your-updated-docker-compose-filename> before giving any cmds.

This should do the job!

Previously published at https://medium.com/consol/running-multiple-docker-containers-with-custom-config-files-777a0f57fe89