DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Spring Cloud Stream: A Brief Guide
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Spring Boot With Kubernetes
  • 7 Microservices Best Practices for Developers

Trending

  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Infrastructure as Code (IaC) Beyond the Basics
  • The Future of Java and AI: Coding in 2025
  • Memory Leak Due to Time-Taking finalize() Method
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Spring Cloud and Spring Boot, Part 5: Deploying the Eureka Server and Spring Boot Application in Docker

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!

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jan. 17, 19 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
49.4K Views

Join the DZone community and get the full member experience.

Join For Free

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!!

Docker (software) Spring Framework Spring Boot application Spring Cloud

Opinions expressed by DZone contributors are their own.

Related

  • Spring Cloud Stream: A Brief Guide
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Spring Boot With Kubernetes
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: