一、安装

1. 通过Docker安装Nginx

1. 拉取镜像

  1. docker pull nginx:1.10

2. 创建测试容器

  1. 创建测试容器

    1. docker run \
    2. --name nginx110 \
    3. -p 80:80 \
    4. -d nginx:1.10
  2. 查看容器运行状态

    1. docker ps

    image.png

  3. 访问Nginx

使用ip:80访问Nginx,出现以下页面,容器启动成功:
image.png

  1. 查看配置文件、静态资源、日志所在目录

    1. 进入容器

      1. docker exec -it nginx110 bash
    2. 查看Nginx配置

      1. cat /etc/nginx/nginx.conf

      ```bash user nginx; worker_processes 1;

error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;

events { worker_connections 1024; }

http { include /etc/nginx/mime.types; default_type application/octet-stream;

  1. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '
  3. '"$http_user_agent" "$http_x_forwarded_for"';
  4. access_log /var/log/nginx/access.log main;
  5. sendfile on;
  6. #tcp_nopush on;
  7. keepalive_timeout 65;
  8. #gzip on;
  9. include /etc/nginx/conf.d/*.conf;

}

  1. 找出以下需挂载目录:
  2. - 日志文件路径:`/var/log/nginx`
  3. - 配置文件路径:`/etc/nginx`
  4. - 静态资源路径:`/usr/share/nginx/html`
  5. 5. 在宿主机创建挂载目录
  6. ```bash
  7. mkdir /opt/docker/nginx
  8. mkdir /opt/docker/nginx/config
  9. mkdir /opt/docker/nginx/data
  10. mkdir /opt/docker/nginx/logs
  1. 将配置文件、静态资源、日志所在目录拷贝至宿主机

    1. docker cp nginx110:/etc/nginx /opt/docker/nginx/config/
    2. docker cp nginx110:/usr/share/nginx/html /opt/docker/nginx/data/
    3. docker cp nginx110:/var/log/nginx /opt/docker/nginx/logs/
  2. 删除测试容器

    1. docker rm -f nginx110

    3. 创建容器

    1. docker run \
    2. --name nginx110 \
    3. -p 80:80 \
    4. --mount type=bind,src=/opt/docker/nginx/config/nginx/,dst=/etc/nginx \
    5. --mount type=bind,src=/opt/docker/nginx/data/html/,dst=/usr/share/nginx/html \
    6. --mount type=bind,src=/opt/docker/nginx/logs/nginx/,dst=/var/log/nginx \
    7. -d nginx:1.10

    查看容器状态,并访问ip:80,检查容器运行状态:

    1. docker ps

    image.png
    image.png
    修改Nginx的默认首页,目录/opt/docker/nginx/data/html下的index.html,确认目录挂载状态: ```bash <!DOCTYPE html>

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.666

``` 出现修改内容,挂载成功:
image.png