Dockerfile
目录结构
app
|__app.jar
|__Dockerfile
注意:
logging.file.name=logs/app.log (使用相对路径logs目录在容器中的位置和jar包同级)
jar包名为 app.jar (如若不同,请相应的修改下面Dockerfile中的jar包名称)
FROM openjdk:17.0.1-oracle
WORKDIR /app
RUN mkdir "logs" "config"
RUN touch "config/application.yml"
VOLUME ["/app/logs","/app/config"]
COPY app.jar /app/
ENTRYPOINT ["java","-jar","-Dspring.config.additional-location=config/application.yml","app.jar"]
docker-compose.yml
version: "3.0"
# 定义服务
services:
nginx:
image: nginx:latest
ports:
- "80:80"
environment:
- "TZ=Aisa/Shanghai"
volumes:
- "nginx_conf:/etc/nginx/conf.d"
- "nginx_html:/usr/share/nginx/html"
networks:
- net
privileged: true
mysql:
image: mysql:8.0
ports:
- "3306:3306"
# 添加环境变量
environment:
- "MYSQL_ROOT_PASSWORD=123456"
volumes:
- "mysql_data:/var/lib/mysql"
- "mysql_conf:/etc/mysql/conf.d"
networks:
- net
privileged: true
redis:
image: redis:7.0
ports:
- "6379:6379"
volumes:
- "redis_data:/data"
- "redis_conf:/etc/redis/conf.d"
command: redis-server /etc/redis/conf.d/default.conf
networks:
- net
privileged: true
app:
# 构建镜像
build:
# 指定Dockerfile的目录
context: ./java/
# Dockerfile的文件名
dockerfile: Dockerfile
# 构建后的镜像名称
image: app:1.0
ports:
- "8080:8080"
volumes:
- "app_logs:/app/logs"
- "app_config:/app/config"
networks:
- net
# 防止打开文件没有权限
privileged: true
depends_on:
- mysql
#使用网络时需要声明,并创建 (带有项目名)
networks:
net:
driver: bridge
#使用数据卷时需要声明,并创建(带有项目名)
#如果使用外部已存在的数据卷,需要添加 external: true 要求:docker volume ls 可查
volumes:
nginx_conf:
external: true
nginx_html:
external: true
mysql_conf:
external: true
mysql_data:
external: true
redis_conf:
external: true
redis_data:
external: true
app_logs:
app_config: