Using Maven plug-ins to build Docker images for SpringBook applications

Keywords: Java Docker MySQL Maven firewall

SpringBoot e-commerce project mall (20k+star) address: https://github.com/macrozheng/mall

abstract

This article mainly introduces how to package SpringBook applications into Docker images using Maven plug-ins and upload them to Docker Registry, a private image warehouse.

Docker Registry

Docker Registry 2.0 Architecture

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 value, then restart the docker service:

{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

Docker Opens Remote API

Modify docker.service file with vim editor

vi /usr/lib/systemd/system/docker.service

The parts that need to be modified are as follows:

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

The revised part:

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

Let Docker support http image upload

echo '{ "insecure-registries":["192.168.3.101:5000"] }' > /etc/docker/daemon.json

Restart the Docker service

systemctl stop docker
systemctl start docker

Docker Construction Port with Firewall Opened

firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload

Building Docker Image with Maven

The code was modified on the basis of mall-tiny-02.

Adding docker-maven-plugin dependencies to application pom.xml files

<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>

Relevant configuration instructions:

  • executions.execution.phase: This configuration builds docker images when maven packages applications;
  • 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;
  • Docker Host: The address of the docker server uploaded after packaging;
  • baseImage: The basic image on which the application relies, in this case java;
  • entryPoint: The command executed when the docker container starts;
  • resources.resource.targetPath: Copy the packaged resource files to the directory;
  • resources.resource.directory: The directory in which the files need to be copied. The maven packaged application jar package is stored under the target directory.
  • resources.resource.include: Files to be copied, packaged application jar packages.

Modify application.yml to change localhost to db

Containers in docker can be regarded as independent virtual machines. When mall-tiny-docker accesses localhost, mysql can not be accessed naturally. Docker containers can be accessed by specifying a good service name db. As for db, the name can be specified when running mall-tiny-docker container.

spring:
  datasource:
    url: jdbc:mysql://db:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

Packaging projects with IDEA and building mirrors

Note: Dependent basic images need to be downloaded first, otherwise there will be a build image timeout. For example, I do not have a Java 8 image locally, we need to pull down the image first, and then build with the maven plug-in.

  • Execute maven's package command:

  • Successful construction:

  • The image warehouse already has the image:

Running mall-tiny-docker project

service mysql start

  • 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 mall database:
create database mall character set utf8
  • take mall.sql Copy files to mysql container / directory:
docker cp /mydata/mall.sql mysql:/
  • Import sql file into database:
use mall;
source /mall.sql;

Start mall-tiny-docker application service

  • Start with the docker command (--link means that the application can access mysql services with the db domain name):
  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

  • Open port 8080:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker

Public Number

mall project In the whole series of learning courses, we pay attention to the first time acquisition of the public number.

Posted by ramzess on Sun, 08 Sep 2019 01:19:42 -0700