Docker安装Nginx

docker pull nginx

拉取Nginx官方镜像

docker images

查看本地的镜像

docker run --name nginx_latest -d nginx

先让容器在后台运行,并取个别名为nginx_latest,此时还没有修改配置文件

docker ps

查看正在运行中的容器

docker exec nginx_latest ls /etc/nginx

查看容器内部的配置文件

docker cp -a nginx_latest:/etc/nginx/ /root/nginx_latest

复制容器内部的配置文件到宿主机,不用事先创建/root/nginx_latest目录

chmod -R 777 nginx_latest

给/root/nginx_latest目录赋予最高权限

docker rm -f nginx_latest

删除nginx_latest容器,-f参数是强制删除运行中的容器

docker run -p 80:80 --restart always --name nginx_lastest -v /root/nginx_latest/:/etc/nginx/ -d nginx

重新运行容器,并挂载配置文件,映射端口

这样下次就可以直接修改宿主机的配置文件,然后docker restart nginx_latest重启容器

或者还是进入容器内部修改配置文件,那么宿主机的配置文件也会同步更新

其它诸如静态文件、日志的配置以此类推

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --reload

开放宿主机防火墙的80端口

访问

http://192.168.1.116/

原文地址:https://www.cnblogs.com/yjlch1016/p/11923695.html