- 环境版本信息:
jdk: 1.8
springboot: 2.3.12.RELEASE
Docker: 24.0.2
- 前提条件如下:
- 确认项目可以正常执行mvn package命令打包
- 执行java -jar xx.jar,本地访问无误
- 确认Docker已正常安装
- pom配置如下
<?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.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chint</groupId>
<artifactId>eureka_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka_server</name>
<description>Demo project for Spring Boot</description>
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</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>
</plugins>
</build>
</project>
在代码主目录main下创建docker目录,并创建Dockerfile文件内容如下:
FROM openjdk:8
EXPOSE 8761
VOLUME /tmp
ADD eureka_server-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
制作docker镜像
- 拉取openjdk镜像:docker pull openjdk:8
- 将jar包放在dockerfile同目录下
- 去到docker目录执行生成镜像命令
执行 docker build -f Dockerfile -t app:1.0 .
[+] Building 0.9s (8/8) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 265B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:8 0.0s
=> [1/3] FROM docker.io/library/openjdk:8 0.0s
=> [internal] load build context 0.4s
=> => transferring context: 51.51MB 0.4s
=> [2/3] ADD eureka_server-0.0.1-SNAPSHOT.jar app.jar 0.1s
=> [3/3] RUN sh -c 'touch /app.jar' 0.2s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:b21e3cce4088c45279f9b27c58b64389615626fc43f8b2518cc6f8625a9d06f6 0.0s
=> => naming to docker.io/library/app:1.0 0.0s
查看镜像
执行 docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
app 1.0 b21e3cce4088 About a minute ago 623MB
生成容器并启动,启动成功
执行 docker run --name app -p 8761:8761 -d app:1.0
da895f804ac17f993e3544b26b68deaa4de5c994a40480ffcfa85e5a8ce9ef44
chenjianping@chenjianpings-Laptop docker % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da895f804ac1 app:1.0 "sh -c 'java $JAVA_O…" 8 seconds ago Up 7 seconds 0.0.0.0:8761->8761/tcp app
页面访问localhost:8761正常显示
后续:打算抽时间使用Jenkins进行拉取自动打包部署到docker
吐槽:简书markdown都无法正常上传图片,不知道是哪里的问题。