方法一、通过 dockerfile构建
创建dockerfile
首先,创建目录nginx,用于存放后面的相关东西。
w3cschool@w3cschool:~$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
www目录将映射为nginx容器配置的虚拟目录
logs目录将映射为nginx容器的日志目录
conf目录里的配置文件将映射为nginx容器的配置文件
进入创建的nginx目录,创建dockerfile
from debian:jessie maintainer nginx docker maintainers "docker-maint@nginx.com" env nginx_version 1.10.1-1~jessie run apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573bfd6b3d8fbc641079a6ababf5bd827bd9bf62 \ && echo "deb http://nginx.org/packages/debian/ jessie nginx" >> /etc/apt/sources.list \ && apt-get update \ && apt-get install --no-install-recommends --no-install-suggests -y \ ca-certificates \ nginx=${nginx_version} \ nginx-module-xslt \ nginx-module-geoip \ nginx-module-image-filter \ nginx-module-perl \ nginx-module-njs \ gettext-base \ && rm -rf /var/lib/apt/lists/* # forward request and error logs to docker log collector run ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log expose 80 443 cmd ["nginx", "-g", "daemon off;"]
通过dockerfile创建一个镜像,替换成你自己的名字
docker build -t nginx .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
w3cschool@w3cschool:~/nginx$ docker images nginx repository tag image id created size nginx latest 555bbd91e13c 3 days ago 182.8 mb
方法二、docker pull nginx
查找docker hub上的nginx镜像
w3cschool@w3cschool:~/nginx$ docker search nginx name description stars official automated nginx official build of nginx. 3260 [ok] jwilder/nginx-proxy automated nginx reverse proxy for docker c... 674 [ok] richarvey/nginx-php-fpm container running nginx + php-fpm capable ... 207 [ok] million12/nginx-php nginx + php-fpm 5.5, 5.6, 7.0 (ng), centos... 67 [ok] maxexcloo/nginx-php docker framework container with nginx and ... 57 [ok] webdevops/php-nginx nginx with php-fpm 39 [ok] h3nrik/nginx-ldap nginx web server with ldap/ad, ssl and pro... 27 [ok] bitnami/nginx bitnami nginx docker image 19 [ok] maxexcloo/nginx docker framework container with nginx inst... 7 [ok] ...
这里我们拉取官方的镜像
w3cschool@w3cschool:~/nginx$ docker pull nginx
等待下载完成后,我们就可以在本地镜像列表里查到repository为nginx的镜像。
使用nginx镜像
运行容器
w3cschool@w3cschool:~/nginx$ docker run -p 80:80 --name mynginx -v $pwd/www:/www -v $pwd/conf/nginx.conf:/etc/nginx/nginx.conf -v $pwd/logs:/wwwlogs -d nginx 45c89fab0bf9ad643bc7ab571f3ccd65379b844498f54a7c8a4e7ca1dc3a2c1e w3cschool@w3cschool:~/nginx$
命令说明:
-p 80:80:将容器的80端口映射到主机的80端口
--name mynginx:将容器命名为mynginx
-v $pwd/www:/www:将主机中当前目录下的www挂载到容器的/www
-v $pwd/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf
-v $pwd/logs:/wwwlogs:将主机中当前目录下的logs挂载到容器的/wwwlogs
查看容器启动情况
w3cschool@w3cschool:~/nginx$ docker ps container id image command ports names 45c89fab0bf9 nginx "nginx -g 'daemon off" ... 0.0.0.0:80->80/tcp, 443/tcp mynginx f2fa96138d71 tomcat "catalina.sh run" ... 0.0.0.0:81->8080/tcp tomcat
通过浏览器访问