Docker打包springboot工程镜像
Docker创建、运行微服务镜像
- 本地Maven添加Docker插件往本地Docker上创建、运行镜像
本地环境要求:
Maven
Java
Docker
1)pom添加docker plugin
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
<docker.maven.plugin.version> 1.2.2</docker.maven.plugin.version>
</properties>
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.maven.plugin.version}</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- Docker maven plugin -->
2)添加Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD services-2.0.jar services-2.0.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Xms200m","-Xmx1000m","-jar","/services-2.0.jar"]
3)mvn package 打包
4)Docker 打包并往本地Docker上生成镜像文件,在Idea Maven插件Plugins点击docker:build即可。在本地target/docker/ 下生成两个文件一个Dockerfile和services-2.0.jar
本地Docker上的镜像
5)上传docker下Jar包、Dockerfile到服务器
6)Dockerfile和jar包在同一目录下(我这里创建了一个docker文件夹放该两个文件)
7)Docker创建images
创建镜像docker build -t springboot/services ./
查看镜像docker images
8)Docker运行镜像
运行镜像docker run --add-host test01:192.168.211.106 --add-host test02:192.168.211.107 --add-host test03:192.168.211.108 --add-host test04:192.168.211.109 -d -p 8888:8888 --name services springboot/services
9)Docker查看镜像运行日志
10)Postman查询结果
11)consul上查看注册的服务
Ps: 如果本地不启动Docker Desktop,则会在docker:build阶段报错
Failed to execute goal com.spotify:docker-maven-plugin:1.2.2:build (default-cli) on project services: Exception caught
2. Maven打Jar包Docker创建、运行微服务镜像
服务器环境要求:
Maven
Java
Docker
1)本地maven工程打包
2)上传Jar包到服务器
3)创建Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD services-2.0.jar services-2.0.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Xms200m","-Xmx1000m","-jar","/services-2.0.jar"]
4)Dockerfile和jar包在同一目录下(我这里创建了一个docker文件夹放该两个文件)
5)Docker创建images
创建镜像docker build -t springboot/services ./
查看镜像docker images
6)Docker运行镜像
运行镜像docker run --add-host test01:192.168.211.106 --add-host test02:192.168.211.107 --add-host test03:192.168.211.108 --add-host test04:192.168.211.109 -d -p 8888:8888 --name services springboot/services
7)Docker查看镜像运行日志
8)Postman查询结果
9)consul上查看注册的服务
遇到的问题以及解决方式:
1)接下来我们将使用docker-maven-plugin来打包Docker镜像,使用的代码为原来的mall-tiny-docker例子。
直接使用docker-maven-plugin打包试试,由于我们的插件版本有点低,使用新一点版本的Docker会出现如下问题,升级到1.2.2版本解决该问题;
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.1.0:build (build-image) on project mall-tiny-docker: Exception caught: com.spotify.docker.client.shaded.com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.spotify.docker.client.messages.RegistryAuth (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘desktop’)
[ERROR] at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.LinkedHashMap[“credsStore”])
[ERROR] -> [Help 1]
2)
Sending build context to Docker daemon 93.29MB
Step 1/4 : FROM openjdk:8-jdk-alpine
8-jdk-alpine: Pulling from library/openjdk
e7c96db7181b: Retrying in 1 second
f910a506b6cb: Retrying in 1 second
c2274a1a0e27: Retrying in 1 second
error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/a3/a3562aa0b991a80cfe8172847c8be6dbf6e46340b759c2b782f8b8be45342717/data?verify=1605067506-ew8bsuvRgcYcg%2BSayXF9Rsjzvqg%3D: dial tcp 104.18.122.25:443: i/o timeout
[[email protected] docker]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[[email protected] docker]$ docker build -t="springboot/services" .
Sending build context to Docker daemon 93.29MB
Step 1/4 : FROM openjdk:8-jdk-alpine
Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:43696->[::1]:53: read: connection refused
[[email protected] docker]$ docker build -t springboot/services ./
Sending build context to Docker daemon 93.29MB
Step 1/4 : FROM openjdk:8-jdk-alpine
8-jdk-alpine: Pulling from library/openjdk
e7c96db7181b: Pulling fs layer
f910a506b6cb: Pulling fs layer
c2274a1a0e27: Pulling fs layer
error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/a3/a3562aa0b991a80cfe8172847c8be6dbf6e46340b759c2b782f8b8be45342717/data?verify=1605068492-zUNWHyIDz9TVwIWVqUZ%2BH9FROAA%3D: dial tcp 104.18.124.25:443: i/o timeout
[[email protected] docker]$
修改DNS或者镜像源
3)
docker启动镜像时Warn:
WARNING: IPv4 forwarding is disabled. Networking will not work.
解决方式:
启动镜像,发现没有网络,而且不能ifconfig,当时就淡腾了后来查找资料发现,是没有开启转发,网桥配置完后,
需要开启转发,不然容器启动后,就会没有网络,配置/etc/sysctl.conf,添加net.ipv4.ip_forward=1