Draymonders / Code-Life

The marathon continues though.
27 stars 3 forks source link

springboot项目打包踩坑 #87

Open Draymonders opened 4 years ago

Draymonders commented 4 years ago

打jar包的命令

然后新写的一个Springboot程序,需要打成jar包, 用的spring-boot-maven-plugin插件,遇到了Jdk版本不对No main class的坑, 最终成功的打包pom.xml写法如下

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.7.RELEASE</version>
    <executions>
        <execution>
        <goals>
            <goal>repackage</goal>
        </goals>
        </execution>
    </executions>
    </plugin>
</plugins>
</build>
Draymonders commented 4 years ago

Dockerfile

FROM adoptopenjdk/openjdk11:alpine-slim

VOLUME /tmp

ENV TimeZone=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone

EXPOSE 2333

ENTRYPOINT ["java", "-jar", "/usr/share/redis-time-out/sturnus.jar"]

ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/redis-time-out/sturnus.jar

打成docker image

docker build --build-arg JAR_FILE=sturnus-v0.1.jar -t sturnus .

如果打包有问题的话,需要删除掉,用下面的命令

docker images | grep sturnus | awk '{ print $3 }' | xargs docker rmi
Draymonders commented 4 years ago

运行docker容器

docker run --name sturnus-redis-timeout --env REDIS_HOST="xx.xx.xx.xx" -d sturnus:latest

查看log内容

docker ps -a | grep sturnus-redis-timeout | awk '{ print $1 }' | xargs docker logs --tail=100

如果运行出错,可以删除容器,重新运行

docker ps -a | grep sturnus-redis-timeout | awk '{ print $1 }' | xargs docker rm -f
Draymonders commented 4 years ago

利用 https://github.com/Draymonders/docker-labs 构建基础环境。

Draymonders commented 4 years ago

将镜像持久化成文件,发送到server上

docker save sturnus > sturnus.tar

在server上, 加载镜像

docker load < sturnus.tar
Draymonders commented 4 years ago

又踩到坑了,发现外网并不能访问服务

expose

network_mode

我理解: host模式,直接用主机的网络设施; bridge模式,用的软件桥接,建立了宿主机port到容器port的映射

Draymonders commented 4 years ago

最后确定的是

services: redis: image: redis restart: always ports: