Dockerfile 示例二:创建一个Nginx的镜像
Nginx简述
Nginx是一个高性能的 HTTP 和 反向代理 服务器。它因为它的轻量级,易用,易于扩展而流行于业界。基于优良的架构设计,它能够比之前的类似软件处理更多的请求。它也可以用来提供静态文件服务,比如图片,脚本和CSS。
和上个例子一样,我们还是从基础镜像开始,运用FROM命令和MAINTAINER命令
############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name
安装Nginx
# Install Nginx
# Add application repository URL to the default sources
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
Bootstrapping
安装Nginx后,我们需要配置Nginx并且替换掉默认的配置文件
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start
保存 dockfile。
使用Dockerfile自动构建Nginx容器
因为我们命令Docker用当前目录的Nginx的配置文件替换默认的配置文件,我们要保证这个新的配置文件存在。在Dockerfile存在的目录下,创建nginx.conf:
sudo nano nginx.conf
然后用下述内容替换原有内容:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
location / {
proxy_pass http://httpstat.us/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
让我们保存nginx.conf。之后我们就可以用Dockerfile和配置文件来构建镜像。