`

Dockerfile

阅读更多
Dockerfile
//////////////////////////////////////////////////////////////////
=====================================================
Dockerfile用来创建一个自定义的image,包含了用户指定的软件依赖等。当前目录下包含Dockerfile,使用命令build来创建新的image,并命名为edwardsbean/centos6-jdk1.7:
docker  build -t edwardsbean/centos6-jdk1.7  .
=====================================================
Dockerfile 语法

Dockerfile语法由两部分构成,注释和命令+参数
# Line blocks used for commenting
command argument argument ..
一个简单的例子:
# Print "Hello docker!"
RUN echo "Hello docker!"
Dockerfile 命令
Dockerfile有十几条命令可用于构建镜像,下文将简略介绍这些命令。
=====================================================
ADD
---------------------------------------------
ADD命令有两个参数,源和目标。它的基本作用是从源系统的文件系统上复制文件到目标容器的文件系统。如果源是一个URL,那该URL的内容将被下载并复制到容器中。
如果文件是可识别的压缩格式,则docker会帮忙解压缩
# Usage: ADD [source directory or URL] [destination directory]
ADD /my_app_folder /my_app_folder
=====================================================
ENV
---------------------------------------------
ENV命令用于设置环境变量。这些变量以”key=value”的形式存在,并可以在容器内被脚本或者程序调用。这个机制给在容器中运行应用带来了极大的便利。
# Usage: ENV key value
ENV SERVER_WORKS 4
=====================================================
EXPOSE
---------------------------------------------
EXPOSE用来指定端口,使容器内的应用可以通过端口和外界交互。
# Usage: EXPOSE [port]
EXPOSE 8080
=====================================================
FROM
---------------------------------------------
FROM命令可能是最重要的Dockerfile命令。改命令定义了使用哪个基础镜像启动构建流程。基础镜像可以为任意镜像。如果基础镜像没有被发现,Docker将试图从Docker image index来查找该镜像。FROM命令必须是Dockerfile的首个命令。
# Usage: FROM [image name]
FROM ubuntu
=====================================================
MAINTAINER
---------------------------------------------
放在Dockerfile的起始部分,虽然理论上它可以放置于Dockerfile的任意位置。
这个命令用于声明作者,并应该放在FROM的后面。
# Usage: MAINTAINER [name]
MAINTAINER authors_name
=====================================================
RUN
---------------------------------------------
构建命令,可以运行任何被基础image支持的命令。如基础image选择了ubuntu,那么软件管理部分只能使用ubuntu的命令
# Usage: RUN [command]
RUN aptitude install -y riak
=====================================================
CMD
---------------------------------------------
设置指令,用于container启动时指定的操作。该操作可以是执行自定义脚本,也可以是执行系统命令。该指令只能在文件中存在一次,如果有多个,则只执行最后一条
# Usage 1: CMD application "argument", "argument", ..
CMD "echo" "Hello docker!"
=====================================================
ENTRYPOINT
---------------------------------------------
container启动时执行的命令,但是一个Dockerfile中只能有一条ENTRYPOINT命令,如果多条,则只执行最后一条
# Usage: ENTRYPOINT application "argument", "argument", ..
# Remember: arguments are optional. They can be provided by CMD
# or during the creation of a container.
ENTRYPOINT echo
# Usage example with CMD:
# Arguments set with CMD can be overridden during *run*
CMD "Hello docker!"
ENTRYPOINT echo
=====================================================


RUN是在building image时会运行的指令, 在Dockerfile中可以写多条RUN指令,RUN是在Build时运行的,先于CMD和ENTRYPOINT。Build完成了,RUN也运行完成后,再运行CMD或者ENTRYPOINT.

CMD和ENTRYPOINT则是在运行container 时会运行的指令, 都只能写一条, 如果写了多条, 则最后一条生效.

ENTRYPOINT和CMD的不同点在于执行docker run时参数传递方式,CMD指定的命令可以被docker run传递的命令覆盖

CMD ["echo"]
然后运行
docker run CONTAINER_NAME echo foo
那么CMD里指定的echo会被新指定的echo覆盖,所以最终相当于运行echo foo,所以最终打印出的结果就是:
foo

ENTRYPOINT ["echo"]
然后运行
docker run CONTAINER_NAME echo foo
则CONTAINER_NAME后面的echo foo都作为参数传递给ENTRYPOING里指定的echo命令了,所以相当于执行了
echo "echo foo"
最终打印出的结果就是:
echo foo

=====================================================
USER
---------------------------------------------
USER命令用于设置运行容器的UID。
# Usage: USER [UID]
USER 751
=====================================================
VOLUME
---------------------------------------------
VOLUME命令用于让你的容器访问宿主机上的目录。
# Usage: VOLUME ["/dir_1", "/dir_2" ..]
VOLUME ["/my_files"]
=====================================================
WORKDIR
---------------------------------------------
WORKDIR命令用于设置CMD指明的命令的运行目录。
# Usage: WORKDIR /path
WORKDIR ~/
=====================================================
如何使用Dockerfiles
使用Dockerfiles和手工使用Docker Daemon运行命令一样简单。脚本运行后输出为新的镜像ID。
# Build an image using the Dockerfile at current location
# Example: sudo docker build -t [name] .
sudo docker build -t my_mongodb .
=====================================================
Dockerfile 示例一:创建一个MongoDB的镜像
############################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
############################################################
设置基础镜像
# Set the base image to Ubuntu
FROM ubuntu
定义作者
# File Author / Maintainer
MAINTAINER Example McAuthor
设置命令与参数下载MongoDB
################## BEGIN INSTALLATION ######################
# Install MongoDB Following the Instructions at MongoDB Docs
# Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
# Add the package verification key
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
# Add MongoDB to the repository sources list
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
# Update the repository sources list once more
RUN apt-get update
# Install MongoDB package (.deb)
RUN apt-get install -y mongodb-10gen
# Create the default data directory
RUN mkdir -p /data/db
##################### INSTALLATION END #####################
设置MongoDB端口
# Expose the default port
EXPOSE 27017
# Default port to execute the entrypoint (MongoDB)
CMD ["--port 27017"]
# Set default container command
ENTRYPOINT usr/bin/mongod
保存Dockerfile。
构建镜像
使用上述的Dockerfile,我们已经可以开始构建MongoDB镜像
sudo docker build -t my_mongodb .
=====================================================
Dockerfile 示例二:创建一个Nginx的镜像
############################################################
# 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和配置文件来构建镜像。

=====================================================

#test
FROM ubuntu
MAINTAINER xxx
RUN echo hello1 > test1.txt
RUN echo hello2 > /test2.txt
EXPOSE 80
EXPOSE 81
CMD ["/bin/bash"]


上面dockerfile文件中最后一行CMD指令的参数是指定容器启动时要执行的命令,这里是bin/bash命令。
1、用docker run命令创建并启动容器(myimage  是用前面dockerfile创建的镜像的名称):
docker run -i -t myimage 
上面命令是创建并启动容器,打开一个交互式shell。 而以前的写法是
docker run -i -t myimage  /bin/bash
这样就省去了在docker run中写命令了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics