Writing a docker-compose file

The Docker Compose file is a simple YAML-format file that contains instructions about an isolated system with links to multiple containers. We can also define the environment of each individual container. It is able to start or stop a composite of services with one command. A typical docker-compose.yml file looks like the following:

version: "2" 
services: 
  eureka: 
    image: doj/eureka-server 
    ports: 
      - "8080:8761" 
  account: 
    image: doj/account-service 
    ports: 
      - "8181:6060" 
    links: 
      - eureka 
  customer: 
    image: doj/customer-service 
    ports: 
      - "8282:6060" 
    links: 
      - eureka 
      - account

As you can see in the preceding docker-compose.yml file, it is very easy and has a simple yml syntax. Let's take a close look and what the file means:

  • At the parent level, the version key defines the format of the Docker Compose file. This field is mandatory.
  • At the same level, the services key defines the names of our services, such as account, customer, and eureka.
  • Each service requires an image to run the Docker container, so we have added additional image parameters.
  • The image keyword is used to specify the image from Docker Hub for eureka, account, and customer services:
    • For eureka, we just refer to the doj/eureka-server image available on Docker Hub
    • For account, we refer to the doj/account-service image available on Docker Hub
    • For customer, we refer to the doj/customer-service image available on Docker Hub
  • The ports keyword mentions the ports that need to be exposed to eureka, account, and customer services:
    • For eureka, we have exposed ports to 8080:8761 to access it outside
    • For account, we have exposed ports to 8181:6060 to access it outside
    • For customer, we have exposed ports to 8282:6060 to access it outside
  • We also specify the links variables for account and customer, which are required to create an internal network link between these services and the listed services:
    • The account service links with the eureka service
    • The customer service links with eureka and account services

Now we need to clear out our old running containers with a new version; let's use the docker stop <container ID> command to stop old running containers:

As we have stopped all old running containers, you can check it with the following command:

$ docker ps

The following output will be displayed:

In an enterprise application, running three services very few, this could be tens or hundreds, so managing these containers and ensuring that all of the various command-line parameters link up to these containers can be a little frustrating. Docker Compose comes and organizes a fleet of containers and makes this work easy.

This process is also known as orchestrating or running the containers. Let's see the following section about orchestration using docker-compose and build a Docker Compose example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset