This article mainly introduces how to use the Maven plugin to package the SpringBoot application as a Docker image and upload it to the private image repository Docker Registry.
content
Building Docker images with Maven
Add the dependency of docker-maven-plugin to the pom.xml file of the application
Modify application.yml and change localhost to db
Use IDEA to package the project and build the image
Run the mall-tiny-docker project
Start the mall-tiny-docker application service
Docker Registry 2.0 build
docker run -d -p 5000:5000 --restart=always --name registry2 registry:2
If the image cannot be downloaded, you need to modify the /etc/docker/daemon.json file and add the registry-mirrors key, and then restart the docker service:
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
Docker open remote API
Modify the docker.service file with the vim editor
vi /usr/lib/systemd/system/docker.service
Parts that need to be modified:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Modified part:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
Let Docker support http uploading images
echo '{ "insecure-registries":["192.168.3.101:5000"] }' > /etc/docker/daemon.json
systemctl daemon-reload
systemctl stop docker
systemctl start docker
Open the Docker build port of the firewall
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload
Building Docker images with Maven
This code is modified from mall-tiny-02.
Add the dependency of docker-maven-plugin to the pom.xml file of the application
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>mall-tiny/${project.artifactId}:${project.version}</imageName>
<dockerHost>http://192.168.3.101:2375</dockerHost>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar","/${project.build.finalName}.jar"]
</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
Related configuration instructions:
- executions.execution.phase: here is configured to build the docker image when maven packages the application;
- imageName: used to specify the image name, mall-tiny is the warehouse name,
${project.artifactId}
is the image name,${project.version}
is the warehouse name; - dockerHost: The address of the docker server uploaded to after packaging;
- baseImage: The base image the application depends on, here is java;
- entryPoint: The command executed when the docker container starts;
- resources.resource.targetPath: Copy the packaged resource file to this directory;
- resources.resource.directory: The directory where the files to be copied are located, and the application jar package packaged by maven is stored in the target directory;
- resources.resource.include: The file to be copied, the packaged application jar package.
Modify application.yml and change localhost to db
The container in docker can be regarded as an independent virtual machine. When mall-tiny-docker accesses localhost, you will not be able to access mysql. The docker container can be accessed through the specified service name db. As for the name db, you can run mall -tiny-docker container when specified.
spring:
datasource:
url: jdbc:mysql://db:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
Use IDEA to package the project and build the image
Note: The dependent base image needs to be downloaded first, otherwise the build image will time out. For example, if I don't have a local java8 image, I need to pull the image first, and then use the maven plugin to build it.
- Execute the maven package command:
- Build succeeded:
- The mirror repository already has this mirror:
Run the mall-tiny-docker project
start mysql service
- Start with the docker command:
docker run -p 3306:3306 --name mysql \ -v /mydata/mysql/log:/var/log/mysql \ -v /mydata/mysql/data:/var/lib/mysql \ -v /mydata/mysql/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:5.7
- Enter the docker container running mysql:
docker exec -it mysql /bin/bash
- Open the client using the mysql command:
mysql -uroot -proot --default-character-set=utf8
- Modify the permissions of the root account so that any ip can access:
grant all privileges on *.* to 'root'@'%'
- Create the mall database:
create database mall character set utf8
- Copy the mall.sql file to the / directory of the mysql container:
docker cp /mydata/mall.sql mysql:/
- Import the sql file to the database:
use mall; source /mall.sql;
Start the mall-tiny-docker application service
- Use the docker command to start (--link means that the application can use the db domain name to access the mysql service):
docker run -p 8080:8080 --name mall-tiny-docker \ --link mysql:db \ -v /etc/localtime:/etc/localtime \ -v /mydata/app/mall-tiny-docker/logs:/var/logs \ -d mall-tiny/mall-tiny-docker:0.0.1-SNAPSHOT
Copy to clipboardErrorCopied - Open port 8080:
firewall-cmd --zone=public --add-port=8080/tcp --permanent firewall-cmd --reload
- Conduct access test, address: http://192.168.3.101:8080/swagger-ui.html