Dockerfile
#为Nginx Dockerfile创建一个目录
$ mkdir sample
$ cd sample
$ touch Dockerfile
#获取Nginx配置文件
$ cd sample
$ mkdir nginx && cd nginx
$ wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
$ wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
$ cd ..
FROM ubuntu:14.04
MAINTAINER juedaiyuer "juedaiyuer@gmail.com"
ENV REFRESHED_AT 2016-7-10
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
#global.conf
server {
listen 0.0.0.0:80;
server_name _;
root /var/www/html/website;
index index.html index.htm;
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}
#nginx.conf
#阻止nginx进入后台,强制其在前台运行;保持Docker容器的活跃状态,其中运行的进程不能中断
user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events { }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
}
#构建镜像
$ sudo docker build -t juedaiyuer/nginx .
创建网站
#在sample目录下的操作
$ mkdir website && cd website
$ wget https://github.com/jamtur01/dockerbook-code/blob/master/code/5/sample/website/index.html
$ cd ..
# -v 允许我们将宿主机的目录作为卷,挂载到容器里
$ sudo docker run -d -p 80 --name website -v $PWD/website:/var/www/html/website juedaiyuer/nginx nginx
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b46432754bc4 juedaiyuer/nginx "nginx" 19 seconds ago Up 18 seconds 0.0.0.0:32768->80/tcp website
http://localhost:32768
可以随时对index文件进行修改
卷
不想把应用或者代码构建到镜像中时,卷的价值得到体现
- 希望同时对代码做开发和测试
- 代码改动很频繁,不想在开发过程中重构镜像
- 希望在多个容器间共享代码