diff --git a/2024/08/votesystem_docker.md b/2024/08/votesystem_docker.md new file mode 100644 index 0000000..a25881f --- /dev/null +++ b/2024/08/votesystem_docker.md @@ -0,0 +1,71 @@ + +> 为什么做此?因为真的有人用的此项目,会来联系我问问题,所以可以考虑部署运行方便。 + +docker需要配置代理 + +## Dockerfile + +首先就是通过编写Dockerfile来构建image, +内部就是通过maven构建jar包来运行。 + +maven拉镜像的时候可能也需要代理,或者换源,所以我把maven中的settings.xml配置copy到image里 则可以在里面配置源,设置代理等等。 + +```Dockerfile +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 + +```yaml +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: + +``` diff --git a/README.md b/README.md index 79e33b6..42ca266 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ The best thing to do is have fun. - [Hobby Projects](/hobby_projects.md) - **2024-08** - - [6.5840 Lab 2: Key/Value Server](/2024/08/mit65840_lab2.md) + - [为5年前的web项目添加docker构建支持](/2024/08/votesystem_docker.md) + - [6.5840 Lab 2: Key/Value Server](/2024/08/mit65840_lab2.md) - [数据库中的Query Execution](/2024/08/cmu15445_p2.md) - [用ts类型系统,实现Rule110(在编译时生成)](/2024/08/ts_type_system_rule110.md) - [Raylib CGo Demo](/2024/08/raylib_cgo_demo.md)