1. 程式人生 > >docker 最新Dockerfile命令手冊

docker 最新Dockerfile命令手冊

Dockerfile Reference

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands you would normally execute manually in order to build a Docker image. By calling docker build from your terminal, you can have Docker build your image step by step, executing the instructions successively.

This page discusses the specifics of all the instructions you can use in your Dockerfile. To further help you write a clear, readable, maintainable Dockerfile, we've also written a Dockerfile Best Practices guide. Lastly, you can test your Dockerfile knowledge with the Dockerfile tutorial.

Usage

To build an image from a source repository, create a description file called Dockerfile at the root of your repository. This file will describe the steps to assemble the image.

Then call docker build with the path of your source repository as the argument (for example, .):

$ sudo docker build 
.

The path to the source repository defines where to find the context of the build. The build is run by the Docker daemon, not by the CLI, so the whole context must be transferred to the daemon. The Docker CLI reports "Sending build context to Docker daemon" when the context is sent to the daemon.

Warning Avoid using your root directory, /, as the root of the source repository. The docker buildcommand will use whatever directory contains the Dockerfile as the build context (including all of its subdirectories). The build context will be sent to the Docker daemon before building the image, which means if you use / as the source repository, the entire contents of your hard drive will get sent to the daemon (and thus to the machine running the daemon). You probably don't want that.

In most cases, it's best to put each Dockerfile in an empty directory, and then add only the files needed for building that Dockerfile to that directory. To further speed up the build, you can exclude files and directories by adding a .dockerignore file to the same directory.

You can specify a repository and tag at which to save the new image if the build succeeds:

$ sudo docker build -t shykes/myapp .

The Docker daemon will run your steps one-by-one, committing the result to a new image if necessary, before finally outputting the ID of your new image. The Docker daemon will automatically clean up the context you sent.

Note that each instruction is run independently, and causes a new image to be created - so RUN cd /tmp will not have any effect on the next instructions.

Whenever possible, Docker will re-use the intermediate images, accelerating docker build significantly (indicated by Using cache - see the Dockerfile Best Practices guide for more information):

$ sudo docker build -t SvenDowideit/ambassador .Uploading context 10.24 kB
Uploading context
Step1: FROM docker-ut
 ---> cbba202fe96b
Step2: MAINTAINER [email protected].org.au
 --->Using cache
 --->51182097be13Step3: CMD env | grep _TCP=| sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/'| sh && top
 --->Using cache
 --->1a5ffc17324dSuccessfully built 1a5ffc17324d

When you're done with your build, you're ready to look into Pushing a repository to its registry.

Format

Here is the format of the Dockerfile:

# Comment
INSTRUCTION arguments

The Instruction is not case-sensitive, however convention is for them to be UPPERCASE in order to distinguish them from arguments more easily.

Docker runs the instructions in a Dockerfile in order. The first instruction must be `FROM` in order to specify the Base Image from which you are building.

Docker will treat lines that begin with # as a comment. A # marker anywhere else in the line will be treated as an argument. This allows statements like:

# Comment
RUN echo 'we are running some # of cool things'

Here is the set of instructions you can use in a Dockerfile for building images.

Environment Replacement

Note: prior to 1.3, Dockerfile environment variables were handled similarly, in that they would be replaced as described below. However, there was no formal definition on as to which instructions handled environment replacement at the time. After 1.3 this behavior will be preserved and canonical.

Environment variables (declared with the ENV statement) can also be used in certain instructions as variables to be interpreted by the Dockerfile. Escapes are also handled for including variable-like syntax into a statement literally.

Environment variables are notated in the Dockerfile either with $variable_name or ${variable_name}. They are treated equivalently and the brace syntax is typically used to address issues with variable names with no whitespace, like ${foo}_bar.

Escaping is possible by adding a \ before the variable: \$foo or \${foo}, for example, will translate to$foo and ${foo} literals respectively.

