Over a million developers have joined DZone.

Deploying a Spring Boot Microservice With Netflix OSS Stack in a Docker Container

DZone's Guide to

Deploying a Spring Boot Microservice With Netflix OSS Stack in a Docker Container

Learn how to easily deploy microservices developed with the Netflix OSS technology stack and Spring Boot in Docker containers.

· Microservices Zone ·
Free Resource

Learn the Benefits and Principles of Microservices Architecture for the Enterprise

Deploying a Spring Boot microservice with Netflix OSS stack may seem like a complex process with lot of hard work, but it is quite easy once you understand the process of building Docker containers using a command - compose.

With this blog, I will show you in simple steps to easily deploy Netflix OSS stack microservices in Docker containers and how it can be started/stopped with a few simple Docker commands. To keep continuity, I will be using the Netflix OSS stack microservice created through Spring Boot and published in my previous blog here.

Before we get into nitty-gritty of deploying microservices in Docker container, let's clear up few important concepts:


DockerFile defines what goes on in the environment inside the container. It is the building block of Docker images and containers. One can think of it as a a script, which is composed of various commands and arguments listed one by one sequentially to automate actions on a base image in order to create (or form) a new one. They are used for organizing things and greatly help with deployments by simplifying the process from start to finish.

Docker Compose

As defined by Docker, Compose is a tool for defining and running multi-container Docker applications. You will have to install Docker compose if you have not already done it.

There are two steps to deploy the Netflix OSS stack microservice -

First, we will have to create a DockerFile to create the image for each microservice:

FROM java:8-jre
MAINTAINER Shashank Rastogi <shaan.infy@gmail.com>
ADD ./target/EurekaServer-0.0.1-SNAPSHOT.jar /app/
CMD ["java", "-Xmx200m", "-jar", "/app/EurekaServer-0.0.1-SNAPSHOT.jar"]
HEALTHCHECK --interval=10s --timeout=5s CMD curl -f || exit 1

  • FROM: The FROM keyword tells Docker to use a given image with its tag (in this case 8-jre). If the image is not found in the local library, then it will be searched for in Dockerhub.
  • MAINTAINER: identifies the email address of the author.
  • Add EurekaServer-0.0.1-SNAPSHOT.jar under directory /app.
  • Run the jar with parameter - -Xmx200m.
  • Performca health check to see if the container has started correctly or not.
  • EXPOSE: this will tell Docker the port where application will run.

The HEALTHCHECK command will check the container's health by running a command inside the container. In the above case, it will run the curl command 10 seconds after the container is started. The command's exit status indicates the health status of the container. The possible values are:

  • 0: success - the container is healthy and ready for use

  • 1: unhealthy - the container is not working correctly

  • 2: reserved - do not use this exit code

Similar to this, create the Docker file for each microservice. This is available in GitHub for your reference.

Docker Compose will be used to be define the connections between containers and their configuration settings. Create a docker-compose.yml file in the parent directory and add the following:

version: '2.2'
    build: EurekaServer
    image: srast3/discovery-service
    restart: always
      - 8761:8761

    build: ConfigServer
    image: srast3/config-server
    restart: always
        condition: service_healthy
      - 8888:8888

    build: EmailService
    image: srast3/email-service
    restart: always
        condition: service_healthy
      - 8082:8082

    build: UserAccount
    image: srast3/user-service
    restart: always
        condition: service_healthy
      - 8081:8081

The depends_on attribute in the Compose definition file ensures the container startup order. This will tell Docker to start a service only if the listed service has started successfully. However, application-level startup needs to be ensured by the applications running inside the container. Port forwarding is achieved using the ports attribute.

Finally, with just one command, we can build our images, create the defined containers, and start it:

docker-compose up --build

Test the Netflix OSS Microservice

Once all the containers are started successfully, you can test the complete flow by

  1. Creating a new user by calling the URL - POST
  2. Verifying that a new user is created.
  3. Verifying that you have received the registration success email.

The complete code for this microservice implementation is available on GitHub.

Some important Docker commands:

Image title

Final Thoughts

Moving microservices to Docker containers will require some hard work initially, but once you have started using Docker containers, you will see the ease and flexibility in doing so. Docker containers are very lightweight, portable, and ephemeral - easy to stop, destroy, rebuild, and redeploy, and provides massive improvements in both developer and production environments.

Please feel free to comment with your opinion about this article and any other suggestions. Also, if you like this article, don't forget to like and share this article with your friends. Thanks!

Happy coding!

Microservices for the Enterprise eBook: Get Your Copy Here

microservices ,docker ,containers ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}