Docker搭建Nginx

1 查找 Docker Hub 上的 nginx 镜像

docker search nginx

 

2 这里我们拉取官方的镜像

docker pull nginx

 

3 等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

docker images nginx

 

4 以下命令使用 NGINX 默认的配置来启动一个 Nginx 容器实例:

 docker run --name runoob-nginx-test -p 8081:80 -d nginx
  • runoob-nginx-test 容器名称。
  • the -d设置容器在在后台一直运行。
  • the -p 端口进行映射,将本地 8081 端口映射到容器内部的 80 端口。

执行以上命令会生成一串字符串,类似 1dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,这个表示容器的 ID,一般可作为日志的文件名。

 

5 我们可以使用 docker ps 命令查看容器是否有在运行:

docker ps

 

6 PORTS 部分表示端口映射,本地的 8081 端口映射到容器内部的 80 端口。

在浏览器中打开 http://127.0.0.1:8081/,效果如下:

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.

 

 第一次运行上面的容器可以把容器内部默认的配置文件复制出来,然后后面使用他的默认配置文件。

 

 

 

nginx 部署

首先,创建目录 nginx, 用于存放后面的相关东西

mkdir -p /opt/nginx/html /opt/nginx/logs /opt/nginx/conf /opt/nginx/ssl

 

拷贝容器内 Nginx 默认配置文件和静态文件到本地创建的挂载目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

docker cp 88a5cb9e51b2:/etc/nginx/nginx.conf /opt/nginx/conf    //把默认的配置文件也拷贝过来,方便直接使用
docker cp 88a5cb9e51b2:/etc/nginx/conf.d /opt/nginx/conf/conf.d    //把默认的配置文件也拷贝过来,方便直接使用
docker cp 88a5cb9e51b2:/usr/share/nginx/html/index.html /opt/nginx/html //把默认的静态文件也拷贝过来,方便直接使用
docker cp 88a5cb9e51b2:/usr/share/nginx/html/50x.html /opt/nginx/html  //把默认的静态文件也拷贝过来,方便直接使用
  • html: 目录将映射为 nginx 容器配置的虚拟目录。
  • logs: 目录将映射为 nginx 容器的日志目录。
  • conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
  • ssl: 目录为后面作https配置用,为可选项。

部署命令

docker run -d -p 80:80 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx


如果加上ssl配置
docker run -d -p 80:80 -p 443:443 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d -v /opt/nginx/ssl:/etc/nginx/ssl nginx


  • -p 80:80: 将容器的 80 端口映射到主机的 80 端口。

  • -p 443:443: 将容器的 443 端口映射到主机的 443 端口。(可选项,配置https时加上)
  • --name nginx:将容器命名为 nginx。

  • -v /opt/nginx/html:/usr/share/nginx/html:将我们自己创建的 html目录挂载到容器的 /usr/share/nginx/html。

  • -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。

  • -v /opt/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。

  • -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d: 将conf.d目录挂载,后面进行二级域名反向代理映射端口时用,不配做这个后面配置不生效
  • -v /opt/nginx/ssl:/etc/nginx/ssl:将我们自己创建的 ssl目录 挂载到容器的 /etc/nginx/ssl,此项为可选项,要使用https证书时加上。

 

在浏览器中打开 http://127.0.0.1:80/,效果如下:

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.

88a5cb9e51b2
posted @ 2019-12-14 00:55  panchanggui  阅读(4092)  评论(0编辑  收藏  举报