In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to deploy Docker in SpringBoot. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
What is Docker?
Docker is a platform that enables developers to develop, publish, and execute applications in the form of containerized applications.
It does this by creating a lightweight executable package for your application. This package includes the application code and all the dependencies needed for the application to run. Dependencies can be environment variables, libraries, tools, and so on.
Such an executable package is often called an image. A running image is a container.
Docker itself is a huge topic. If you want to know more, I have a detailed article on the basics of Docker.
Define a Docker image using Dockerfile
To create a Docker image for our Spring Boot Docker deployment, we need to create a Dockerfile in the root directory of the project.
The following is the Dockerfile content of a typical Spring Boot application.
# Start with base imageFROM openjdk:8-jdk-alpine# Add Maintainer InfoLABEL maintainer= "Progressive Coder" # Add a temporary volumeVOLUME / tmp# Expose Port 8080EXPOSE 808 Application Jar FileARG JAR_FILE=target/spring-boot-starter-0.0.1-SNAPSHOT.jar# Add Application Jar File to the ContainerADD ${JAR_FILE} spring-boot-starter.jar# Run the JAR fileENTRYPOINT ["java", "- jar", "/ spring-boot-starter.jar"]
Let's take a look at each step in Dockerfile.
FROM: this statement is used to indicate the base image that our new Docker image will use. In the example above, we used the OpenJDK 8 image. This is a very lightweight option for running Java applications.
LABEL: this directive is used to add some type of metadata to an image. In this case, we added metadata named MAINTAINER.
The VOLUME:Volume directive creates a mount point in the container. You can also map the mount point to a directory on the host operating system. A typical use case for volumes is to store log files generated by our application.
EXPOSE: this is an important instruction. It allows us to expose specific ports to the outside world. In our example, we expose port 8080.
ARG: this directive defines a variable with a default value. In our example, we set it to the location of the JAR file.
Add: in this directive, we are basically copying files and directories to our docker image.
ENTRYPOINT: last but not least, ENTRYPOINT is where you configure how your application should be executed. In our example, we specified how to run the JAR file.
Create a Docker image
Now is the time to create a Docker image. To do this, we need to have the JAR file ready in the Spring Boot project area.
To create a JAR file, execute the following command.
Mvn clean package
Now to build Dockerfile, run the following command:
Docker build-t spring-boot-starter.
After executing this command, you will be able to see the image you are building. If you execute the following command, you can see a list of images:
Docker image ls runs Docker image
Now that the image has been successfully built, you can run the Docker image using the Docker Run command.
Docker run spring-boot-starter
Basically, we ask Docker to run the image marked spring-boot-starter. In other words, this is the Spring Boot Docker deployment that we have been targeting throughout this article.
This will create a container and the application will start running. To view a list of currently running containers, you can use the following command:
Docker ps
The application can be accessed through http://localhost:8080
You can also change the port of the container by issuing the following command to run the container.
Docker run-p 5000 purl 8080 spring-boot-starter
Basically, 5000 here is the port on the host operating system. We map port 8080 on the container to port 5000 on the host operating system.
If you want to stop the running container, you can press CTRL + C.
This is the end of the article on "how to deploy Docker in SpringBoot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.