Docker Compose :Build and Deploy Microservices using Docker Compose file

Sonu Kumar
4 min readApr 9, 2020

The Compose file provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command ( docker-compose up ).

Here we are using Docker Compose file for defining and running multi-container Docker applications.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and Compose starts and runs your entire app.

Using the Code

Creating Two microservices named service1.Api and service2.Api and deploy and run using docker compose file

Step 1: Create a project file:

Open command prompt (in folder where you want to start, our case using src as root folder )

start executing below commands step by step…

create a new project in a subdirectory named service1.Api:

md service1.Api

cd service1.Api

dotnet new api

Open this project in visual studio and delete the default created weatherforecast controller and class file. Add new api controller named ValueController with read /write option in controller folder

Step 2: Create a Dockerfile :

echo dockerfile>Dockerfile

notepad Dockerfile

— Remove all content in it and paste the below content then save:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY — from=build-env /app/out ./

ENTRYPOINT [“dotnet”, “service1.Api.dll”]

Similarly , Repeat above steps for service2.Api and create for docker file for service2.Api as below :

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /app

# Copy the project file and restore the dependencies
COPY *.csproj ./
RUN dotnet restore

# Copy the remaining source files and build the application
COPY . ./
RUN dotnet publish -c Release -o out

# Build the runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY — from=build-env /app/out ./

ENTRYPOINT [“dotnet”, “service2.Api.dll”]

Step 3: Create a Docker Compose:

Navigate to Root folder and create docker-compose.yaml file as below:

cd ..

echo dockerfile>docker-compose.yml

notepad Dockerfile

— Remove all content in it and paste the below content then save:

version: ‘3’
services:

API1:
image: service1api
build:
context: ./src/service1.api
dockerfile: Dockerfile
ports:
— “9000:80”

API2:
image: service2api
build:
context: ./src/service2.api
dockerfile: Dockerfile
ports:
— “9001:80”

Step 4: RUN the application:

We can run using below command:

docker-compose up

Check the running containers by using docker PS.

Check the docker images using docker images .

We can see that there are 2 containers(running at 9000 & 9001) and 2 images .

Hence our job is done !!!

Conclusion

In previous article, We have looked at how to package our applications into images via docker file. In this section, We have looked at how to deploy docker’s containerize image via the command-line using docker-compose up and docker-compose.yaml file configuration.

Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover .

--

--

Sonu Kumar

Software Consultant interested in Microservices / Serverless computing, Middleware / SOA, Event Driven Architecture & Machine Learning.