docker部署nginx并使外网能访问

一、docker启动

1、方案一(不建议使用)

bash 复制代码
# 运行提示WARNING: Published ports are discarded when using host network mode
docker run -d --name nginx -p 80:80 --net host nginx

2、方案二

bash 复制代码
# 启动容器,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启动
bash 复制代码
配置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
相关推荐
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特3 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
分布式存储与RustFS3 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
玉&心3 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
赵文宇(温玉)3 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang3 天前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特3 天前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe3 天前
docker常见命令
docker
正经教主3 天前
【FDE系列】阶段2:Day 43:Docker Compose — 多容器一键编排
人工智能·docker·fde