Docker-Compose for Multi-Container Applications

Mahdi Karimipour
4 min readSep 13, 2021

Managing multi-container applications could become tedious when each container has a range of parameters and environment variables to start up. Docker-compose helps us with that process as it orchestrates management of these containers with just one command. In this guide I will use docker-compose to manage four .NET APIs and one React app.

The Need

Docker-Compose is another abstraction on top of Docker, which helps you manage and orchestrate a multi-container application. Using just one file, you can define which containers you would like to start along with passing the below parameters to each container:

  • Build Arguments
  • Ports
  • Environment Variables
  • Volumes

By the way, this topic belongs to the series to set up Infrastructure for your React Apps and Asp.NET APIs.

  1. Containerise Asp.NET 5.0 API
  2. Containerise React Apps Hosted by Asp.NET 5.0
  3. Use Docker-Compose to Run a Multi-Container Application
  4. Setup Minikube for Asp.NET APIs and React Apps
  5. Kubernetes with Asp.NET, React and Azure DevOps

Dependencies

To begin, please make sure you have the below dependencies installed:

Prerequisites

In this post, I will build, run and manage images and containers of a React App, and Four .NET APIs. However containing React Apps, and .NET APIs on their own individually would be separate topics which I have covered in Containerising .NET APIs and Containerising React Apps using Pellerex.

End Result

Here is the final Docker-Compose file you will have by following this post, and I will explain how I put them together shortly.

As you see, docker-compose is just another yaml file, which automates image creation, and running of the containers of those images along with the injections of all the parameters needed to run those containers. As all of the Apps and APIs I have defined in my docker-compose file are pretty much similar, I just explain them once here in the below sections.

1. Services

Services are all the containers you want to run as part of your multi-container application.

2. Apps/APIs

I have the below services running, as part of my ready-to-use Pellerex stack:

  • Web App: a react app which serves as the front-end of my application.
  • Identity API: an Identity API to take care of Identity functions
  • Subscription API: a Subscription API to manage plans and payments
  • Messaging API: a Messaging API to take care of comms with clients
  • Location API: a Location API to provide location data such as countries, cities, etc.

3. Image

Image is how I name the final docker image, and as you see comes with a prefix (technologyleads), to be used to push my images to DockerHub repositories under the same name (technologyleads), using docker push image-name:tag command.

4. Build

Here we define where to find the Dockerfile for each service, as well as any build arguments we might need to build the image. As you can see I have included the access token needed to fetch the artifacts from a private Azure feed.

5. Ports

The port mapping I use to target each application. As an example, for the web app, 8500:443 means https://localhost:8500, will be mapped to the SSL port (443) within my container.

6. Environment Variables

The list of all environment variables to be passed to each container. This includes SSL settings, Environment, and any other variable we might need to override.

7. Volumes

To map any volumes needed to run my containers. Here I have used them to access the development certificates on the host to support SSL. More on how to enable SSL/TLS for your containers here and here

Note

Configuration, plumbing and troubleshooting your software foundation take a considerable amount of time in your product development. Consider using Pellerex which is a complete foundation for your enterprise software products, providing source-included Identity and Payment functions across UI (React), API (.NET), Pipeline (Azure DevOps) and Infrastructure (Kubernetes).

Build and Run

Once we defined the Docker-compose file, we need to build the images and run the containers, and here is the beauty of docker-compose: It’s all done with one command.

docker-compose up

In this case, if this is the first time we are running the compose command, it also take care of the build process. But if this is not the first time, all it does, is to start the containers. In case we need to rebuild the images, we can use:

docker-compose build

Pellerex: Infrastructure Foundation for Your Next Enterprise Software

How are you building your current software today? Build everything from scratch or use a foundation to save on development time, budget and resources? For an enterprise software MVP, which might take 8–12 months with a small team, you might indeed spend 6 months on your foundation. Things like Identity, Payment, Infrastructure, DevOps, etc. they all take time, while contributing not much to your actual product. These features are needed, but they are not your differentiators.

Pellerex does just that. It provides a foundation that save you a lot development time and effort at a fraction of the cost. It gives you source-included Identity, Payment, Infrastructure, and DevOps to build Web, Api and Mobile apps all-integrated and ready-to-go on day 1.

Check out Pellerex and talk to our team today to start building your next enterprise software fast.

--

--