Hot Ice

记录精彩的程序人生

Anything is possible!
  menu
41 文章
0 浏览
3 当前访客
ღゝ◡╹)ノ❤️

docker软件安装-nginx

1.拉取镜像
docker pull nginx

2.创建nginx文件目录
mkdir -p /home/soft/docker/nginx
创建conf, logs和web文件夹
mkdir -p /home/soft/docker/nginx/conf
mkdir -p /home/soft/docker/nginx/logs
mkdir -p /home/soft/docker/nginx/web

3.复制配置
docker run --name nginx -d nginx
docker cp nginx:/etc/nginx/nginx.conf /home/soft/docker/nginx/conf/nginx.conf
docker rm -f nginx

4.运行
docker run -p 80:80 -v /homs/soft/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /homs/soft/docker/nginx/conf/conf.d:/etc/nginx/conf.d -v /homs/soft/docker/nginx/logs:/var/log/nginx -v /homs/soft/docker/nginx/web:/usr/share/nginx/html --name nginx -d nginx

5.测试
将web资源放到/home/soft/docker/nginx/web下,
在conf.d中新建web.conf, 内容如下:

server {
   listen       80;
   server_name  localhost;

   location / {
       root   /usr/share/nginx/html;
       index  index.html index.htm;
   }
}

#使配置生效
docker exec -it nginx nginx -s reload
#访问127.0.0.1可以看到页面则成功

6.端口号变更
(1)
docker stop nginx
docker rm nginx
docker run -p 80:80 -p newPort:newPort -v /homs/soft/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /homs/soft/docker/nginx/conf/conf.d:/etc/nginx/conf.d -v /homs/soft/docker/nginx/logs:/var/log/nginx -v /homs/soft/docker/nginx/web:/usr/share/nginx/html -v newPath:newPath --name nginx -d nginx

(2)
如果安装了防火墙 Iptables 还有个不需要修改即可端口映射的临时方法:
查看容器ID的IP地址:docker inspect 容器id | grep IPAddress
该命令获取了docker容器的在宿主机里的真实ip,然后使用宿主机的端口转发功能iptables
iptables -t nat -A DOCKER -p tcp --dport 宿主机端口 -j DNAT --to-destination 容器ip:容器端口


标题:docker软件安装-nginx
作者:炎无道
地址:https://justxy.xyz/articles/2020/11/06/1604642608776.html