3

Running Multiple Spring Boot Services with Docker Compose

 3 years ago
source link: https://www.briansdevblog.com/2020/08/running-multiple-spring-boot-services-with-docker-compose/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this post we’ll look at how Docker Compose makes it easier to configure and run multiple containers in your local environment.

Why Docker Compose?

First up, you don’t need Docker compose to run multiple containers. You can do this just fine by manually starting and stopping the containers yourself, as shown previously in this post.  However, as the number of containers in your application grows,  it becomes more cumbersome to manage each container manually.

Docker compose simplifies things by allowing you to configure a multi container application in a single YAML file. You can start and stop all containers in the application with a single command.

Sample App Code

I’ve created a sample app for this post which you can pull from Github. It contains the following

  • 2 Spring Boot applications
    • Bank Account Service – exposes a REST API for creating and reading bank simple account details
    • Config Service – exposes a REST API with application configuration for the Bank Account Service
  • 2 Dockerfiles – to define the container images for the above services
  • A Docker compose file defining the multi container application

Aside form the Docker side of things, I wont go into any detail on the Boot services. If you want more info you can check out this previous post.

Bank Account Service Dockerfile

We’ll begin defining a Docker image for the Bank Account Service.

# MAINTAINER Brian Hannaway

FROM openjdk:8-jre-alpine

WORKDIR /app

# Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
COPY /scripts/wait /app/
RUN chmod +x /app

RUN apk --no-cache add curl

COPY /target/bank-account-service-0.0.1-SNAPSHOT.jar /app/

CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar
  1. # MAINTAINER Brian Hannaway
  2. FROM openjdk:8-jre-alpine
  3. WORKDIR /app
  4. # Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
  5. COPY /scripts/wait /app/
  6. RUN chmod +x /app
  7. RUN apk --no-cache add curl
  8. COPY /target/bank-account-service-0.0.1-SNAPSHOT.jar /app/
  9. CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar
# MAINTAINER Brian Hannaway

FROM openjdk:8-jre-alpine

WORKDIR /app

# Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
COPY /scripts/wait /app/
RUN chmod +x /app

RUN apk --no-cache add curl

COPY /target/bank-account-service-0.0.1-SNAPSHOT.jar /app/

CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar

FROM openjdk:8-jre-alpineFROM openjdk:8-jre-alpine tells Docker to use the openjdk:8-jre-alpine base image.

WORKDIR /appWORKDIR /app tells Docker to create a new working directory in the image called /app.  All further commands will run from this directory.

COPY /scripts/wait /app/COPY /scripts/wait /app/ tells Docker to copy the  waitwait script from the scripts directory on the host to the /app directory in the image. I’ll explain the purpose of the wait script in detail later.

RUN chmod +x /appRUN chmod +x /app makes the contents of the /app directory executable

COPY /target/bank-account-service-0.0.1-SNAPSHOT.jar /app/COPY /target/bank-account-service-0.0.1-SNAPSHOT.jar /app/ copies the service JAR from the target directory on the host to the /app directory in the image

CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jarCMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar runs the  waitwait script, followed by the bank account service. The service won’t run until the waitwait script has finished.

Config Service Dockerfile

Next we’ll define the Config Service Docker image. Its a slightly simpler version of the image we created for the Bank Account Service above. We’ll simply create a working directory, copy in the service JAR and run it.

FROM openjdk:8-jre-alpine

MAINTAINER Brian Hannaway

WORKDIR /app

COPY /target/config-server-0.0.1-SNAPSHOT.jar /app/

ENTRYPOINT ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]
  1. FROM openjdk:8-jre-alpine
  2. MAINTAINER Brian Hannaway
  3. WORKDIR /app
  4. COPY /target/config-server-0.0.1-SNAPSHOT.jar /app/
  5. ENTRYPOINT ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]
FROM openjdk:8-jre-alpine

MAINTAINER Brian Hannaway

WORKDIR /app

COPY /target/config-server-0.0.1-SNAPSHOT.jar /app/

ENTRYPOINT ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]

Defining the Docker Compose File

Now that we’ve defined Dockerfiles for the Bank Account and Config services, the next step to create a docker-compose file that describes how we’ll uses these images to run containers.

version: "3"

