Building a Microservice with Docker Compose

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

You’ve just completed developing your weather application, and are ready to deploy it to your production Docker server. After doing some analysis, you decided to deploy three containers that will be load-balanced using Nginx. To do this you need to create a Docker Compose file that will create three `weather-app` services on a private network. Then you will create an Nginx service that will be publicly accessible and have it load balance traffic to the weather-app services.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the Compose File

Create docker-compose.yml:

vi docker-compose.yml

The contents of docker-compose.yml should be:

version: '3'
services:
  weather-app1:
    build:
      context: ./weather-app
      args:
        - VERSION=v2.0
    ports:
      - "8080:3000"
    networks:
     - weather_app
    environment:
      - NODE_ENV=production
  weather-app2:
    build:
      context: ./weather-app
      args:
        - VERSION=v2.0
    ports:
      - "8081:3000"
    networks:
     - weather_app
    environment:
      - NODE_ENV=production
  weather-app3:
    build:
      context: ./weather-app
      args:
        - VERSION=v2.0
    ports:
      - "8082:3000"
    networks:
     - weather_app
    environment:
      - NODE_ENV=production
  nginx:
      build: ./nginx
      tty: true
      ports:
       - '80:80'
      networks:
       - frontend
       - weather_app

networks:
  frontend:
  weather_app:
    internal: true
Add the Services to nginx.conf

Update nginx/nginx.conf:

vi nginx/nginx.conf

The contents of nginx.conf should be:

events { worker_connections 1024; }

http {
  upstream localhost {
    server weather-app1:3000;
    server weather-app2:3000;
    server weather-app3:3000;
  }
  server {
    listen 80;
    server_name localhost;
    location / {
      proxy_pass http://localhost;
      proxy_set_header Host $host;
    }
  }
}
Create the Docker Compose Service

Create the Compose service:

docker-compose up -d

Additional Resources

In the lab directory you will find two directories. The nginx directory has a Dockerfile and an nginx.conf file. Nginx will be used to load balance the weather-app. The weather-app directory has the Dockerfile and source code.

In the lab directory, create a Docker Compose file.

Setting up the weather-app

Make sure the version you are using is 3.
Create three weather-app services: weather-app1, weather-app2, and weather-app3.
Each one should build an image that is located in the weather-app directory.
It will take one argument, VERSION, Set it to v2.0.
For weather-app1, publish port 8080 to 3000 on the container.
For weather-app2, publish port 8081 to 3000 on the container.
For weather-app3, publish port 8082 to 3000 on the container.
All three weather-app containers will be on the weather_app network, which is internal.
The NODE_ENV environment variable will be set to production.

Setting up Nginx

Create a service called nginx.
Update upstream localhost in nginx.conf to include the three containers with their private port.
Example: server [APP-NAME]:[PORT]
Build an image using the Dockerfile in the nginx directory.
Set tty to true.
Publish port 80 to port 80 on the container.
The nginx container will be on two networks: frontend and weather_app.

Setting up the Networks:

Create a public network called frontend.
Create an internal network called weather_app.

Deploy the Services:

Bring up the services. Test to make sure that everything is working by running curl against localhost.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!