docker环境配置
一.docker远程环境配置
1.1 完成docker安装
参照:centos docker 安装指南
1)卸载老版本docker文件
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2)安装yum工具包
$ sudo yum install -y yum-utils
3)设置docker镜像仓库
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
4)安装docker engine
$ sudo yum install docker-ce docker-ce-cli containerd.io
- 启动docker
$ sudo systemctl start docker
docker 安装结束。
1.2 docker配置
1)docker开启远程API
方式一:
使用vi编辑器修改docker.service文件
vi /usr/lib/systemd/system/docker.service
修改
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
为
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
方式二
在daemon.json中添加
"hosts":["tcp://0.0.0.0:2375","unix:///var/run/docker.sock"]
2)重启docker服务
$ systemctl restart docker
使用方式二需要在重启前执行:systemctl daemon-reload
3)开通防火墙
$ firewall-cmd --zone=public --add-port=2375/tcp --permanentfirewall-cmd --reload
1.3 Docker支持http上传镜像
不上传可不进行配置
1)在daemon.json中添加
"insecure-registries":["192.168.0.43:8082"]
2)重启docker服务
$ systemctl daemon-reload
$ systemctl restart docker
二、本地环境配置
2.1 window系统添加环境变量
2.2 pom.xml引入插件
- dockerfile-maven-plugin
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.sinocontrol</groupId>
<artifactId>hello</artifactId>
<version>0.0.2</version>
<name>hello</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- 私服地址-->
<docker.image.prefix>192.168.0.43:8082</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<!-- dockerfile:build 构建docker镜像-->
<goal>build</goal>
<!-- dockerfile:push 上传至私服-->
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!--私服用户名-->
<username>docker</username>
<!--私服用户名-->
<password>123456</password>
<!--私服连接地址-->
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
2)编写Dockerfile
可参考:Dockerfile指南
FROM java:8
ARG JAR_FILE
ADD target/${JAR_FILE} /app.jar
ENTRYPOINT ["java", "-jar","/app.jar"]
3)使用mvn clean package进行镜像打包上传