.nh .TH "DOCKER" "1" "JUNE 2014" "Docker Community" "Docker User Manuals" .SH NAME .PP docker-build - Build an image from a Dockerfile .SH SYNOPSIS .PP \fBdocker build\fP [\fB--add-host\fP[=\fI[]\fP]] [\fB--build-arg\fP[=\fI[]\fP]] [\fB--cache-from\fP[=\fI[]\fP]] [\fB--cpu-shares\fP[=\fI0\fP]] [\fB--cgroup-parent\fP[=\fICGROUP-PARENT\fP]] [\fB--help\fP] [\fB--iidfile\fP[=\fICIDFILE\fP]] [\fB-f\fP|\fB--file\fP[=\fIPATH/Dockerfile\fP]] [\fB-squash\fP] \fIExperimental\fP [\fB--force-rm\fP] [\fB--isolation\fP[=\fIdefault\fP]] [\fB--label\fP[=\fI[]\fP]] [\fB--no-cache\fP] [\fB--pull\fP] [\fB--compress\fP] [\fB-q\fP|\fB--quiet\fP] [\fB--rm\fP[=\fItrue\fP]] [\fB-t\fP|\fB--tag\fP[=\fI[]\fP]] [\fB-m\fP|\fB--memory\fP[=\fIMEMORY\fP]] [\fB--memory-swap\fP[=\fILIMIT\fP]] [\fB--network\fP[=\fI"default"\fP]] [\fB--shm-size\fP[=\fISHM-SIZE\fP]] [\fB--cpu-period\fP[=\fI0\fP]] [\fB--cpu-quota\fP[=\fI0\fP]] [\fB--cpuset-cpus\fP[=\fICPUSET-CPUS\fP]] [\fB--cpuset-mems\fP[=\fICPUSET-MEMS\fP]] [\fB--target\fP[=\fI[]\fP]] [\fB--ulimit\fP[=\fI[]\fP]] PATH | URL | - .SH DESCRIPTION .PP This will read the Dockerfile from the directory specified in \fBPATH\fP\&. It also sends any other files and directories found in the current directory to the Docker daemon. The contents of this directory would be used by \fBADD\fP commands found within the Dockerfile. .PP Warning, this will send a lot of data to the Docker daemon depending on the contents of the current directory. 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. .PP When the URL to a tarball archive or to a single Dockerfile is given, no context is sent from the client to the Docker daemon. In this case, the Dockerfile at the root of the archive and the rest of the archive will get used as the context of the build. When a Git repository is set as the \fBURL\fP, the repository is cloned locally and then sent as the context. .SH OPTIONS .PP \fB-f\fP, \fB--file\fP \fIPATH/Dockerfile\fP Path to the Dockerfile to use. If the path is a relative path and you are building from a local directory, then the path must be relative to that directory. If you are building from a remote URL pointing to either a tarball or a Git repository, then the path must be relative to the root of the remote context. In all cases, the file must be within the build context. The default is \fIDockerfile\fP\&. .PP \fB--squash\fP \fItrue\fP|\fIfalse\fP \fBExperimental Only\fP Once the image is built, squash the new layers into a new image with a single new layer. Squashing does not destroy any existing image, rather it creates a new image with the content of the squashed layers. This effectively makes it look like all \fBDockerfile\fR commands were created with a single layer. The build cache is preserved with this method. .PP \fBNote\fP: using this option means the new image will not be able to take advantage of layer sharing with other images and may use significantly more space. .PP \fBNote\fP: using this option you may see significantly more space used due to storing two copies of the image, one for the build cache with all the cache layers in tact, and one for the squashed version. .PP \fB--add-host\fP [] Add a custom host-to-IP mapping (host:ip) .PP Add a line to /etc/hosts. The format is hostname:ip. The \fB--add-host\fP option can be set multiple times. .PP \fB--build-arg\fP \fIvariable\fP name and value of a \fBbuildarg\fP\&. .PP For example, if you want to pass a value for \fBhttp_proxy\fR, use \fB--build-arg=http_proxy="http://some.proxy.url"\fR .PP Users pass these values at build-time. Docker uses the \fBbuildargs\fR as the environment context for command(s) run via the Dockerfile's \fBRUN\fR instruction or for variable expansion in other Dockerfile instructions. This is not meant for passing secret values. Read more about the buildargs instruction \[la]https://docs.docker.com/engine/reference/builder/#arg\[ra] .PP \fB--cache-from\fP "" Set image that will be used as a build cache source. .PP \fB--force-rm\fP \fItrue\fP|\fIfalse\fP Always remove intermediate containers, even after unsuccessful builds. The default is \fIfalse\fP\&. .PP \fB--isolation\fP "\fIdefault\fP" Isolation specifies the type of isolation technology used by containers. .PP \fB--label\fP \fIlabel\fP Set metadata for an image .PP \fB--no-cache\fP \fItrue\fP|\fIfalse\fP Do not use cache when building the image. The default is \fIfalse\fP\&. .PP \fB--iidfile\fP "" Write the image ID to the file .PP \fB--help\fP Print usage statement .PP \fB--pull\fP \fItrue\fP|\fIfalse\fP Always attempt to pull a newer version of the image. The default is \fIfalse\fP\&. .PP \fB--compress\fP \fItrue\fP|\fIfalse\fP Compress the build context using gzip. The default is \fIfalse\fP\&. .PP \fB-q\fP, \fB--quiet\fP \fItrue\fP|\fIfalse\fP Suppress the build output and print image ID on success. The default is \fIfalse\fP\&. .PP \fB--rm\fP \fItrue\fP|\fIfalse\fP Remove intermediate containers after a successful build. The default is \fItrue\fP\&. .PP \fB-t\fP, \fB--tag\fP "" Repository names (and optionally with tags) to be applied to the resulting image in case of success. Refer to \fBdocker-tag(1)\fP for more information about valid tag names. .PP \fB-m\fP, \fB--memory\fP \fIMEMORY\fP Memory limit .PP \fB--memory-swap\fP \fInumber\fP[\fIS\fP] Combined memory plus swap limit; \fIS\fP is an optional suffix which can be one of \fBb\fP (bytes), \fBk\fP (kilobytes), \fBm\fP (megabytes), or \fBg\fP (gigabytes). .PP This option can only be used together with \fB--memory\fP\&. The argument should always be larger than that of \fB--memory\fP\&. Default is double the value of \fB--memory\fP\&. Set to \fB-1\fP to enable unlimited swap. .PP \fB--network\fP \fItype\fP Set the networking mode for the RUN instructions during build. Supported standard values are: \fBnone\fP, \fBbridge\fP, \fBhost\fP and \fBcontainer:\fP<\fIname\fP|\fIid\fP>. Any other value is taken as a custom network's name or ID which this container should connect to. .PP In Linux, default is \fBbridge\fP\&. .PP \fB--shm-size\fP \fISHM-SIZE\fP Size of \fB/dev/shm\fR\&. The format is \fB\fR\&. \fBnumber\fR must be greater than \fB0\fR\&. Unit is optional and can be \fBb\fR (bytes), \fBk\fR (kilobytes), \fBm\fR (megabytes), or \fBg\fR (gigabytes). If you omit the unit, the system uses bytes. If you omit the size entirely, the system uses \fB64m\fR\&. .PP \fB--cpu-shares\fP \fI0\fP CPU shares (relative weight). .PP By default, all containers get the same proportion of CPU cycles. CPU shares is a 'relative weight', relative to the default setting of 1024. This default value is defined here: .EX cat /sys/fs/cgroup/cpu/cpu.shares 1024 .EE .PP You can change this proportion by adjusting the container's CPU share weighting relative to the weighting of all other running containers. .PP To modify the proportion from the default of 1024, use the \fB--cpu-shares\fP flag to set the weighting to 2 or higher. .EX Container CPU share Flag {C0} 60% of CPU --cpu-shares 614 (614 is 60% of 1024) {C1} 40% of CPU --cpu-shares 410 (410 is 40% of 1024) .EE .PP The proportion is only applied when CPU-intensive processes are running. When tasks in one container are idle, the other containers can use the left-over CPU time. The actual amount of CPU time used varies depending on the number of containers running on the system. .PP For example, consider three containers, where one has \fB--cpu-shares 1024\fP and two others have \fB--cpu-shares 512\fP\&. When processes in all three containers attempt to use 100% of CPU, the first container would receive 50% of the total CPU time. If you add a fourth container with \fB--cpu-shares 1024\fP, the first container only gets 33% of the CPU. The remaining containers receive 16.5%, 16.5% and 33% of the CPU. .EX Container CPU share Flag CPU time {C0} 100% --cpu-shares 1024 33% {C1} 50% --cpu-shares 512 16.5% {C2} 50% --cpu-shares 512 16.5% {C4} 100% --cpu-shares 1024 33% .EE .PP On a multi-core system, the shares of CPU time are distributed across the CPU cores. Even if a container is limited to less than 100% of CPU time, it can use 100% of each individual CPU core. .PP For example, consider a system with more than three cores. If you start one container \fB{C0}\fP with \fB--cpu-shares 512\fP running one process, and another container \fB{C1}\fP with \fB--cpu-shares 1024\fP running two processes, this can result in the following division of CPU shares: .EX PID container CPU CPU share 100 {C0} 0 100% of CPU0 101 {C1} 1 100% of CPU1 102 {C1} 2 100% of CPU2 .EE .PP \fB--cpu-period\fP \fI0\fP Limit the CPU CFS (Completely Fair Scheduler) period. .PP Limit the container's CPU usage. This flag causes the kernel to restrict the container's CPU usage to the period you specify. .PP \fB--cpu-quota\fP \fI0\fP Limit the CPU CFS (Completely Fair Scheduler) quota. .PP By default, containers run with the full CPU resource. This flag causes the kernel to restrict the container's CPU usage to the quota you specify. .PP \fB--cpuset-cpus\fP \fICPUSET-CPUS\fP CPUs in which to allow execution (0-3, 0,1). .PP \fB--cpuset-mems\fP \fICPUSET-MEMS\fP Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems. .PP For example, if you have four memory nodes on your system (0-3), use \fB--cpuset-mems 0,1\fR to ensure the processes in your Docker container only use memory from the first two memory nodes. .PP \fB--cgroup-parent\fP \fICGROUP-PARENT\fP Path to \fBcgroups\fR under which the container's \fBcgroup\fR are created. .PP If the path is not absolute, the path is considered relative to the \fBcgroups\fR path of the init process. Cgroups are created if they do not already exist. .PP \fB--target\fP "" Set the target build stage name. .PP \fB--ulimit\fP [] Ulimit options .PP For more information about \fBulimit\fR see Setting ulimits in a container \[la]https://docs.docker.com/engine/reference/commandline/run/#set\-ulimits\-in\-container\-\-\-ulimit\[ra] .SH EXAMPLES .SH Building an image using a Dockerfile located inside the current directory .PP Docker images can be built using the build command and a Dockerfile: .EX docker build . .EE .PP During the build process Docker creates intermediate images. In order to keep them, you must explicitly set \fB--rm false\fR\&. .EX docker build --rm false . .EE .PP A good practice is to make a sub-directory with a related name and create the Dockerfile in that directory. For example, a directory called mongo may contain a Dockerfile to create a Docker MongoDB image. Likewise, another directory called httpd may be used to store Dockerfiles for Apache web server images. .PP It is also a good practice to add the files required for the image to the sub-directory. These files will then be specified with the \fBCOPY\fR or \fBADD\fR instructions in the \fBDockerfile\fR\&. .PP Note: If you include a tar file (a good practice), then Docker will automatically extract the contents of the tar file specified within the \fBADD\fR instruction into the specified target. .SH Building an image and naming that image .PP A good practice is to give a name to the image you are building. Note that only a-z0-9-_. should be used for consistency. There are no hard rules here but it is best to give the names consideration. .PP The \fB-t\fP/\fB--tag\fP flag is used to rename an image. Here are some examples: .PP Though it is not a good practice, image names can be arbitrary: .EX docker build -t myimage . .EE .PP A better approach is to provide a fully qualified and meaningful repository, name, and tag (where the tag in this context means the qualifier after the ":"). In this example we build a JBoss image for the Fedora repository and give it the version 1.0: .EX docker build -t fedora/jboss:1.0 . .EE .PP The next example is for the "whenry" user repository and uses Fedora and JBoss and gives it the version 2.1 : .EX docker build -t whenry/fedora-jboss:v2.1 . .EE .PP If you do not provide a version tag then Docker will assign \fBlatest\fR: .EX docker build -t whenry/fedora-jboss . .EE .PP When you list the images, the image above will have the tag \fBlatest\fR\&. .PP You can apply multiple tags to an image. For example, you can apply the \fBlatest\fR tag to a newly built image and add another tag that references a specific version. For example, to tag an image both as \fBwhenry/fedora-jboss:latest\fR and \fBwhenry/fedora-jboss:v2.1\fR, use the following: .EX docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 . .EE .PP So renaming an image is arbitrary but consideration should be given to a useful convention that makes sense for consumers and should also take into account Docker community conventions. .SH Building an image using a URL .PP This will clone the specified GitHub repository from the URL and use it as context. The Dockerfile at the root of the repository is used as Dockerfile. This only works if the GitHub repository is a dedicated repository. .EX docker build github.com/scollier/purpletest .EE .PP Note: You can set an arbitrary Git repository via the \fBgit://\fR scheme. .SH Building an image using a URL to a tarball'ed context .PP This will send the URL itself to the Docker daemon. The daemon will fetch the tarball archive, decompress it and use its contents as the build context. The Dockerfile at the root of the archive and the rest of the archive will get used as the context of the build. If you pass an \fB-f PATH/Dockerfile\fP option as well, the system will look for that file inside the contents of the tarball. .EX docker build -f dev/Dockerfile https://10.10.10.1/docker/context.tar.gz .EE .PP Note: supported compression formats are 'xz', 'bzip2', 'gzip' and 'identity' (no compression). .SH Specify isolation technology for container (--isolation) .PP This option is useful in situations where you are running Docker containers on Windows. The \fB--isolation \fR option sets a container's isolation technology. On Linux, the only supported is the \fBdefault\fR option which uses Linux namespaces. On Microsoft Windows, you can specify these values: .RS .IP \(bu 2 \fBdefault\fR: Use the value specified by the Docker daemon's \fB--exec-opt\fR . If the \fBdaemon\fR does not specify an isolation technology, Microsoft Windows uses \fBprocess\fR as its default value. .IP \(bu 2 \fBprocess\fR: Namespace isolation only. .IP \(bu 2 \fBhyperv\fR: Hyper-V hypervisor partition-based isolation. .RE .PP Specifying the \fB--isolation\fR flag without a value is the same as setting \fB--isolation "default"\fR\&. .SH HISTORY .PP March 2014, Originally compiled by William Henry (whenry at redhat dot com) based on docker.com source material and internal work. June 2014, updated by Sven Dowideit SvenDowideit@home.org.au \[la]mailto:SvenDowideit@home.org.au\[ra] June 2015, updated by Sally O'Malley somalley@redhat.com \[la]mailto:somalley@redhat.com\[ra] August 2020, Updated by Des Preston despreston@gmail.com \[la]mailto:despreston@gmail.com\[ra]