为5年前的web项目添加docker构建支持

· Technology

为什么做此?因为真的有人用的此项目,会来联系我问问题,所以可以考虑部署运行方便。

docker 需要配置代理

Vote System

Dockerfile

首先就是通过编写 Dockerfile 来构建 image,
内部就是通过 maven 构建 jar 包来运行。

maven 拉镜像的时候可能也需要代理,或者换源,所以我把 maven 中的 settings.xml 配置 copy 到 image 里 则可以在里面配置源,设置代理等等。

FROM maven:3.8.5-openjdk-11 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY settings.xml /usr/share/maven/conf/settings.xml
RUN mvn clean package -DskipTests -X
 
 
FROM openjdk:11-jdk
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
 

Docker-compose

因为需要等待 mysql 启动完成后再启动 service,否则 service 连接不上 mysql 所以需要加上 health check

version: "3.8"
 
services:
  db:
    image: mysql:5.7
    container_name: mysql-db
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: vote
      MYSQL_USER: ultraman
      MYSQL_PASSWORD: 123456
    volumes:
      - db_data:/var/lib/mysql
      - ./vote.sql:/docker-entrypoint-initdb.d/vote.sql
    ports:
      - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p123456"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
 
  app:
    build: .
    container_name: vote-system-app
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "8080:8080"
 
volumes:
  db_data:

Comments (0)

    Send comment

    Markdown supported. Please keep comments clean.