Abstract
This article describes how to package SpringBoot applications as Docker images using Maven plug-ins and upload them to the Private image repository Docker Registry. Docker command unfamiliar students can first look at the developer must Docker command.
Docker Registry
Docker Registry 2.0 build
docker run -d -p 5000:5000 --restart=always --name registry2 registry:2
Copy the code
If the image cannot be downloaded, modify the /etc/docker-daemon. json file, add the registry-mirrors key value, and restart the docker service.
{
"registry-mirrors": ["https://registry.docker-cn.com"]}Copy the code
Docker enable remote API
Modify the docker.service file with the Vim editor
vi /usr/lib/systemd/system/docker.service
Copy the code
Parts that need to be modified:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Copy the code
The revised section:
ExecStart = / usr/bin/dockerd -h TCP: / / 0.0.0.0:2375 - H Unix: / / var/run/docker. The sockCopy the code
Let Docker support HTTP upload images
Echo '{"insecure-registries":["192.168.3.101:5000"]}' > /etc/docker-daemon.jsonCopy the code
Restart the Docker service
systemctl stop docker
systemctl start docker
Copy the code
Enable the Docker build port for the firewall
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload
Copy the code
Build a Docker image using Maven
This code is modified from mall-tiny-02.
Add the docker-Maven-plugin dependency to the application’s POM. XML file
<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>
Copy the code
Related configuration description:
- Executions. Execution. Phase: during building a docker maven packaged application is configured on the mirror;
- ImageName: used to specify the imageName, mall-tiny is the warehouse name,
${project.artifactId}
Is the mirror name,${project.version}
Is the name of the warehouse; - DockerHost: the address of the docker server that is uploaded after packaging.
- BaseImage: the baseImage on which the application depends, in this case Java;
- EntryPoint: command executed when the docker container is started;
- Resources. The resource. TargetPath: copies after packaging resource file to the directory;
- Resources. The resource. Directory: need to copy the file directory, maven packaged application jar packages exist below the target directory;
- Resources. The resource. Include: the need to copy the file, packaged good application jar package.
Change application.yml to db
Docker container can be regarded as an independent virtual machine, mall tiny-Docker access localhost naturally can not access mysql, docker containers can be accessed through the specified service name DB, The db name can be specified when running the mall-tiny-Docker container.
spring:
datasource:
url: jdbc:mysql://db:3306/mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
Copy the code
Package the project and build the image using IDEA
Note: The dependent base image must be downloaded in advance, otherwise the image will timeout. For example, I do not have a java8 image locally, so I need to pull the image down first, and then use maven plug-in to build.
- Run maven’s package command:
- Successful construction:
- The mirror already exists in the mirror warehouse:
Run the mall-tiny-Docker project
Start the 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.7Copy the code
- Enter the docker container running mysql:
docker exec -it mysql /bin/bash
Copy the code
- Open client with mysql command:
mysql -uroot -proot --default-character-set=utf8
Copy the code
- Change the permission of the root account so that any IP address can access:
grant all privileges on*. *to 'root'@The '%'
Copy the code
- Create mall database:
create database mall character set utf8
Copy the code
- Copy the mall. SQL file to the/directory of the mysql container:
docker cp /mydata/mall.sql mysql:/
Copy the code
- Import SQL file into database:
use mall;
source /mall.sql;
Copy the code
Start the mall-tiny-Docker application service
- Start with docker command (–link indicates that the application can access mysql service with db) :
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 - the SNAPSHOTCopy the code
- Enable port 8080:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
Copy the code
- To conduct access tests, address:http://192.168.3.101:8080/swagger-ui.html
Project source code address
Github.com/macrozheng/…
The public,
Mall project full set of learning tutorials serialized, attention to the public number the first time access.