Dockerfile是一个镜像的表示,可以通过Dockerfile来描述构建镜像的步骤,并自动构建一个容器
所有的 Dockerfile 命令格式都是:
INSTRUCTION arguments
虽然指令忽略大小写,但是建议使用大写。
FROM 命令
FROM <image>
或
FROM <image>:<tag>
这个设置基本的镜像,为后续的命令使用,所以应该作为Dockerfile的第一条指令。
比如:
FROM ubuntu
如果没有指定 tag ,则默认tag是latest,如果都没有则会报错。
RUN 命令
RUN命令会在上面FROM指定的镜像里执行任何命令,然后提交(commit)结果,提交的镜像会在后面继续用到。
两种格式:
RUN <command> (the command is run in a shell - `/bin/sh -c`)
或:
RUN ["executable", "param1", "param2" ... ] (exec form)
RUN命令等价于:
docker run image command
docker commit container_id
注释
使用 # 作为注释
如:
# Memcached
#
# VERSION 1.0
# use the ubuntu base image provided by dotCloud
FROM ubuntu
# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
# install memcached
RUN apt-get install -y memcached
MAINTAINER 命令
# use the ubuntu base image provided by dotCloud
FROM ubuntu
MAINTAINER Victor Coisne victor.coisne@dotcloud.com
# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
# install memcached
RUN apt-get install -y memcached
# Launch memcached when launching the container
ENTRYPOINT ["memcached"]
# run memcached as the daemon user
USER daemon
# expose memcached port
EXPOSE 11211
上面是官方例子,国内建议换成163或sohu的源,不然太慢了。
root@tankywoo-docker:~# docker build -t tankywoo - < dck [38/480]
Uploading context 2.56 kB
Uploading context
Step 0 : FROM ubuntu
---> 99ec81b80c55
Step 1 : MAINTAINER Victor Coisne victor.coisne@dotcloud.com
---> Using cache
---> 2b58110877f6
Step 2 : RUN echo "deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse" > /etc/apt/sources.list
---> Running in f55a4a8bb069
---> d48c6a965398
Step 3 : RUN apt-get update
---> Running in da091a1dd6e7
Ign http://mirrors.163.com precise InRelease
Get:1 http://mirrors.163.com precise Release.gpg [198 B]
WORKDIR /path/to/workdir
配置RUN, CMD, ENTRYPOINT 命令设置当前工作路径
可以设置多次,如果是相对路径,则相对前一个 WORKDIR 命令
比如:
WORKDIR /a WORKDIR b WORKDIR c RUN pwd
其实是在 /a/b/c 下执行 pwd
CMD 命令
有三种格式:
CMD ["executable","param1","param2"] (like an exec, preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (as a shell)
一个Dockerfile里只能有一个CMD,如果有多个,只有最后一个生效。
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT as well.
TODO 还没搞清楚这个的作用