Docker使用---静态网站测试

来源:互联网 发布:绿盟网络审计系统 编辑:程序博客网 时间:2024/06/05 10:53

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.04MAINTAINER juedaiyuer "juedaiyuer@gmail.com"ENV REFRESHED_AT 2016-7-10RUN apt-get updateRUN apt-get -y -q install nginxRUN mkdir -p /var/www/htmlADD nginx/global.conf /etc/nginx/conf.d/ADD nginx/nginx.conf /etc/nginx/nginx.confEXPOSE 80#global.confserver {    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 -lCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMESb46432754bc4        juedaiyuer/nginx    "nginx"             19 seconds ago      Up 18 seconds       0.0.0.0:32768->80/tcp   websitehttp://localhost:32768

可以随时对index文件进行修改

不想把应用或者代码构建到镜像中时,卷的价值得到体现

  1. 希望同时对代码做开发和测试
  2. 代码改动很频繁,不想在开发过程中重构镜像
  3. 希望在多个容器间共享代码
0 0