Example (parsed representation is displayed after the #):

FROM busybox
ENV foo /bar
WORKDIR ${foo}# WORKDIR /bar
ADD . $foo       # ADD . /bar
COPY \$foo /quux # COPY $foo /quux

The instructions that handle environment variables in the Dockerfile are:

  • ENV
  • ADD
  • COPY
  • WORKDIR
  • EXPOSE
  • VOLUME
  • USER

ONBUILD instructions are NOT supported for environment replacement, even the instructions above.

The .dockerignore file

If a file named .dockerignore exists in the source repository, then it is interpreted as a newline-separated list of exclusion patterns. Exclusion patterns match files or directories relative to the source repository that will be excluded from the context. Globbing is done using Go's filepath.Match rules.

Note: The .dockerignore file can even be used to ignore the Dockerfile and .dockerignore files. This might be useful if you are copying files from the root of the build context into your new containter but do not want to include the Dockerfile or .dockerignore files (e.g. ADD . /someDir/).

The following example shows the use of the .dockerignore file to exclude the .git directory from the context. Its effect can be seen in the changed size of the uploaded context.

$ sudo docker build .Uploading context 18.829 MB
Uploading context
Step0: FROM busybox
 --->769b9341d937Step1: CMD echo HelloWorld--->Using cache
 --->99cc1ad10469Successfully built 99cc1ad10469
$ echo ".git">.dockerignore
$ sudo docker build .Uploading context  6.76 MB
Uploading context
Step0: FROM busybox
 --->769b9341d937Step1: CMD echo HelloWorld--->Using cache
 --->99cc1ad10469Successfully built 99cc1ad10469

FROM

FROM <image>

Or

FROM <image>:<tag>

The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.

FROM must be the first non-comment instruction in the Dockerfile.

FROM can appear multiple times within a single Dockerfile in order to create multiple images. Simply make a note of the last image ID output by the commit before each new FROM command.

If no tag is given to the FROM instruction, latest is assumed. If the used tag does not exist, an error will be returned.

MAINTAINER

MAINTAINER <name>

The MAINTAINER instruction allows you to set the Author field of the generated images.

RUN

RUN has 2 forms:

  • RUN <command> (the command is run in a shell - /bin/sh -c - shell form)
  • RUN ["executable", "param1", "param2"] (exec form)

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

Layering RUN instructions and generating commits conforms to the core concepts of Docker where commits are cheap and containers can be created from any point in an image's history, much like source control.

The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain /bin/sh.

Note: To use a different shell, other than '/bin/sh', use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]

Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, RUN [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: RUN [ "sh", "-c", "echo", "$HOME" ].

The cache for RUN instructions isn't invalidated automatically during the next build. The cache for an instruction like RUN apt-get dist-upgrade -y will be reused during the next build. The cache for RUNinstructions can be invalidated by using the --no-cache flag, for example docker build --no-cache.

The cache for RUN instructions can be invalidated by ADD instructions. See below for details.

Known Issues (RUN)

  • Issue 783 is about file permissions problems that can occur when using the AUFS file system. You might notice it during an attempt to rm a file, for example. The issue describes a workaround.

CMD

The CMD instruction has three forms:

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the lastCMD will take effect.

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 ENTRYPOINTinstruction as well.

Note: If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD andENTRYPOINT instructions should be specified with the JSON array format.

Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].

When used in the shell or exec formats, the CMD instruction sets the command to be executed when running the image.

If you use the shell form of the CMD, then the <command> will execute in /bin/sh -c:


            
           

相關推薦

docker 最新Dockerfile命令手冊

Dockerfile Reference Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text

最新docker命令手冊

Command Line Note: if you are using a remote Docker daemon, such as Boot2Docker, then do not type the sudobefore the docker comma

DockerDockerfile 構建映象 :build 命令的用法

Dockerfile 建立完成後,可以使用 docker build 命令根據 Dockerfile 構建一個映象。1. 首先準備好 Dockerfile : 2. 執行構建命令: docker bui

Docker(三):Dockerfile 命令詳解

上一篇文章Docker(二):Dockerfile 使用介紹介紹了 Dockerfile 的使用,這篇文章我們來繼續瞭解 Dockerfile ,學習 Dockerfile 各種命令的使用。 Dockerfile 指令詳解 1 FROM 指定基礎映象 FROM 指令用於指定其後構建新映象所使用的基礎映象。FR

