Docker publishes a springboot project

1. Create a new SpringBootDemo project and package it

Normally, we execute:

maven clean
maven compile
maven package

The project will generate a target directory locally, and a project jar package is generated under it.
Take a very simple demo as an example:
insert image description here
page:
insert image description here

2. Use Dockerfile to package (basic usage)

At the same level as the target, that is, create a new file in the root directory of the project and name it: Dockerfile
The following basic parameters:

FROM openjdk:8
# 指定可以被宿主机文件挂载目录
VOLUME /opt/demo/
# 指定路径
WORKDIR /opt/demo/
# 添加 springboot-web-demo-0.0.1-SNAPSHOT.jar 包到workdir 以 web.jar保存
ADD ["springboot-web-demo-0.0.1-SNAPSHOT.jar", "web.jar"]
EXPOSE 8090
ENTRYPOINT ["java", "-jar", "/web.jar"]

Then use the remote tool (I use Xftp7) to drag the jar package and Dockerfile file to the newly created directory of the Linux server: command
insert image description here
line to enter the directory to execute image creation:

docker build -t web:test .  # 镜像名:tag   当下目录

Check out the mirror:

docker images

insert image description here
Then start running:

docker run -d -p 8090:8090 --name test web

Map the port to the outside world;
the container is named test, and the image is the generated web
insert image description here
browser access:
failed.
(My project port is 8080, 8090 is exposed, which does not correspond)
Pay attention to the port mapping problem here!

  • The port in the project application.yml should be consistent with the EXPOSE port;
  • When starting the container -p host port: container port, the host port can be specified arbitrarily, and the container port is the port of EXPOSE.

Otherwise, the container can start normally, but the port is not exposed, and the project’s own port is still accessed when accessing (under Linux, the outside world cannot access it)

After re-modifying the dockerfile and packaging the image to run:

curl  ip:port

insert image description here
Browser access:
insert image description here

Summarize:

1. Local maven package + create Dockerfile
2. Upload to the server under the Docker environment
3. Make image
4. Run

It can be seen that it does not seem to bring any convenience to our project deployment? Only in this way, the advantages of docker cannot be reflected.

Further maven source code packaging method

In the Linux docker environment, you can install git and maven, and use:
① git to pull the warehouse code;
② mvn packaging command can remove steps 1 and 2;
③ make a mirror image
④ run to run the container

But still not simple enough.


If maven is not installed in the environment, please install it manually. The script is as follows:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo
# yum-config-manager --enable epel-apache-maven
// 安装maven
# yum install -y apache-maven

1. Upload the original code to the docker environment (usually git/svn directly pulls the source code)
insert image description here

2. maven package
mvn clean package
insert image description here

The generated jar is in the target directory of the same level
insert image description here

3. Execute the docker command to generate
the contents of the image dockerfile
insert image description here

command to create a mirror
insert image description here

3. Going further (maven plug-in packaging)

That is, it is applicable to the production environment. After the IDEA code is developed, use the docker-maven plug-in to directly compile and package the image with one line of commands and push it to the Docker host environment.
After that, you only need to run the run command under Docker to run the project.

docker-maven-plugin

The Docker client is integrated in it, and the docker command is sent through the docker api interface.

spring-boot-maven-plugin

This is the official plug-in of Spring Boot, which provides the ability to build Docker images in a certain version of 2.x (2.3.x).


Below, demonstrate a maven plug-in: docker-maven-plugin usage, to achieve.

prerequisite

1. The docker service needs to be installed on our windows.
2. The docker service needs to configure the http warehouse interface. The docker service configuration on windows is as follows (traditional configuration mode does not have permission to modify files)

local environment configuration

1. Install docker-toolbox on windows, just install it for a fool.
2. Open the Docker Quickstart Terminal and wait for the initialization to complete.
3. Enter the docker-machine env command to return the api interface and certificate location of the docker service, as follows:
insert image description here
4. Enter the docker-machine ssh command to enter the sh environment and configure the http warehouse path:
insert image description here
modify the file configuration (the current user is docker not root , you need sudo to upgrade to root):
sudo vi /var/lib/boot2docker/profile
insert image description here
5. After the modification is completed, save it. Restart the docker service
sudo /etc/init.d/docker restart

insert image description here

Project environment configuration maven plugin

Add the configuration of the docker-maven-plugin plug-in to our project pom, as follows:
insert image description here
1. Among them, imageName configures the full path name of the image, that is, specifies the name of the private library
2. dockerHost and dockerCertPath correspond to the configuration of docker's api and certificate value
insert image description here

package run

Taking idea as an example, after the entire project is assembled, it only needs to operate the first, second and third steps of maven, that is, directly mirror into the warehouse, and the whole process is extremely convenient.
insert image description here

You can also save multiple clicks, and directly complete the packaging and uploading with one mvn command:

mvn clean package docker:build -DpushImage

insert image description here
insert image description here

Verify mirror warehouse results

insert image description here
So far, our server environment can directly run the docker run image to start the container.

4. Summary

After writing this article, I found out that some bloggers have already summarized these methods in their articles. I will post the link here. If you are interested, take a look: use docker to deploy projects to linux
(super detailed)


In addition, you can also refer to the following articles (optional, you can read or not):
1. Use the docker-maven-plugin plug-in to compile the project into a docker image and send it to a remote linux server
2. Idea docker plug-in to package and upload maven projects
3. Maven for docker packaging and push

Guess you like

Origin blog.csdn.net/qq_36256590/article/details/132149149