(四)制作一个nginx 的Docker镜像

我们在ubuntu 14.04镜像基础上创建一个nginx镜像,用来发布一个静态页面。

创建一个目录

ubuntu@ubuntu:~$ mkdir nginx
ubuntu@ubuntu:~$ cd nginx
ubuntu@ubuntu:~/nginx$ touch Dockerfile

编辑Dockerfile

ubuntu@ubuntu:~/nginx$ vim Dockerfile

FROM ubuntu:14.04
MAINTAINER Reed  "reed@example.com"

RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html

ADD global.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

对上面Dockerfile文件做一个解释:
FROM 指定基础镜像,我们制做的镜像是在Ubuntu:14.04的基础上生成的。
MAINTAINER 镜像制作人的信息
RUN 指定创建镜像时执行的命令
ADD 将本地文件添加到镜像中
EXPOSE 容器开放80端口

创建Nginx配置文件

ubuntu@ubuntu:~/nginx$ cat nginx.conf

user  www-data;
worker_processes  2;
daemon  off;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

    gzip  on;
    gzip_disable  "msie6";
    gzip_vary  on;
    gzip_proxied  any;
    gzip_comp_level  6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
}

ubuntu@ubuntu:~/nginx$ vim 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;
}

建立一个测试页面。

ubuntu@ubuntu:~/nginx$ pwd
/home/ubuntu/nginx
ubuntu@ubuntu:~/nginx$ mkdir website
ubuntu@ubuntu:~/nginx$ cd website/
ubuntu@ubuntu:~/nginx/website$ cat index.html 
<head>

<title>Container Test  website</title>

</head>

<body>

<h1>My First Container Test  Website</h1>

</body>

创建Docker镜像 ,需要几分钟时间

ubuntu@ubuntu:~/nginx$ pwd
/home/ubuntu/nginx

ubuntu@ubuntu:~/nginx$ docker build -t "reed/nginx" .
Sending build context to Docker daemon 6.144 kB
Step 1 : FROM ubuntu:14.04
 ---> 07c86167cdc4
Step 2 : MAINTAINER Reed  "reed@example.com"
 ---> Running in 65c82003b728
 ---> d753b4af208f
Removing intermediate container 65c82003b728
Step 3 : RUN apt-get update
 ---> Running in 046595f8d595

ubuntu@ubuntu:~/nginx$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
reed/nginx          latest              ef9da3a3be13        5 minutes ago       227.7 MB

运行我们创建的镜像

ubuntu@ubuntu:~/nginx$ docker  run -d -p 80:80 --name website -v /home/ubuntu/nginx/website:/var/www/html/website  reed/nginx nginx
07fc828624f2083be22da85603178644c8fec251d0fa9a0f7c12812309f7ed04

ubuntu@ubuntu:~/nginx$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
07fc828624f2        reed/nginx          "nginx"             3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   website

解释一下docker run 命令使用的几个参数
-d 让Docker在后台运行
-p 80:80 本地的80端点映射到容器中的80端点
–name 给容器设置一个名字
-v 将本地的文件挂载到容器中,因为容器中关闭中数据就消失了,所以像网站、数据库的文件可以存放在本地,在容器中挂载就可以
reed/nginx 我们创建的镜像
nginx 我们将这个命令传递给容器,因为在配置文件中设置了 daemon off,这样就可以让nginx在前台运行。

验证创建的web站点

ubuntu@ubuntu:~/nginx$ curl  localhost
<head>

<title>Container Test  website</title>

</head>

<body>

<h1>My First Container Test  Website</h1>

</body>

好了,至此我们创建的nginx镜像就可以正常工作。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值