Docker 安装 Nginx

Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 。

1、查看可用的 Nginx 版本

访问 Nginx 镜像库地址: https://hub.docker.com/_/nginx?tab=tags

可以通过 Sort by 查看其他版本的 Nginx,默认是最新版本 nginx:latest

此外,我们还可以用 docker search nginx 命令来查看可用版本:

changedeMBP:~ changeluo$ docker search nginx
NAME                                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                             Official build of Nginx.                        17040               [OK]
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   169
bitnami/nginx                                     Bitnami nginx Docker Image                      133                                     [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   52
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   18                                      [OK]
rancher/nginx-ingress-controller                                                                  10
clearlinux/nginx                                  Nginx reverse proxy server 
...

2、取最新版的 Nginx 镜像

这里我们拉取官方的最新版本的镜像:

$ docker pull nginx:latest

3、查看本地镜像

使用以下命令来查看是否已安装了 nginx:

$ docker images

4、运行容器

安装完成后,我们可以使用以下命令来运行 nginx 容器:

$ docker run --name nginx-test -p 8090:80 -d nginx

参数说明:

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

5、安装成功

最后我们可以通过浏览器可以直接访问 8090 端口的 nginx 服务。