Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Cloud and Spring Boot, Part 5: Deploying the Eureka Server and Spring Boot Application in Docker

DZone's Guide to

Spring Cloud and Spring Boot, Part 5: Deploying the Eureka Server and Spring Boot Application in Docker

The title pretty much tells you all you need to know. Let's get started with part 5!

· Cloud Zone ·
Free Resource

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

In this article, you will learn how to run the Eureka Server and Spring Boot application in the Docker.

You need to go through the article Implementing Eureka Server and it is one of the prerequisites for this article.

Prerequisites:

  • Docker runtime or Docker Toolbox installed on your laptop or desktop.

  • Basic knowledge of Spring Boot and Spring Cloud.

  • Walkthrough the article Implementing Eureka Server.

What is Docker?

Docker is a software platform or tool allows you to create, deploy, or run the application by using containers. Docker allows developers to package the application and all its required dependencies, like runtime, required to run the application, external dependencies and ship out it as a single package.

The primary purpose of Docker is to automate the deployment as portable, self-sufficient containers to promote DevOps.

Add Dockerfile in Your Eureka Server Project

Dockerfile is small text file which is used to build the images for Docker. It contains build instructions to build your Docker image.

You need to add Dockerfile to your project root folder and it should contain the below entry.

FROM java:8
EXPOSE 8761
ADD /target/netflix-eureka-naming-server-1.0.jar netflix-eureka-naming-server-1.0.jar
ENTRYPOINT ["java","-jar","netflix-eureka-naming-server-1.0.jar"]

FROM java:8 determines the Java application and it will require Java runtime and libraries to run the application and it will pull all the required Java libraries and add to container.

EXPOSE 8761 determines the application will be exposed to the world on port 8761.

ADD /target/netflix-eureka-naming-server-1.0.jar netflix-eureka-naming-server-1.0.jar:

ADD <Source path of jar from where Docker needs to create image> <Destination in Docker>

ENTRYPOINT ["java","-jar","netflix-eureka-naming-server-1.0.jar"] determines the which Java jar file needs to be run.

Image title

Deploying Eureka Server in Docker

1. Make sure Docker or Docker toolbox is up and running.

2. Go to Docker and build the image.

Go to the path of Eureka server in docker

Image title

3.) Build the Eureka Server image in Docker. Execute below command to build the Docker image.

In Docker build -f Dockerfile -t eureka-server  , -f is Docker filename and -t is tagname)

Image title

4.) To view Docker image, execute below command.

Image title

To view all the Docker images, execute below command.

 Docker images -a  or   Docker images --all 

5.) To run the Docker image eureka-server, execute the below command


docker run -p 8761:8761 eureka-server


Image title

To see the containers running in Docker, execute the below command.

 Docker ps  


Image title

Browse Eureka Server

You can browse the eureka server using URL:

http://localhost:8761  or  http://<IP address of Docker>:8761

To find IP address of Docker, execute below command


 docker-machine ip  


Image title

Add Docker File in your Spring Boot Project

You need to add Dockerfile in your project root folder and it should contain below entry.

FROM java:8
EXPOSE 8080
ADD /target/EurekaClientApplication-0.0.1-SNAPSHOT.jar EurekaClientApplication-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","EurekaClientApplication-0.0.1-SNAPSHOT.jar"]

Image title

Deploying Spring Boot Application in Docker

1.) Make sure Docker or Docker toolbox is up and running.

2.) Go to Docker and build the image.

Go to the path of Spring Boot Application in Docker

cd "<Eureka Server Path>"


3.) Build the Spring Boot application image in Docker. Execute below command to build the Docker image.


Docker build -f Dockerfile -t eureka-client .



 (-f is Docker filename, -t is tagname).

4.) To view Docker image, execute below command.


Docker images eureka-client

 

4.) To view Docker image, execute below command.


Docker images eureka-client


Image title

To view all the Docker images, execute below command.

 Docker images -a    or   Docker images --all 

5.) To run the Docker image eureka-client, execute the below command

docker run -p 8081:8081 eureka-client


Image title

Before performing above steps, please make sure you need to update property eureka.client.service-url.defaultZone in application.properties file located at src/main/resources.

eureka.client.service-url.defaultZone = http://localhost:8761/eureka

or 

eureka.client.service-url.defaultZone = http://<IP address of Docker>:8761/eureka

Now, you try to browse Eureka Server http://localhost:8761 and you will notice that Eureka Client registered with Eureka server.

Image title

Useful Docker Commands

Command Description Example
docker ps List the containers. Docker ps
docker images List images. Docker images <image_name>   
or
docker images -a
or
Docker images --all
docker build Build the Docker image from Dockerfile. Docker build  -f Dockerfile -t <image_name> .
Docker rm Remove the Docker container. Docker rm <image_name>
Docker rmi Remove the Docker image. Docker rmi <container_name>
Docker run Run the command in new container. Docker run -p  8080:8080 <image_name>
Docker stop Stop the container. Docker stop <container_name>
Docker restart Restart the container. Docker restart <container_name>
Docker-machine info Get Docker machine IP address. Docker-machine info
docker pull Pull the Docker image. Docker pull <image_name>
Docker push Push the Docker image to repository. Docker push <image_name>
Docker info Display Docker system information. Docker info

More information on Docker can be found here.

Now, you know how to deploy Eureka Server and Spring Boot application in Docker!!

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.

Topics:
docker ,spring boot ,spring cloud ,microservices ,cloud

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}