Dockerfile 示例二:创建一个Nginx的镜像

Dockerfile 示例二:创建一个Nginx的镜像

Nginx简述

Nginx是一个高性能的 HTTP 和 反向代理 服务器。它因为它的轻量级,易用,易于扩展而流行于业界。基于优良的架构设计,它能够比之前的类似软件处理更多的请求。它也可以用来提供静态文件服务,比如图片,脚本和CSS。

和上个例子一样,我们还是从基础镜像开始,运用FROM命令和MAINTAINER命令

 
 
  1. ############################################################
  2. # Dockerfile to build Nginx Installed Containers
  3. # Based on Ubuntu
  4. ############################################################
  5. # Set the base image to Ubuntu
  6. FROM ubuntu
  7. # File Author / Maintainer
  8. MAINTAINER Maintaner Name
安装Nginx
 
 
  1. # Install Nginx
  2. # Add application repository URL to the default sources
  3. RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
  4. # Update the repository
  5. RUN apt-get update
  6. # Install necessary tools
  7. RUN apt-get install -y nano wget dialog net-tools
  8. # Download and Install Nginx
  9. RUN apt-get install -y nginx
Bootstrapping 

安装Nginx后,我们需要配置Nginx并且替换掉默认的配置文件

 
 
  1. # Remove the default Nginx configuration file
  2. RUN rm -v /etc/nginx/nginx.conf
  3. # Copy a configuration file from the current directory
  4. ADD nginx.conf /etc/nginx/
  5. # Append "daemon off;" to the beginning of the configuration
  6. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  7. # Expose ports
  8. EXPOSE 80
  9. # Set the default command to execute
  10. # when creating a new container
  11. CMD service nginx start

保存 dockfile。

使用Dockerfile自动构建Nginx容器

因为我们命令Docker用当前目录的Nginx的配置文件替换默认的配置文件,我们要保证这个新的配置文件存在。在Dockerfile存在的目录下,创建nginx.conf:

 
 
  1. sudo nano nginx.conf

然后用下述内容替换原有内容:

 
 
  1. worker_processes 1;
  2. events { worker_connections 1024; }
  3. http {
  4.      sendfile on;
  5.      server {
  6.          listen 80;
  7.          location / {
  8.               proxy_pass http://httpstat.us/;
  9.               proxy_set_header X-Real-IP $remote_addr;
  10.          }
  11.      }
  12. }

让我们保存nginx.conf。之后我们就可以用Dockerfile和配置文件来构建镜像。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值