SpringCloud项目打包Docker镜像并发布到Harbor仓库
- 1. Docker-compose安装
- 2. Harbor仓库搭建
- 3. Docker环境配置
- 4. 项目配置
1. Docker-compose安装
#安装
wget http://rancher-mirror.cnrancher.com/docker-compose/v1.27.4/docker-compose-Linux-x86_64
mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
#赋权
chmod a+x /usr/local/bin/docker-compose
#查看版本号
docker-compose --version
2. Harbor仓库搭建
Harbor安装前务必先安装好Docker-compose
。
Harbor的安装包有offline
和online
两种,可视网络情况选择,此处选用offline
安装。
手动下载好离线包并上传到服务器进行安装
tar zxvf harbor-offline-installer-v2.5.2.tgz
修改配置
#修改配置
cp harbor.yml.tmpl harbor.yml
vim harbor.yml
关键配置
#配置主机ip及端口
hostname: 8.142.11.11
http:
port: 8099
#屏蔽https
#https:
# port: 443
#certificate: /your/certificate/path
#private_key: /your/private/key/path
#管理员默认密码
harbor_admin_password: Harbor12345
安装
./install.sh
安装完成后可使用http://8.142.11.11:8099进行访问,账号admin/Harbor12345。
视需要创建用户及项目
3. Docker环境配置
将私有仓库地址添加到docker配置 /etc/docker/daemon.json中
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries":["8.142.11.11:8099"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
修改完成后重新加载配置并重启docker
systemctl daemon-reload
systemctl restart docker
使用docker登录harbor
docker login http://8.142.11.11:8099 --username harbor --password harbor
登录成功后docker会将账号密码以base64形式存放到/root/.docker/config.json中
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
测试镜像推送
#标记镜像tag
docker tag registry 8.142.11.11:8099/leway-cloud/registry:latest
#推送镜像
docker push 8.142.11.11:8099/leway-cloud/registry:latest
#推送完成
The push refers to repository [8.142.11.11:8099/leway-cloud/registry]
a1d94f4a4c89: Pushed
1d80677cd085: Pushed
58548fced558: Pushed
79f02b258438: Pushed
24302eb7d908: Pushed
latest: digest: sha256:affdb68e24c93262ee98a5bdc371e8f631568e9b4401375e7a0ecc705ac526b2 size: 1363
推送完成后可以在仓库中查看
4. 项目配置
在父级pom.xml
中使用dockerfile-maven-plugin
插件
<properties>
<dockerfile.maven.plugin.version>1.4.13</dockerfile.maven.plugin.version>
<docker.repository>8.142.11.11:8099</docker.repository>
<docker.repository.project>leway-cloud</docker.repository.project>
<!--默认全局跳过dockerfile-->
<dockerfile.skip>true</dockerfile.skip>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile.maven.plugin.version}</version>
<!--不需打包推送镜像到仓库时将此处executions屏蔽即可-->
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.repository}/${docker.repository.project}/${docker.image.name}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<!--使用maven的setting.xml文件中harbor的登录信息-->
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
</plugin>
</plugins>
</build>
在Maven的配置文件settings.xml
中配置harbor登录信息
<servers>
<server>
<id>8.142.11.11:8099</id>
<username>harbor</username>
<password>harbor</password>
<configuration>
<email>liuwei.cd@mapgis.com</email>
</configuration>
</server>
</servers>
需要打包成镜像的子模块中配置pom.xml
<properties>
<docker.image.name>${artifactId}</docker.image.name>
<dockerfile.skip>false</dockerfile.skip>
</properties>
同时在子模块添加Dockerfile
文件
#指定所需依赖的基础镜像
FROM openjdk:8-jdk-alpine
#维护者
MAINTAINER leway
ARG JAR_FILE
#设置环境变量
ENV TIME_ZONE=Asia/Shanghai
ENV LANG=C.UTF-8
#在容器构建过程中执行的命令
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
&& echo "https://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
#以上两行指定alpine(Linux)源
#升级包
&& apk update upgrade \
#安装常用命令
&& apk add --no-cache procps unzip curl bash tzdata \
#安装宋体
&& apk add ttf-dejavu \
#同步时区
&& ln -snf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime \
&& echo ${TIME_ZONE} > /etc/timezone
#声明服务运行的端口
EXPOSE 8003
#将当前目录下的jar包复制到docker容器的/目录下
ADD ${JAR_FILE} /app.jar
#指定docker容器启动时执行的命令
ENTRYPOINT ["java","-jar","-Duser.timezone=Asia/Shanghai","/app.jar"]
- Docker开启远程
vim /usr/lib/systemd/system/docker.service
修改ExecStart
内容
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
重启docker
systemctl daemon-reload
systemctl restart docker
DOCKER_HOST配置
插件默认使用本地docker环境,因此使用远程docker时需要配置环境变量DOCKER_HOST=tcp://8.142.11.11:2375
- 打包及推送
至此配置完成,使用 mvn package
或mvn dockfile:build
可打包镜像文件。打包镜像时会按照Dockerfile
中的指令顺序执行。
[INFO] --- dockerfile-maven-plugin:1.4.13:build (default-cli) @ leway-cloud-module-rabbitmq ---
[INFO] dockerfile: null
[INFO] contextDirectory: D:\Learning\Code\Java\leway-cloud\leway-cloud-module\leway-cloud-module-rabbitmq
[INFO] Building Docker context D:\Learning\Code\Java\leway-cloud\leway-cloud-module\leway-cloud-module-rabbitmq
[INFO] Path(dockerfile): null
[INFO] Path(contextDirectory): D:\Learning\Code\Java\leway-cloud\leway-cloud-module\leway-cloud-module-rabbitmq
[INFO]
[INFO] Image will be built as 8.142.11.11:8099/leway-cloud/leway-cloud-module-rabbitmq:1.0.0
[INFO]
[INFO] Step 1/9 : FROM openjdk:8-jdk-alpine
[INFO]
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
[INFO] Status: Image is up to date for openjdk:8-jdk-alpine
[INFO] ---> a3562aa0b991
[INFO] Step 2/9 : MAINTAINER leway
[INFO]
[INFO] ---> Using cache
[INFO] ---> 3a9e41adcdc0
[INFO] Step 3/9 : ARG JAR_FILE
[INFO]
[INFO] ---> Using cache
[INFO] ---> b232c45b47c2
[INFO] Step 4/9 : ENV TIME_ZONE=Asia/Shanghai
[INFO]
[INFO] ---> Using cache
[INFO] ---> 2324dc03007e
[INFO] Step 5/9 : ENV LANG=C.UTF-8
[INFO]
[INFO] ---> Using cache
[INFO] ---> 1b9fafcabcff
[INFO] Step 6/9 : RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories && echo "https://mirrors.aliyun.com/alpine/v3.6/community" >> /
etc/apk/repositories && apk update upgrade && apk add --no-cache procps unzip curl bash tzdata && apk add ttf-dejavu && ln -snf /usr/share/zoneinfo/
${TIME_ZONE} /etc/localtime && echo ${TIME_ZONE} > /etc/timezone
[INFO]
[INFO] ---> Running in d55e4e836de2
[INFO] fetch https://mirrors.aliyun.com/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
[INFO] fetch https://mirrors.aliyun.com/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
[INFO] v3.6.5-44-gda55e27396 [https://mirrors.aliyun.com/alpine/v3.6/main]
[INFO] v3.6.5-34-gf0ba0b43d5 [https://mirrors.aliyun.com/alpine/v3.6/community]
[INFO] OK: 8496 distinct packages available
[INFO] fetch https://mirrors.aliyun.com/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
[INFO] fetch https://mirrors.aliyun.com/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
[INFO] (1/15) Installing ncurses-terminfo-base (6.0_p20171125-r1)
[INFO] (2/15) Installing ncurses-terminfo (6.0_p20171125-r1)
[INFO] (3/15) Installing ncurses-libs (6.0_p20171125-r1)
[INFO] (4/15) Installing readline (6.3.008-r5)
[INFO] (5/15) Installing bash (4.3.48-r1)
[INFO] Executing bash-4.3.48-r1.post-install
[INFO] (6/15) Installing libressl2.5-libcrypto (2.5.5-r2)
[INFO] (7/15) Installing libssh2 (1.8.2-r0)
[INFO] (8/15) Installing libressl2.5-libssl (2.5.5-r2)
[INFO] (9/15) Installing libcurl (7.61.1-r2)
[INFO] (10/15) Installing curl (7.61.1-r2)
[INFO] (11/15) Installing libintl (0.19.8.1-r1)
[INFO] (12/15) Installing libproc (3.3.12-r1)
[INFO] (13/15) Installing procps (3.3.12-r1)
[INFO] (14/15) Installing tzdata (2019a-r0)
[INFO] (15/15) Installing unzip (6.0-r3)
[INFO] Executing busybox-1.29.3-r10.trigger
[INFO] OK: 119 MiB in 69 packages
[INFO] (1/7) Installing expat (2.2.0-r1)
[INFO] (2/7) Installing fontconfig (2.12.1-r0)
[INFO] (3/7) Installing encodings (1.0.4-r0)
[INFO] (4/7) Installing libfontenc (1.1.3-r1)
[INFO] (5/7) Installing mkfontscale (1.0.7-r2)
[INFO] (6/7) Installing mkfontdir (1.0.5-r1)
[INFO] (7/7) Installing ttf-dejavu (2.35-r0)
[INFO] Executing busybox-1.29.3-r10.trigger
[INFO] Executing fontconfig-2.12.1-r0.trigger
[INFO] Executing mkfontscale-1.0.7-r2.trigger
[INFO] Executing mkfontdir-1.0.5-r1.trigger
[INFO] OK: 138 MiB in 76 packages
[INFO] Removing intermediate container d55e4e836de2
[INFO] ---> 378fd0858cfd
[INFO] Step 7/9 : EXPOSE 8003
[INFO]
[INFO] ---> Running in e41dc6dbc824
[INFO] Removing intermediate container e41dc6dbc824
[INFO] ---> 92de1fae51a0
[INFO] Step 8/9 : ADD ${JAR_FILE} /app.jar
[INFO]
[INFO] ---> 206b5a58cf99
[INFO] Step 9/9 : ENTRYPOINT ["java","-jar","-Duser.timezone=Asia/Shanghai","/app.jar"]
[INFO]
[INFO] ---> Running in 4b7d1f835beb
[INFO] Removing intermediate container 4b7d1f835beb
[INFO] ---> 313aa092c1b7
[INFO] Successfully built 313aa092c1b7
[INFO] Successfully tagged 8.142.11.11:8099/leway-cloud/leway-cloud-module-rabbitmq:1.0.0
[INFO]
[INFO] Detected build of image with id 313aa092c1b7
[INFO] Building jar: D:\Learning\Code\Java\leway-cloud\leway-cloud-module\leway-cloud-module-rabbitmq\target\leway-cloud-module-rabbitmq-1.0.0-docker-info.jar
[INFO] Successfully built 8.142.11.11:8099/leway-cloud/leway-cloud-module-rabbitmq:1.0.0
使用mvn dockerfile:push
可推送镜像到harbor仓库。
[INFO] --- dockerfile-maven-plugin:1.4.13:push (default-cli) @ leway-cloud-module-rabbitmq ---
[INFO] The push refers to repository [8.142.11.11:8099/leway-cloud/leway-cloud-module-rabbitmq]
[INFO] Image adc19342964d: Preparing
[INFO] Image 9fbe15911047: Preparing
[INFO] Image ceaf9e1ebef5: Preparing
[INFO] Image 9b9b7f3d56a0: Preparing
[INFO] Image f1b5933fe4b5: Preparing
[INFO] Image f1b5933fe4b5: Pushing
[INFO] Image 9fbe15911047: Pushing
[INFO] Image 9b9b7f3d56a0: Pushing
[INFO] Image 9b9b7f3d56a0: Pushed
[INFO] Image adc19342964d: Pushing
[INFO] Image ceaf9e1ebef5: Pushing
[INFO] Image f1b5933fe4b5: Pushed
[INFO] Image 9fbe15911047: Pushed
[INFO] Image ceaf9e1ebef5: Pushed
[INFO] Image adc19342964d: Pushed
[INFO] 1.0.0: digest: sha256:dbf3026879aa7480a36df40cf3a742ca6500ee09eb80d83754ea45dc1e9a3490 size: 1371