Docker 安装 Nginx 容器 (完整详细版)

说明:

Docker如果想安装软件 , 必须先到 Docker镜像仓库下载镜像。

Docker官方镜像

1、寻找Nginx镜像

2、下载Nginx镜像

命令描述docker pull nginx下载最新版Nginx镜像 (其实此命令就等同于 : docker pull nginx:latest )docker pull nginx:xxx下载指定版本的Nginx镜像 (xxx指具体版本号)

检查当前所有Docker下载的镜像

# 查询docker镜像

docker images

3、创建Nginx配置文件

启动前需要先创建Nginx外部挂载的配置文件( /opt/nginx/conf/nginx.conf) 之所以要先创建 , 是因为Nginx本身容器只存在/etc/nginx 目录 , 本身就不创建 nginx.conf 文件 当服务器和容器都不存在 nginx.conf 文件时, 执行启动命令的时候 docker会将nginx.conf 作为目录创建 , 这并不是我们想要的结果

# 创建挂载目录

mkdir -p /opt/nginx/conf

mkdir -p /opt/nginx/log

mkdir -p /opt/nginx/html

# 进入创建的文件夹中查看是否成功

cd /opt/nginx

容器中的nginx.conf文件和conf.d文件夹复制到宿主机

# 生成容器

docker run --name nginx -p 9000:80 -d nginx

# 将容器nginx.conf文件复制到宿主机

docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf

# 将容器conf.d文件夹下内容复制到宿主机

docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf/conf.d

# 将容器中的html文件夹复制到宿主机

docker cp nginx:/usr/share/nginx/html /opt/nginx/

# 进入创建的文件夹中查看是否成功

cd /opt/nginx

4、创建Nginx容器并运行

Docker 创建Nginx容器

# 直接执行docker rm nginx或者以容器id方式关闭容器

# 找到nginx对应的容器id

docker ps -a

# 关闭该容器

docker stop nginx

# 删除该容器

docker rm nginx

# 删除正在运行的nginx容器

docker rm -f nginx

# 个人测试,不需要https服务的就用这个

docker run \

-p 9000:80 \

--name nginx \

-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \

-v /opt/nginx/conf/conf.d:/etc/nginx/conf.d \

-v /opt/nginx/log:/var/log/nginx \

-v /opt/nginx/html:/usr/share/nginx/html \

-d nginx:latest

# 需要通过域名访问并且强加https,就要使用SSL认证

docker run \

-p 9000:80 \

-p 443:443 \

--privileged=true \

-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \

-v /opt/nginx/html:/usr/share/nginx/html \

-v /opt/nginx/conf/conf.d:/etc/nginx/conf.d \

-v /opt/nginx/log:/var/log/nginx \

-v /opt/nginx/conf/conf.d/cert:/etc/nginx/cert \

--name nginx \

-d nginx:latest

命令描述–name nginx启动容器的名字-d后台运行,nginx启动后保持长时间启动-p 9000:80映射docker的端口,将80端口与服务器的80端口绑定-p 443:443映射docker的端口,将443端口与服务器的443端口绑定–privileged=true开启特权模式,这个可以去掉,以防万一用的-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf挂载nginx.conf配置文件-v /opt/nginx/html:/usr/share/nginx/html映射路径存储html文件夹的路径-v /opt/nginx/conf/conf.d:/etc/nginx/conf.d映射nginx配置文件的路径-v /opt/nginx/log:/var/log/nginx映射日志文件的路径-v /opt/nginx/conf/conf.d/cert:/etc/nginx/cert映射存放SSL证书的文件路径nginx:latest本地运行的版本\shell 命令换行

5、结果检测

curl 127.0.0.1:9000

6、修改内容进行展示

# 重启容器

docker restart nginx

更多精彩文章可以关注 鹏摇星海 公众号

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。