配置TCP远程连接(docker-maven-plugin插件连接的地址)
# 加上红色标识的部分
[root@localhost admin]# vim /lib/systemd/system/docker.service [Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com BindsTo=containerd.service After=network-online.target firewalld.service containerd.service Wants=network-online.target Requires=docker.socket [Service] Type=notify # the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not support the cgroup feature set required # for containers run by docker ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=always # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229. # Both the old, and new location are accepted by systemd 229 and up, so using the old location # to make them work for either version of systemd. StartLimitBurst=3 # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make # this option work for either version of systemd. StartLimitInterval=60s # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Comment TasksMax if your systemd version does not support it. # Only systemd 226 and above support this option. TasksMax=infinity # set delegate yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process [Install] WantedBy=multi-user.target
开放2375端口
# 开启防火墙端口 firewall-cmd --zone=public --add-port=2375/tcp --permanent systemctl restart firewalld
配置私有仓库
配置daemon.json文件
[root@localhost admin]# vim /etc/docker/daemon.json { "insecure-registries":["192.168.192.128:443"] }
# 重启服务 systemctl daemon-reload systemctl restart docker
然后下载registry镜像
[root@localhost admin]# docker pull registry [root@localhost admin]# mkdir /usr/docker_registry_data [root@localhost admin]# docker run -d -p 443:5000 -v /usr/docker_registry_data:/var/lib/registry registry # 开启防火墙端口 firewall-cmd --zone=public --add-port=443/tcp --permanent systemctl restart firewalld
新建SpringBoot工程
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.0</version> <configuration> <!--Docker要求推送的映像名称以仓库的主机名和端口为前缀。例如,要推送my-image到registry.example.com,镜像需要标记为registry.example.com/my-image--> <imageName>192.168.192.128:443/hello</imageName> <!--基础镜像--> <baseImage>java</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <!-- copy the service's jar file from target into the root directory of the image --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <forceTags>true</forceTags> <imageTags> <imageTag>latest</imageTag> </imageTags> </configuration> </plugin> </plugins> </build>
打包(注意,要保证服务器的Registry容器开启)
SET DOCKER_HOST=tcp://192.168.192.128:2375 mvn clean package -Dmaven.test.skip=true docker:build -DpushImageTag
成功
现在回来看镜像
启动(后台启动加上-d)
访问
另一种方式:Dockerfile
在src/main下新建一个docker目录
Dockerfile内容
# 基础镜像 FROM java:8 # 打包jar去向 ADD /hello-0.0.1-SNAPSHOT.jar /app.jar # 暴露端口 EXPOSE 8081 # 启动命令 ENTRYPOINT ["java", "-jar", "/app.jar"]
pom文件修改如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.0</version> <configuration> <!--Docker要求推送的映像名称以注册表的主机名和端口为前缀。例如,要推送my-image到registry.example.com,镜像需要标记为registry.example.com/my-image--> <imageName>192.168.192.128:443/hello-2</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <forceTags>true</forceTags> <imageTags> <imageTag>latest</imageTag> </imageTags> </configuration> </plugin> </plugins> </build>
打包推送
SET DOCKER_HOST=tcp://192.168.192.128:2375 mvn clean package -Dmaven.test.skip=true docker:build -DpushImageTag
运行结果
查看镜像