Docker 基础教程

Docker 安装

Docker 使用

Docker 实例

Docker 参考手册

Docker 安装 Nginx


Docker 安装 Nginx 主要有 2 种方法,分别是直接通过 docker 命令安装或通过 Dockerfile 构建。nginx 相关的 docker 镜像所有版本可以查看镜像库的 nginx tags,官方地址为 https://hub.docker.com/_/nginx?tab=tags

方式一:docker pull nginx

关键字搜索相关镜像

首先通过 nginx 关键字查询当前镜像库的所有 nginx 相关的镜像,使用 docker search 命令,具体示例如下:

knowledgedict@Mac ~ $ docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        13574     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1856                 [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   780                  [OK]
linuxserver/nginx                  An Nginx container, brought to you by Linu...   125                  
bitnami/nginx                      Bitnami nginx Docker Image                      88                   [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   85                   [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   76                   
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from s...   72                   [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page ...   58                   [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        53                   [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         37                   
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & ...   31                   [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP t...   18                   [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  16                   
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       14                   
centos/nginx-18-centos7            Platform for running nginx 1.8 or building...   13                   
raulr/nginx-wordpress              Nginx front-end for the official wordpress...   13                   [OK]
centos/nginx-112-centos7           Platform for running nginx 1.12 or buildin...   13                   
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                   [OK]
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats...   7                    [OK]
mailu/nginx                        Mailu nginx frontend                            7                    [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a...   7                    
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Con...   6                    [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
wodby/nginx                        Generic nginx                                   1                    [OK]

拉取指定版本的镜像

从镜像库拉取镜像用 docker pull 命令。

knowledgedict@Mac ~ $ docker pull nginx:1.19.1
1.19.1: Pulling from library/nginx
bf5952930446: Already exists 
ba755a256dfe: Pull complete 
c57dd87d0b93: Pull complete 
d7fbf29df889: Pull complete 
1f1070938ccd: Pull complete 
Digest: sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2
Status: Downloaded newer image for nginx:1.19.1

下载完成后,可以在本地的镜像列表查看相关信息,如下:

knowledgedict@Mac ~ $ docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.19.1              08393e824c32        2 days ago          132 MB

运行容器

运行容器使用 docker run 命令,具体如下:

knowledgedict@Mac ~ $ docker run -itd --name nginx-demo -p 80:80 nginx:1.19.1
6355f5f66fdd495aa22de56bb786756fc4b4b55b969cab4717401bd91e8bad0a

启动后,查看相应容器运行信息:

knowledgedict@Mac ~ $ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6355f5f66fdd        nginx:1.19.1        "/docker-entrypoin..."   10 seconds ago      Up 11 seconds       0.0.0.0:80->80/tcp   nginx-demo