Docker 安装 Nginx 并挂载目录的实现示例

Docker 安装 Nginx 并挂载目录的实现示例

1.拉取镜像

docker pull nginx

2.创建容器

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

Bash 执行 docker ps 查看

3.创建挂载目录

为什幺要创建挂载目录?因为docker容器内部有自己的文件系统,在主机创建挂载目录并关联 Nginx 容器的配置目录后,以后可以通过修改主机下的配置目录文件来直接影响到容器内的配置。

mkdir -p /data/nginx/{conf,log,html}

把 Nginx 容器中的文件复制到主机

docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.confdocker cp nginx:/etc/nginx/conf.d /data/nginx/conf/conf.ddocker cp nginx:/usr/share/nginx/html /data/nginx/

4.停止并删除 Nginx 容器

执行 docker stop + 容器ID的前两位

docker stop 6adocker rm 6a

5.重新创建容器

docker run -d --name nginx -p 80:80 \-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \-v /data/nginx/conf/conf.d:/etc/nginx/conf.d \-v /data/nginx/log:/var/log/nginx \-v /data/nginx/html:/usr/share/nginx/html \--privileged=true nginx

执行docker ps 查看运行状态,如果启动失败执行 docker logs + 容器ID前两位 查看报错。

6.验证

curl localhost

出现以下内容说明安装成功

!DOCTYPE html html head title Welcome to nginx! /title style html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; } /style /head body h1 Welcome to nginx! /h1 p If you see this page, the nginx web server is successfully installed andworking. Further configuration is required. /p p For online documentation and support please refer to a href="http://nginx.org/" rel="external nofollow" nginx.org /a . br/ Commercial support is available at a href="http://nginx.com/" rel="external nofollow" nginx.com /a . /p p em Thank you for using nginx. /em /p /body /html

或者打开浏览器输入服务器 IP 验证

Docker 安装 Nginx 并挂载目录的实现示例