services:
   config-service:
      image: config-service
      container_name: config-service
      networks:
         - micro-service-network
      ports:
         - 8888:8888
         
   bank-service:
      image: bank-service
      container_name: bank-service
      networks:
         - micro-service-network
      ports:
         - 8080:8080
      environment:
         WAIT_HOSTS: config-service:8888

networks:
    micro-service-network:
  1. version: "3"
  2. services:
  3. config-service:
  4. image: config-service
  5. container_name: config-service
  6. networks:
  7. - micro-service-network
  8. ports:
  9. - 8888:8888
  10. bank-service:
  11. image: bank-service
  12. container_name: bank-service
  13. networks:
  14. - micro-service-network
  15. ports:
  16. - 8080:8080
  17. environment:
  18. WAIT_HOSTS: config-service:8888
  19. networks:
  20. micro-service-network:
version: "3"

services:
   config-service:
      image: config-service
      container_name: config-service
      networks:
         - micro-service-network
      ports:
         - 8888:8888
         
   bank-service:
      image: bank-service
      container_name: bank-service
      networks:
         - micro-service-network
      ports:
         - 8080:8080
      environment:
         WAIT_HOSTS: config-service:8888

networks:
    micro-service-network:

version: "3"version: "3" tells Docker that we’re using version 3 of the docker-compose file format. At the time of writing version 3 is the latest and recommended version. The docker-compose format version you use will be dictated by the version of Docker you’re running. I’m running Docker version 19.03.12 which means that I should be using version 3. If you want to check what version of docker-compose is compatible with your Docker version, check out this compatibility matrix.

Services Definition

The servicesservices section defines the containers that make up your application. Each service definition contains all the configuration required to start a container from an image. The information in each service definition is what you’d typically supply on the command line, running a container manually.

config-service:
   image: config-service
   container_name: config-service
   networks:
      - micro-service-network
   expose:
      - "8888"
  1. config-service:
  2. image: config-service
  3. container_name: config-service
  4. networks:
  5. - micro-service-network
  6. expose:
  7. - "8888"
config-service:
   image: config-service
   container_name: config-service
   networks:
      - micro-service-network
   expose:
      - "8888"

Config Service

The config-serviceconfig-service section defines all the configuration docker needs to run the config-service container

imageimage tells compose which image to use to run the container.

container_namecontainer_name is the name given to the container when it starts. If we don’t specify a name, compose will derive one based on the name of the compose file and the image name. For example, if I omit the name attribute for the config-serviceconfig-service and run docker-compose updocker-compose up , I can see that the containers derived name is boot-microservices-docker-compose_config-service_1boot-microservices-docker-compose_config-service_1.

containerDerivedName.png

Generally its a good idea to give your containers a meaningful name. You’ll see later that we need to reference config-serviceconfig-service from the bank-servicebank-service. We’ll do this using the name specified in container_namecontainer_name.

networksnetworks defines the networks that the config-serviceconfig-service container will join when it starts. In this instance it will join  micro-service-networkmicro-service-network, which we’ll define later.

exposeexpose lists the ports that are exposed on the container. The ports are exposed on either the default network or any network the container is attached to. The ports are not exposed to the host machine. To do this you’ll need to use the  portsports attribute and supply the appropriate mappings.

Bank Service

The bank-servicebank-service definition is very similar to what we’ve already defined, with the imageimage, container_namecontainer_name, networksnetworks and  exposeexpose attributes being similar to those defined for the config-serviceconfig-service

   image: bank-service
   container_name: bank-service
   networks:
      - micro-service-network
   expose:
      - "8080"
   environment:
      WAIT_HOSTS: config-service:8888
  1. image: bank-service
  2. container_name: bank-service
  3. networks:
  4. - micro-service-network
  5. expose:
  6. - "8080"
  7. environment:
  8. WAIT_HOSTS: config-service:8888
   image: bank-service
   container_name: bank-service
   networks:
      - micro-service-network
   expose:
      - "8080"
   environment:
      WAIT_HOSTS: config-service:8888

The environmentenvironment attribute is used to specify a list of environment variables for the container. In the bank-servicebank-service we specify the environment variable WAIT_HOSTSWAIT_HOSTS and give it the value config-service:8888config-service:8888. In short, this  is required to control container start up order and ensure that the config-serviceconfig-service is up and running before the bank-servicebank-service starts. I’ll explain this in detail later.

