企业购物网站开发,学校网站搭建,wordpress自适应插件,长沙做网站的有哪些项目背景#xff1a;这是一个SpringBootvue3的项目#xff0c;通过maven打包后#xff0c;需要在Linux服务器上部署#xff0c;本篇博客主要记录docker-compose.yaml文件的含义#xff1a;
docker-compose.yml 文件中定义了一个 algorithm_platform_frontend 容器#…项目背景这是一个SpringBootvue3的项目通过maven打包后需要在Linux服务器上部署本篇博客主要记录docker-compose.yaml文件的含义
docker-compose.yml 文件中定义了一个 algorithm_platform_frontend 容器该容器基于 nginx:latest 镜像并通过数据卷 (volumes) 绑定了 前端静态文件 和 Nginx 配置文件。下面详细解析各个参数的作用以及 :z 选项的含义。 1. docker-compose.yml 解析
algorithm_platform_frontend:image: nginx:latestcontainer_name: algorithm_platform_frontendhostname: algorithm_platform_frontendrestart: unless-stoppedvolumes:- ./frontend/html:/usr/share/nginx/html:z- ./frontend/nginx.conf:/etc/nginx/nginx.conf:zports:- 8000:80networks:- algorithm_platform_network参数解析
参数作用image: nginx:latest使用官方 nginx 最新版镜像container_name: algorithm_platform_frontend指定容器名称方便管理hostname: algorithm_platform_frontend设置容器的主机名restart: unless-stopped容器异常退出时自动重启但如果手动停止则不会重启volumes挂载数据卷绑定前端文件和 Nginx 配置文件ports映射端口宿主机 8000 - 容器 80外部访问 http://localhost:8000networks指定容器加入 algorithm_platform_network 网络 2. volumes 详解
volumes:- ./frontend/html:/usr/share/nginx/html:z- ./frontend/nginx.conf:/etc/nginx/nginx.conf:z挂载的路径 ./frontend/html:/usr/share/nginx/html:z 作用将宿主机 ./frontend/html 目录即前端 HTML 代码挂载到 Nginx 的 /usr/share/nginx/html使 Nginx 直接读取宿主机的静态资源。效果修改 ./frontend/html 里的文件后Nginx 自动加载更新无需重新构建镜像。 ./frontend/nginx.conf:/etc/nginx/nginx.conf:z 作用将 nginx.conf 配置文件挂载到容器的 /etc/nginx/nginx.conf自定义 Nginx 配置。 3. :z 选项的作用
在 volumes 挂载路径的末尾:z 选项是 SELinux 相关的设置适用于启用了 SELinux如 CentOS、RHEL的环境。
:z 的作用
改变 SELinux 上的标签让容器能访问宿主机文件。SELinux 默认会阻止 Docker 访问 非 docker 目录的文件:z 选项会自动添加 共享访问权限。
是否需要 :z
如果使用 CentOS / RHEL而且 SELinux 开启那么需要 :z 选项。如果是 Ubuntu / Debian / macOS / Windows则不需要 :z可以去掉。 示例去掉 :z
volumes:- ./frontend/html:/usr/share/nginx/html- ./frontend/nginx.conf:/etc/nginx/nginx.conf如果你的环境 没有 SELinux可以去掉 :z避免不必要的权限修改。 4. 启动容器 启动 docker-compose
docker-compose up -d-d后台运行容器访问 http://localhost:8000应该能看到你的前端页面 查看运行状态
docker ps查看日志
docker logs -f algorithm_platform_frontend更新配置后重启 如果修改了 nginx.conf 或 html 文件
docker restart algorithm_platform_frontend5. 总结
这个 docker-compose.yml 文件创建了一个 nginx 容器负责运行前端静态页面。挂载数据卷 html 目录提供前端代码nginx.conf自定义 Nginx 配置 :z 选项用于 SELinux 权限管理非 SELinux 环境可以去掉。端口映射 8000:80让外部可以通过 http://localhost:8000 访问前端。 建议
确保 ./frontend/html 目录存在否则 Nginx 可能无法启动。如果 nginx.conf 有错误可以进入容器调试docker exec -it algorithm_platform_frontend /bin/sh
cat /etc/nginx/nginx.conf
nginx -t # 测试 Nginx 配置是否正确如果你有更具体的问题欢迎找博主讨论