1. Introduction
Docker is what? What are the benefits? What? Now if you’re a developer and you don’t know or even use it, I feel like you’re behind The Times. Docker is a virtual container technology for continuous integration, version control, portability, isolation and security of application integration. It’s all gobbledygook. In fact, I think the easiest way to understand it is that Docker can create a set of isolated application environment for your application. Any environment that can run Docker, whether it’s Linux, Mac, or Windows, can quickly deploy your application and even launch in seconds. If you have a Mysql cluster image, you can start a Mysql cluster in a flash, in the time of a cigarette. If you have never seen Docker before, you should find some materials to learn it. It is not difficult to learn. Don’t forget to check it out, and come back to share the dry stuff below. So today I’m going to look at how to containerize Spring Boot applications.
2. Environmental
- Spring Boot version 2.2.0.release
Jdk
versionjava 8
I can chooseopenjdk8
ororacle jdk8
Docker
The environment is your choiceLinux
.Mac
orWin10 pro
Not much difference- The development tools
Idea 2019
Ultimate.
3. Spring Boot application
Build a simple Springboot Web Maven application named Docker-Springboot through Idea. And write a Spring MVC interface.
Next we’ll package the Spring Boot as an executable JAR
If you are familiar with Maven, click on the package shown above or use the MVN Clean Package command line to compile it and you will find a packaged JAR file in the target directory.
4. Write Docker scripts
Down the preparation of Docker to create the image of the package script, the name must be Dockerfile, the location of the project root directory, and through Maven plug-in to operate the location is not the same. The following
MAINTAINER Felordcn # Mount container disks VOLUME ["/ TMP "] # MAINTAINER Felordcn # Use the E8 time environment ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # Add /target/docker-springboot-0.0.1- snapshot.jar app.jar # expose port # add /target/docker-springboot-0.0.1- snapshot.jar app.jar Java -jar app.jar -djava.security.egd =file:/dev/./urandom Egd =file:/dev/./urandom","-jar","app.jar"]Copy the code
5. Build an image
Then open the command line tool (idea comes with it) in the project directory and execute the script (remember to start docker). Don’t forget that the last dot represents the current path and other paths write relative paths
Docker build-t docker-springboot:0.0.1Copy the code
And then you go into the layered construction phase and you end up with this mirror image
6. Start the container
Run the docker images command to see if a docker-Springboot: 0.0.1 image exists, as long as the result matches the image. Then start a container from the image with the following command line
Docker run --name docker-app -p 8088:8080 Docker-springboot :0.0.1Copy the code
Docker-springboot :0.0.1, and map port 8080 from the container to host port 8088, so we can access port 8080 from the local port 8088. Then you visit the above written the interface at http://localhost:8088/docker/hello
And you’re done. This makes it easy to containerize the Spring Boot application
7. To summarize
Application containerization is a hot topic in recent years. And container technology emerges in endlessly, it is necessary to master application container technology. Today we built a Spring Boot container from scratch using Docker step by step. I hope it helps.
Follow our public id: Felordcn for more information
Personal blog: https://felord.cn