2014-04-16 14:07:55 -04:00
% DOCKER(1) Docker User Manuals
2014-06-30 22:58:04 -04:00
% Docker Community
% JUNE 2014
2014-04-16 14:07:55 -04:00
# NAME
2016-12-09 10:15:26 -05:00
docker-build - Build an image from a Dockerfile
2014-04-16 14:07:55 -04:00
# SYNOPSIS
2014-06-30 22:58:04 -04:00
**docker build**
2015-10-03 11:56:41 -04:00
[**--build-arg**[=*[]*]]
2015-10-16 15:05:18 -04:00
[**--cpu-shares**[=*0*]]
2015-10-03 11:56:41 -04:00
[**--cgroup-parent**[=*CGROUP-PARENT*]]
2014-10-15 17:14:12 -04:00
[**--help**]
2015-03-11 12:04:30 -04:00
[**-f**|**--file**[=*PATH/Dockerfile*]]
2016-04-21 12:08:37 -04:00
[**-squash**] *Experimental*
2015-12-23 09:37:06 -05:00
[**--force-rm**]
2015-11-09 04:11:10 -05:00
[**--isolation**[=*default*]]
2016-03-25 14:16:19 -04:00
[**--label**[=*[]*]]
2015-12-23 09:37:06 -05:00
[**--no-cache**]
[**--pull**]
2016-08-18 04:35:23 -04:00
[**--compress**]
2015-12-23 09:37:06 -05:00
[**-q**|**--quiet**]
2014-06-30 22:58:04 -04:00
[**--rm**[=*true*]]
2015-08-30 09:48:03 -04:00
[**-t**|**--tag**[=*[]*]]
2015-02-06 09:33:01 -05:00
[**-m**|**--memory**[=*MEMORY*]]
2015-12-29 20:23:35 -05:00
[**--memory-swap**[=*LIMIT*]]
2016-03-06 07:29:23 -05:00
[**--network**[=*"default"*]]
2015-09-09 02:30:56 -04:00
[**--shm-size**[=*SHM-SIZE*]]
2015-05-19 16:57:41 -04:00
[**--cpu-period**[=*0*]]
2015-04-23 20:54:08 -04:00
[**--cpu-quota**[=*0*]]
2015-02-06 09:33:01 -05:00
[**--cpuset-cpus**[=*CPUSET-CPUS*]]
2015-05-19 16:57:41 -04:00
[**--cpuset-mems**[=*CPUSET-MEMS*]]
2015-07-22 22:26:06 -04:00
[**--ulimit**[=*[]*]]
2014-11-27 23:21:55 -05:00
PATH | URL | -
2014-04-16 14:07:55 -04:00
# DESCRIPTION
2014-04-17 11:36:58 -04:00
This will read the Dockerfile from the directory specified in **PATH** .
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 **ADD** commands found within the Dockerfile.
2014-04-16 14:07:55 -04:00
2014-04-17 11:36:58 -04:00
Warning, this will send a lot of data to the Docker daemon depending
2015-10-03 11:56:41 -04:00
on the contents of the current directory. The build is run by the Docker
2014-04-21 13:55:06 -04:00
daemon, not by the CLI, so the whole context must be transferred to the daemon.
2015-10-03 11:56:41 -04:00
The Docker CLI reports "Sending build context to Docker daemon" when the context is sent to
2014-04-21 13:55:06 -04:00
the daemon.
2014-04-17 11:36:58 -04:00
2015-04-04 10:54:43 -04:00
When the URL to a tarball archive or to a single Dockerfile is given, no context is sent from
2015-10-02 07:26:13 -04:00
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 **URL** , the repository is cloned locally and then sent as the context.
2014-04-16 14:07:55 -04:00
# OPTIONS
2015-03-11 12:04:30 -04:00
**-f**, ** --file**=*PATH/Dockerfile*
2015-04-04 10:54:43 -04:00
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 *Dockerfile* .
2014-09-11 10:42:17 -04:00
2016-04-21 12:08:37 -04:00
**--squash**=*true*|*false*
**Experimental Only**
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
2016-12-18 19:55:09 -05:00
image with the content of the squashed layers. This effectively makes it look
2016-04-21 12:08:37 -04:00
like all `Dockerfile` commands were created with a single layer. The build
cache is preserved with this method.
**Note** : 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.
**Note** : 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.
2014-11-14 13:59:14 -05:00
**--build-arg**=*variable*
2015-09-16 04:47:10 -04:00
name and value of a **buildarg** .
2014-11-14 13:59:14 -05:00
2015-09-16 04:47:10 -04:00
For example, if you want to pass a value for `http_proxy` , use
2015-09-23 20:38:09 -04:00
`--build-arg=http_proxy="http://some.proxy.url"`
2015-09-16 04:47:10 -04:00
Users pass these values at build-time. Docker uses the `buildargs` as the
environment context for command(s) run via the Dockerfile's `RUN` instruction
or for variable expansion in other Dockerfile instructions. This is not meant
2016-11-30 03:48:39 -05:00
for passing secret values. [Read more about the buildargs instruction ](https://docs.docker.com/engine/reference/builder/#arg )
2014-11-14 13:59:14 -05:00
2014-06-30 22:58:04 -04:00
**--force-rm**=*true*|*false*
Always remove intermediate containers, even after unsuccessful builds. The default is *false* .
2015-11-09 04:11:10 -05:00
**--isolation**="*default*"
Isolation specifies the type of isolation technology used by containers.
2016-03-25 14:16:19 -04:00
**--label**=*label*
Set metadata for an image
2014-06-30 22:58:04 -04:00
**--no-cache**=*true*|*false*
Do not use cache when building the image. The default is *false* .
2014-04-16 14:07:55 -04:00
2014-10-15 17:14:12 -04:00
**--help**
Print usage statement
2015-01-06 23:23:32 -05:00
**--pull**=*true*|*false*
Always attempt to pull a newer version of the image. The default is *false* .
2016-08-18 04:35:23 -04:00
**--compress**=*true*|*false*
Compress the build context using gzip. The default is *false* .
2014-04-16 14:07:55 -04:00
**-q**, ** --quiet**=*true*|*false*
2015-10-27 20:29:21 -04:00
Suppress the build output and print image ID on success. The default is *false* .
2014-04-16 14:07:55 -04:00
**--rm**=*true*|*false*
2014-06-30 22:58:04 -04:00
Remove intermediate containers after a successful build. The default is *true* .
2014-04-16 14:07:55 -04:00
2014-06-30 22:58:04 -04:00
**-t**, ** --tag**=""
2016-05-11 09:15:50 -04:00
Repository names (and optionally with tags) to be applied to the resulting
image in case of success. Refer to **docker-tag(1)** for more information
about valid tag names.
2014-04-16 14:07:55 -04:00
2015-05-19 16:57:41 -04:00
**-m**, ** --memory**=*MEMORY*
Memory limit
2015-12-29 20:23:35 -05:00
**--memory-swap**=*LIMIT*
A limit value equal to memory plus swap. Must be used with the ** -m**
(**--memory**) flag. The swap `LIMIT` should always be larger than ** -m**
(**--memory**) value.
The format of `LIMIT` is `<number>[<unit>]` . Unit can be `b` (bytes),
`k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you don't specify a
unit, `b` is used. Set LIMIT to `-1` to enable unlimited swap.
2015-05-19 16:57:41 -04:00
2016-12-13 14:58:02 -05:00
**--network**=*bridge*
2016-12-09 10:15:26 -05:00
Set the networking mode for the RUN instructions during build. Supported standard
values are: `bridge` , `host` , `none` and `container:<name|id>` . Any other value
is taken as a custom network's name or ID which this container should connect to.
2016-03-06 07:29:23 -05:00
2015-09-09 02:30:56 -04:00
**--shm-size**=*SHM-SIZE*
Size of `/dev/shm` . The format is `<number><unit>` . `number` must be greater than `0` .
Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes.
If you omit the size entirely, the system uses `64m` .
2015-10-16 15:05:18 -04:00
**--cpu-shares**=*0*
2015-05-19 16:57:41 -04:00
CPU shares (relative weight).
2015-10-02 07:26:13 -04:00
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:
```
cat /sys/fs/cgroup/cpu/cpu.shares
1024
```
You can change this proportion by adjusting the container's CPU share
weighting relative to the weighting of all other running containers.
2015-05-19 16:57:41 -04:00
2015-10-02 07:26:13 -04:00
To modify the proportion from the default of 1024, use the ** --cpu-shares**
flag to set the weighting to 2 or higher.
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)
2015-05-19 16:57:41 -04:00
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.
2015-10-02 07:26:13 -04:00
For example, consider three containers, where one has ** --cpu-shares=1024** and
two others have ** --cpu-shares=512**. When processes in all three
2015-05-19 16:57:41 -04:00
containers attempt to use 100% of CPU, the first container would receive
2015-10-02 07:26:13 -04:00
50% of the total CPU time. If you add a fourth container with ** --cpu-shares=1024**,
the first container only gets 33% of the CPU. The remaining containers
2015-05-19 16:57:41 -04:00
receive 16.5%, 16.5% and 33% of the CPU.
2015-10-02 07:26:13 -04:00
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%
2015-05-19 16:57:41 -04:00
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.
For example, consider a system with more than three cores. If you start one
2015-10-02 07:26:13 -04:00
container ** {C0}** with ** --cpu-shares=512** running one process, and another container
** {C1}** with ** --cpu-shares=1024** running two processes, this can result in the following
2015-05-19 16:57:41 -04:00
division of CPU shares:
PID container CPU CPU share
100 {C0} 0 100% of CPU0
101 {C1} 1 100% of CPU1
102 {C1} 2 100% of CPU2
**--cpu-period**=*0*
Limit the CPU CFS (Completely Fair Scheduler) period.
Limit the container's CPU usage. This flag causes the kernel to restrict the
container's CPU usage to the period you specify.
**--cpu-quota**=*0*
Limit the CPU CFS (Completely Fair Scheduler) quota.
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.
**--cpuset-cpus**=*CPUSET-CPUS*
CPUs in which to allow execution (0-3, 0,1).
**--cpuset-mems**=*CPUSET-MEMS*
2015-10-02 07:26:13 -04:00
Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on
2015-05-19 16:57:41 -04:00
NUMA systems.
For example, if you have four memory nodes on your system (0-3), use `--cpuset-mems=0,1`
to ensure the processes in your Docker container only use memory from the first
two memory nodes.
**--cgroup-parent**=*CGROUP-PARENT*
Path to `cgroups` under which the container's `cgroup` are created.
If the path is not absolute, the path is considered relative to the `cgroups` path of the init process.
Cgroups are created if they do not already exist.
2015-07-22 22:26:06 -04:00
**--ulimit**=[]
Ulimit options
For more information about `ulimit` see [Setting ulimits in a
2016-10-19 05:42:45 -04:00
container](https://docs.docker.com/engine/reference/commandline/run/#set-ulimits-in-container---ulimit)
2015-07-22 22:26:06 -04:00
2014-04-16 14:07:55 -04:00
# EXAMPLES
2015-01-26 22:19:02 -05:00
## Building an image using a Dockerfile located inside the current directory
2014-04-16 14:07:55 -04:00
2014-04-17 11:36:58 -04:00
Docker images can be built using the build command and a Dockerfile:
2014-04-16 14:07:55 -04:00
docker build .
2014-04-17 11:36:58 -04:00
During the build process Docker creates intermediate images. In order to
2014-04-21 13:55:06 -04:00
keep them, you must explicitly set `--rm=false` .
2014-04-16 14:07:55 -04:00
docker build --rm=false .
2014-04-17 11:36:58 -04:00
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.
2014-04-16 14:07:55 -04:00
2014-04-17 11:36:58 -04:00
It is also a good practice to add the files required for the image to the
2014-12-12 13:32:11 -05:00
sub-directory. These files will then be specified with the `COPY` or `ADD`
instructions in the `Dockerfile` .
Note: If you include a tar file (a good practice), then Docker will
automatically extract the contents of the tar file specified within the `ADD`
instruction into the specified target.
2014-04-16 14:07:55 -04:00
2014-05-27 13:56:11 -04:00
## Building an image and naming that image
2015-07-01 11:15:17 -04:00
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.
2014-05-27 13:56:11 -04:00
2014-05-27 14:05:48 -04:00
The ** -t**/**--tag** flag is used to rename an image. Here are some examples:
2014-05-27 13:56:11 -04:00
2015-02-01 21:39:28 -05:00
Though it is not a good practice, image names can be arbitrary:
2014-05-27 13:56:11 -04:00
docker build -t myimage .
2014-05-31 17:44:17 -04:00
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:
2014-05-27 13:56:11 -04:00
2015-10-02 07:26:13 -04:00
docker build -t fedora/jboss:1.0 .
2014-05-27 13:56:11 -04:00
2014-05-27 14:05:48 -04:00
The next example is for the "whenry" user repository and uses Fedora and
2014-05-31 17:44:17 -04:00
JBoss and gives it the version 2.1 :
2014-05-27 13:56:11 -04:00
2015-10-02 07:26:13 -04:00
docker build -t whenry/fedora-jboss:v2.1 .
2014-05-27 13:56:11 -04:00
2014-05-31 17:44:17 -04:00
If you do not provide a version tag then Docker will assign `latest` :
2014-05-27 13:56:11 -04:00
2015-10-02 07:26:13 -04:00
docker build -t whenry/fedora-jboss .
2014-05-31 17:44:17 -04:00
When you list the images, the image above will have the tag `latest` .
2014-05-27 13:56:11 -04:00
2015-08-30 09:48:03 -04:00
You can apply multiple tags to an image. For example, you can apply the `latest`
tag to a newly built image and add another tag that references a specific
version.
For example, to tag an image both as `whenry/fedora-jboss:latest` and
`whenry/fedora-jboss:v2.1` , use the following:
docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
2014-05-27 13:56:11 -04:00
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.
2014-04-16 14:07:55 -04:00
## Building an image using a URL
2015-06-13 12:21:50 -04:00
This will clone the specified GitHub repository from the URL and use it
2014-04-17 11:36:58 -04:00
as context. The Dockerfile at the root of the repository is used as
2015-06-13 12:21:50 -04:00
Dockerfile. This only works if the GitHub repository is a dedicated
2014-04-17 11:36:58 -04:00
repository.
2014-04-16 14:07:55 -04:00
2015-10-02 07:26:13 -04:00
docker build github.com/scollier/purpletest
2014-04-16 14:07:55 -04:00
2016-03-29 02:36:38 -04:00
Note: You can set an arbitrary Git repository via the `git://` scheme.
2014-04-16 14:07:55 -04:00
2015-04-04 10:54:43 -04:00
## Building an image using a URL to a tarball'ed context
This will send the URL itself to the Docker daemon. The daemon will fetch the
2015-10-02 07:26:13 -04:00
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 ** -f PATH/Dockerfile** option as well,
the system will look for that file inside the contents of the tarball.
2015-04-04 10:54:43 -04:00
docker build -f dev/Dockerfile https://10.10.10.1/docker/context.tar.gz
Note: supported compression formats are 'xz', 'bzip2', 'gzip' and 'identity' (no compression).
2015-11-09 04:11:10 -05:00
## Specify isolation technology for container (--isolation)
This option is useful in situations where you are running Docker containers on
Windows. The `--isolation=<value>` option sets a container's isolation
technology. On Linux, the only supported is the `default` option which uses
Linux namespaces. On Microsoft Windows, you can specify these values:
* `default` : Use the value specified by the Docker daemon's `--exec-opt` . If the `daemon` does not specify an isolation technology, Microsoft Windows uses `process` as its default value.
* `process` : Namespace isolation only.
* `hyperv` : Hyper-V hypervisor partition-based isolation.
Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"` .
2014-04-16 14:07:55 -04:00
# HISTORY
2014-04-17 11:36:58 -04:00
March 2014, Originally compiled by William Henry (whenry at redhat dot com)
2014-07-01 20:30:25 -04:00
based on docker.com source material and internal work.
2014-06-30 22:58:04 -04:00
June 2014, updated by Sven Dowideit < SvenDowideit @ home . org . au >
2015-07-01 11:15:17 -04:00
June 2015, updated by Sally O'Malley < somalley @ redhat . com >