Home > Java > javaTutorial > body text

How to package SpringBoot project into Docker image

PHPz
Release: 2023-05-14 20:55:04
forward
5253 people have browsed it

There are two options for packaging the SpringBoot project into a Docker image:

Full automation: First build the docker image warehouse, and then configure the warehouse address in the project's maven configuration. Configure the Dockerfile file in the project, so that it can be packaged directly in the idea and automatically uploaded to the image warehouse, and then just start the image on the server.

Semi-automation: There are two options for semi-automation. One is to place the Dockerfile file inside the project, and the other is to place it outside the project.

  • Put it in the project: configure maven plug-in support in springboot pom.

  • #Put it outside the project: springboot is still packaged into an ordinary jar, and then upload the jar to the server. At the same time, create a Dockerfile file on the server, execute the docker build command, and build this jar into a docker image, and then execute it through the image.

Generally speaking, semi-automation is used more than full automation. This article uses the second method of semi-automation. Generally speaking, there are several steps:

1. Build the SpringBoot project

How to package SpringBoot project into Docker image

How to package SpringBoot project into Docker image

Write a controller

How to package SpringBoot project into Docker image

Conduct local testing

How to package SpringBoot project into Docker image

2. Package the application

Click package to package the project

How to package SpringBoot project into Docker image

Packaging successful

How to package SpringBoot project into Docker image

cmd runs java -jar and it can run successfully

How to package SpringBoot project into Docker image

You can download a Docker plug-in

How to package SpringBoot project into Docker image

3. Write dockerfile

Create the Dockerfile file in the target. At this time, the Dockerfile file is highlighted

How to package SpringBoot project into Docker image

Dockerfile configuration content (there will be prompts when writing Dockerfile in Idea)

#发布到网上时只会把jar包和Dockerfile发布上去
COPY *.jar /app.jar
#地址映射
CMD ["--server.port=8080"]
#对外暴露端口
EXPOSE 8080
#执行命令
ENTRYPOINT ["java","-jar","/app.jar"]
Copy after login

4. Build the image

Take out the jar package and Dockerfile file and create a new folder in it, press Hold down the shift key to open PowerShell

(Note: Docker Desktop is installed on my computer)

How to package SpringBoot project into Docker image

Enter the command to build the image, firstdocker is the image name (the last . means Dockerfile The file is in the current directory)

How to package SpringBoot project into Docker image

View the built image (if you don’t know the commands, you can learn the basic commands first)

How to package SpringBoot project into Docker image

After the image is built successfully, run the container (the first is the server port 8080, the second 8080 is the docker container port)

How to package SpringBoot project into Docker image

You can see whether the container is running and the logs Information

How to package SpringBoot project into Docker image

Access successful

How to package SpringBoot project into Docker image

5. Release and run

You can upload the image to dockerhub, After using Docker in the future, all you need to deliver to others is an image!

The above is the detailed content of How to package SpringBoot project into Docker image. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!