Docker學習——Dockerfile中的構建命令

目錄 前言 ENV FROM RUN CMD LABEL ENV ADD COPY USER ARG SHELL 前言 docker的有些文件寫的真的有點糟糕.............

Docker基礎學習(五)-Dockerfile命令詳解(超全版本)

製作Dockerfile為Docker入門學習的第一步(當然,除了環境搭建)。 本文收集、整理了官網關於製作Dockerfile的全部命令(除SHELL沒整理,這個就不弄了),可幫助大家快速進入Dockfile製作的學習。 以下為正文,下面進入學習姿勢吧!

如何使用Docker實現PHP命令行程序的CI/CD?

ensure 現在 持續集成 mage 服務器遠程 本地 數據 詳細 提交 本文標簽: Docker PHP命令行程序的CI/CD Codeship 內容要點: - 使用Jet設置環境並在本地運行測試 - 配置Codeship Pro每次新代碼提交時,自動運行測試 - 上一

docker-compose常用命令(持續更新...)

art 啟動 cal start 構建 doc tty span 鏡像 build 構建或重建服務 help 命令幫助 kill 殺掉容器 logs 顯示容器的輸出內容 port 打印綁定的開放端口 ps 顯示容器 pull 拉取服務鏡像 restart 重啟服務 rm

Redis 學習之路 (010) - redis命令手冊

哈希 sts 集中 cluster htm second 不同 index scribe Redis 鍵(key) 命令 命令描述 Redis DEL 命令 該命令用於在 key 存在是刪除 key。 Redis Dump 命令 序列化給定 key ,並返回被

7 docker-使用dockerfile創建鏡像

docker在前面的實驗中我們多次用到的 Dockerfile,在本實驗裏我們將通過完成一個實例來學習Dockerfile的編寫。本節中,我們需要依次完成下面幾項任務: Dockerfile 基本框架 Dockerfile 編寫常用命令 從 Dockerfile 構建鏡像本次實驗的需求是完成

docker run常用命令及 解決 ubuntu鏡像無法識別 ifconfig ping 命令

docker ubuntu ipconfig pingdocker run -it docker 前端啟動 container容器 -d 後端啟動 container容器 -p 固定端口映射

Docker鏡像命令筆記

獲取 container ges down try 暫停 input 創建 min 獲取 docker pull NAME[:TAG] docker pull registry.docker-cn.com/library/ubuntu:14.04 查看 docker ima

dockerDockerfile構建LNMP平臺

docker dockerfile docker lnmp docker build 1、dockerfile介紹 ?Dockerfile是Docker用來構建鏡像的文本文件,包含自定義的指令和格式。可以通過docker build命令從Dockerfile中構建鏡像。這個過程與傳統分布式集

Hadoop命令手冊

classname 分隔 ktr section 任務 hdf 不可 1.0 給定 原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/commands_manual.html 概述 常規選項 用戶命令

Docker mongodb Dockerfile ubuntu

docker1.目錄結構,把配置文件也放這裏,等下復制到image裏面去2.mongod.confnet: port: 27017 bindIp: 0.0.0.0 security: authorization: enabled systemLog: destination: file p

docker-compose 常用命令參數解釋

docker composecompose 常用命令解釋Usage: docker-compose [options] [options] [COMMAND] [ARGS...][options] build Usage: build [options] [--build-arg key=val...] [

Docker常用操作命令

article 包括 環境變量 退出 mirror ini last csdn nginx 一、啟動、重啟、停止docker服務 sudo /etc/init.d/docker start sudo /etc/init.d/docker restart sudo /etc/

【轉】dockerDockerfile實踐

b2c size ebe rem 目錄 all 緩存 local title 上一篇介紹了Dockerfile中使用的指令,現在開始進行指令實踐 先查看下本地的鏡像,選一個作為base image: [root@docker ~]# docker images REPO

Docker基本使用命令

RF ber TE 綁定 名稱 AR -i container 修改 Docker基本使用命令 首先要在宿主機上安裝Docker,Docker安裝參考 Docker學習(二)—–Docker安裝和使用 Docker命令也比較類似Git,支持push以及pull操作上傳以及下