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

来源:互联网 发布:axure rp pro mac版 编辑:程序博客网 时间:2024/06/05 00:21

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
原创粉丝点击