一、准备
- 运行系统:centos7 64位
- docker-ce(社区版):18.09.3
二、安装docker和搭建私有镜像仓库
1、搭建docker
第一步:更新yum包
yum update
第二步:移除原有的旧的docker程序
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
第三步:安装docker所依赖的一些安装程序
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
第四步:配置docker稳定的仓库地址
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
第五步:安装docker ce和容器
yum install docker-ce docker-ce-cli containerd.io
第六步:开放docker远程端口
①修改docker.service内容,配置远程访问
vim /usr/lib/systemd/system/docker.service
增加如下内容:
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
②将管理地址写入/etc/profile并生效
echo 'export DOCKER_HOST=tcp://0.0.0.0:2375' >> /etc/profile
source /etc/profile
③docker重新读取配置文件,重启docker服务
systemctl daemon-reload
systemctl restart docker
④查看系统的网络端口,是否是2375监听docker守护进程
netstat -tulp
2、搭建私有镜像仓库
第一步:启动docker私有镜像库镜像,从docker镜像库获取
docker run -d -p 5000:5000 --restart=always --name=registry \
-v /mnt/registry:/var/lib/registry \
registry
第二步:配置客户端访问本地私有镜像库使用http
①创建daemon.json文件,配置使用http访问本地私有镜像库
echo '{ "insecure-registries":["192.168.0.105:5000"] }' > /etc/docker/daemon.json
②重启docker
systemctl restart docker
三、spring boot应用封装并推送到私有镜像库
第一步:创建spring boot应用,并在src/main/resources下创建一个Dockerfile文件,内容如下:
FROM java
MAINTAINER "sxp<1378127237@qq.com>"
ADD demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8090
CMD java -jar app.jar
第二步:在pom文件中配置docker-maven插件,内容如下:
<!-- 镜像构建 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${docker.registry}/${project.artifactId}:${project.version}</imageName>
<dockerDirectory>${project.build.outputDirectory}</dockerDirectory>
<dockerHost>http://192.168.0.105:2375</dockerHost>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
第三步:mvn clean package,就会打包应用并构建镜像和推送到私有镜像库中
c3fe59dd9556: Pushing [================================================> ] 341.1MB/351.5MB
a2ae92ffcd29: Pushing [================================================> ] 118.5MB/123MB
c3fe59dd9556: Pushing [================================================> ] 341.7MB/351.5MB
a2ae92ffcd29: Pushing [================================================> ] 119MB/123MB
a2ae92ffcd29: Pushing [================================================> ] 119.5MB/123MB
a2ae92ffcd29: Pushing [=================================================> ] 120.6MB/123MB
a2ae92ffcd29: Pushing [=================================================> ] 121.1MB/123MB
a2ae92ffcd29: Pushing [=================================================> ] 121.6MB/123MB
a2ae92ffcd29: Pushing [=================================================> ] 122.2MB/123MB
a2ae92ffcd29: Pushing [=================================================> ] 122.7MB/123MB
a2ae92ffcd29: Pushing [==================================================>] 123.2MB
a2ae92ffcd29: Pushing [==================================================>] 123.7MB
a2ae92ffcd29: Pushing [==================================================>] 124.3MB
a2ae92ffcd29: Pushing [==================================================>] 124.8MB
c3fe59dd9556: Pushing [================================================> ] 342.2MB/351.5MB
c3fe59dd9556: Pushing [================================================> ] 342.7MB/351.5MB
c3fe59dd9556: Pushing [================================================> ] 343.2MB/351.5MB
c3fe59dd9556: Pushing [================================================> ] 343.8MB/351.5MB
c3fe59dd9556: Pushing [=================================================> ] 344.9MB/351.5MB
a2ae92ffcd29: Pushing [==================================================>] 125.9MB
c3fe59dd9556: Pushing [=================================================> ] 346MB/351.5MB
a2ae92ffcd29: Pushing [==================================================>] 126.4MB
c3fe59dd9556: Pushing [=================================================> ] 346.5MB/351.5MB
a2ae92ffcd29: Pushing [==================================================>] 126.9MB
c3fe59dd9556: Pushing [=================================================> ] 347.6MB/351.5MB
c3fe59dd9556: Pushing [=================================================> ] 348.7MB/351.5MB
c3fe59dd9556: Pushing [=================================================> ] 349.8MB/351.5MB
a2ae92ffcd29: Pushing [==================================================>] 127.5MB
c3fe59dd9556: Pushing [=================================================> ] 350.4MB/351.5MB
a2ae92ffcd29: Pushing [==================================================>] 128MB
a2ae92ffcd29: Pushing [==================================================>] 128.6MB
a2ae92ffcd29: Pushing [==================================================>] 128.9MB
c3fe59dd9556: Pushing [=================================================> ] 350.9MB/351.5MB
c3fe59dd9556: Pushing [=================================================> ] 351.5MB/351.5MB
c3fe59dd9556: Pushing [==================================================>] 352MB
c3fe59dd9556: Pushing [==================================================>] 352.5MB
c3fe59dd9556: Pushing [==================================================>] 353.6MB
c3fe59dd9556: Pushing [==================================================>] 354.7MB
c3fe59dd9556: Pushing [==================================================>] 355.7MB
a2ae92ffcd29: Pushed
c3fe59dd9556: Pushing [==================================================>] 356.7MB
c3fe59dd9556: Pushed
0.0.1-SNAPSHOT: digest: sha256:bba655036c13ffe43429d8cbcb3381b622fd950962d42f335d41a896620c4772 size: 2212
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:48 min
[INFO] Finished at: 2019-03-24T15:29:51+08:00
[INFO] ------------------------------------------------------------------------