Networks Definition

The networksnetworks section allows you define a network for your services. For our application we defined a network called micro-service-networkmicro-service-network.   When you run docker-compose updocker-compose up, each container that starts will be added to the micro-services-networkmicro-services-network and will be visible to every other container in the application. Containers can reference one another via their host name, which is the same as the service name.  So in our sample application, the banks-servicebanks-service is able to access the config-serviceconfig-service as config-service:8888.config-service:8888.

If we don’t explicitly define a networknetwork, Docker will create one by default and add all services in the compose file to it.

Running the Application

Running the docker-compose updocker-compose up command will

  • create a bridge network called micro-service-networkmicro-service-network
  • start a container using the config-serviceconfig-service image. The container will expose port 8888 on micro-service-networkmicro-service-network and will be accessible to other containers via host name config-serviceconfig-service.
  • start a container using the bank-servicebank-service image. The container will expose port 8080 on micro-service-networkmicro-service-network and will be accessible to other containers via host name bank-servicebank-service.

It takes approximately 20 seconds for both the  bank-servicebank-service and config-serviceconfig-service to start. If you run docker container lsdocker container ls you should see the two containers that have just been created.

containers_running.png

Service Dependencies & Startup Order

Its common to have dependencies between containers, such that a container A requires container B to be running before container A can start. Compose allows you to handle this scenario to a certain degree, by defining the startup order using the depends_ondepends_on attribute. For example, the compose file below defines a webweb service and a dbdb service, where webweb is dependent on dbdb.

version: '3'
services:
  web:
    image: myWebApp
    depends_on:
      - db
  db:
    image: postgres
  1. version: '3'
  2. services:
  3. image: myWebApp
  4. depends_on:
  5. image: postgres
version: '3'
services:
  web:
    image: myWebApp
    depends_on:
      - db
  db:
    image: postgres

In the above example, compose will start the containers in dependency order, so dbdb will be started before webweb. Although depends_ondepends_on sets the order in which containers are started, it does not guarantee that Postgres, inside the dbdb container is fully operational before the  webweb container starts.

We have a similar problem in our sample application because bank-servicebank-service tries to call config-serviceconfig-service on startup. If config-serviceconfig-service isn’t fully stood up and available to take requests on port 8888, bank-servicebank-service will fail. Using the depends_ondepends_on attribute to start the config-serviceconfig-service first, won’t guarantee that the config-serviceconfig-service is fully operational before bank-servicebank-service calls it.

Introducing docker-compose-wait

docker-compose-wait is a great command line utility that solves the problem described above. When defining the bank-servicebank-service earlier we made docker-compose-wait available to the image by copying the waitwait script into the appapp directory.

# Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
COPY /scripts/wait /app/
  1. # Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
  2. COPY /scripts/wait /app/
# Add wait script to the image - script pulled from https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
COPY /scripts/wait /app/

We then told Docker to run the waitwait script along with the JAR when starting the container.

CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar
  1. CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar
CMD ./wait && java -jar bank-account-service-0.0.1-SNAPSHOT.jar

When we defined bank-servicebank-service in the docker-compose file, we included a WAIT_HOSTSWAIT_HOSTS environment variable that referenced config-serviceconfig-service on port 8888. When we run  docker-compose updocker-compose up, the waitwait script pings config-serviceconfig-service on port 8888. It will not allow bank-servicebank-service container to start until config-serviceconfig-service is up and running on port 8888.

We can see this in action in the log snippets below. The waitwait script checks if  config-serviceconfig-service is available on port 8888, initially reporting that it isn’t available.

docker-compose-wait-1.png

Eventually config-serviceconfig-service bootstraps and is up and running on port 8888. The waitwait script then reports Host config-service:8888 is now available and the bank-servicebank-service container is started.

docker-compose-wait-2.png

Wrapping Up

In this post we looked at how docker-compose makes it easy to manage multiple containers in a simple single node environment. This is particularly useful for development environments and automated test environments. If you want to manage multiple containers in a multi node environment, then Docker Swarm is a better bet. We’ll look at Swarm in another post soon.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK