关于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命令,查看容器运行状况
[root@localhost nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b3e68a0d311 nginx "nginx -g 'daemon ..." 26 minutes ago Up 26 minutes 0.0.0.0:80->80/tcp mynginx
也可以通过,docker ps -a查看所有容器,包括已经退出的容器。
经过以上步骤,nginx镜像已经成功在docker容器中运行,接下来,我们通过访问http://localhost来验证,正常情况下:
这样,我们的nginx已经启动成功了。
注意事项:
1、如果出现运行容器后,docker ps没有发现容器进程,说明容器启动过程发生错误闪退,可以通过docker ps -a看到闪退的容器,然后,通过docker logs 容器名或容器ID来查看docker日志,里面记录了容器启动发生错误的原因,找到问题所在,就好办了。
这个过程中,还会经常用到docker rm移除容器的命令,docker logs 查看容器日志,docker stop/start/restart等停止/启动/重启命令。
由此,docker中运行NGINX的过程就完成了,一回生,二回熟,nginx的配置到启动,只需要秒秒钟。