一、docker启动
1、方案一(不建议使用)
# 运行提示WARNING: Published ports are discarded when using host network mode
docker run -d --name nginx -p 80:80 --net host nginx
2、方案二
# 启动容器,centOs7内核版本太低访问不了,需要像下面compose一样升级内核
docker run -d --name nginx -p 80:80 nginx
# 请求测试,内核升级后能成功
curl http://localhost:80
# 指定挂载数据卷要用绝对路径,相对路径要报错
docker run -d --name nginx -p 8090:8090 -p 8091:8091 --network test-net -v /root/data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/data/nginx/html:/usr/share/nginx/html nginx
# 请求测试,内核升级后能成功
curl http://localhost:8091
二、docker compose启动
配置yml:
version: "3.8"
services:
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
- "8090:8090"
- "8091:8091"
volumes:
- "./nginx/nginx.conf:/etc/nginx/nginx.conf"
- "./nginx/html:/usr/share/nginx/html"
networks:
- test-net
networks:
test-net:
name: test-net
# 需要预创建volumes需要挂载的nginx.conf文件在对应目录下(/预备文件/nginx复制)
# 启动多容器
docker compose -f ./env-compose.yml up -d
# 请求测试,请求不通报错,原因是环境使用的centOs7内核3.10,内核版本太低需要升级
# 解决方法参考以下博客:
# https://www.cnblogs.com/xzkzzz/p/9627658.html
curl http://localhost:80
shutdown -r -t 3 # 3秒后重启
# 如果改了nginx.conf,需要重启容器
docker compose -f ./env-compose.yml restart nginx