关于Docker运行Nginx的文章层出不穷,鄙人推荐http://www.runoob.com/docker/docker-install-nginx.html这篇文章中介绍的两种方法。在这里,我着重介绍下第二种方法吧,直接从docker Hub上拉取Nginx镜像,供我们使用。

 这里,我在虚拟机中安装了CentOS7,然后,安装docker,这一步,就不必详细介绍了。假设,你已经成功安装了docker。我这里安装的版本是1.13.1,API版本1.26
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: <unknown>
 Go version:      go1.8.3
 Git commit:      774336d/1.13.1
 Built:           Wed Mar  7 17:06:16 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: <unknown>
 Go version:      go1.8.3
 Git commit:      774336d/1.13.1
 Built:           Wed Mar  7 17:06:16 2018
 OS/Arch:         linux/amd64
 Experimental:    false

首先,创建一个目录:

mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

命令进入nginx目录。

然后,我们通过docker search命令,来检索nginx镜像

docker search nginx
INDEX       NAME                                                             DES                                                                                                                                                    CRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                                                  Off                                                                                                                                                    icial build of Nginx.                        8564      [OK]
docker.io   docker.io/jwilder/nginx-proxy                                    Aut                                                                                                                                                    omated Nginx reverse proxy for docker c...   1335                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm                                Con                                                                                                                                                    tainer running Nginx + PHP-FPM capable ...   547                  [OK]
docker.io   docker.io/jrcs/letsencrypt-nginx-proxy-companion                 Let                                                                                                                                                    sEncrypt container to use with nginx as...   368                  [OK]
docker.io   docker.io/kong                                                   Ope                                                                                                                                                    n-source Microservice & API Management ...   188       [OK]
docker.io   docker.io/webdevops/php-nginx                                    Ngi                                                                                                                                                    nx with PHP-FPM                              103                  [OK]
docker.io   docker.io/kitematic/hello-world-nginx                            A l                                                                                                                                                    ight-weight nginx container that demons...   99
docker.io   docker.io/bitnami/nginx                                          Bit                                                                                                                                                    nami nginx Docker Image                      52                   [OK]
docker.io   docker.io/zabbix/zabbix-web-nginx-mysql                          Zab                                                                                                                                                    bix frontend based on Nginx web-server ...   51                   [OK]
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubu                                                                                                                                                    ntu-16-nginx-php-phpmyadmin-mysql-5          35                   [OK]
docker.io   docker.io/linuxserver/nginx                                      An                                                                                                                                                     Nginx container, brought to you by Linu...   35
docker.io   docker.io/tobi312/rpi-nginx                                      NGI                                                                                                                                                    NX on Raspberry Pi / armhf                   19                   [OK]
docker.io   docker.io/nginxdemos/nginx-ingress                               NGI                                                                                                                                                    NX Ingress Controller for Kubernetes . ...   11
docker.io   docker.io/blacklabelops/nginx                                    Doc                                                                                                                                                    kerized Nginx Reverse Proxy Server.          9                    [OK]
docker.io   docker.io/wodby/drupal-nginx                                     Ngi                                                                                                                                                    nx for Drupal container image                9                    [OK]
docker.io   docker.io/webdevops/nginx                                        Ngi                                                                                                                                                    nx container                                 8                    [OK]
docker.io   docker.io/centos/nginx-18-centos7                                Pla                                                                                                                                                    tform for running nginx 1.8 or building...   6
docker.io   docker.io/nginxdemos/hello                                       NGI                                                                                                                                                    NX webserver that serves a simple page ...   6                    [OK]
docker.io   docker.io/1science/nginx                                         Ngi                                                                                                                                                    nx Docker images that include Consul Te...   4                    [OK]
docker.io   docker.io/centos/nginx-112-centos7                               Pla                                                                                                                                                    tform for running nginx 1.12 or buildin...   3
docker.io   docker.io/behance/docker-nginx                                   Pro                                                                                                                                                    vides base OS, patches and stable nginx...   2                    [OK]
docker.io   docker.io/pebbletech/nginx-proxy                                 ngi                                                                                                                                                    nx-proxy sets up a container running ng...   2                    [OK]
docker.io   docker.io/toccoag/openshift-nginx                                Ngi                                                                                                                                                    nx reverse proxy for Nice running on sa...   1                    [OK]
docker.io   docker.io/travix/nginx                                           NGi                                                                                                                                                    nx reverse proxy                             1                    [OK]
docker.io   docker.io/mailu/nginx                                            Mai                                

然后,通过docker pull命令,从远程拉取nginx镜像,命令如下:

docker pull nginx

拉取成功后,我们可以通过docker images命令,查看镜像:

[root@localhost nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              ae513a47849c        4 weeks ago         109 MB

然后,通过docker run命令启动容器,运行镜像:

[root@localhost nginx]#  docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf --privileged=true -v $PWD/logs:/www/logs -v $PWD/html:/etc/nginx/html  -d nginx

这里,-v $PWD/www:/www把主机当前目录下的www目录绑定到了docker中www目录。需要注意的是,

由于docker容器需要对nginx.conf的访问权限,因此,绑定nginx.conf文件时,后面添加--privileged=true命令。

通过docker ps命令,查看容器运行状况

原文链接

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