github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/man/docker-build.1.md (about)

     1  % DOCKER(1) Docker User Manuals
     2  % Docker Community
     3  % JUNE 2014
     4  # NAME
     5  docker-build - Build a new image from the source code at PATH
     6  
     7  # SYNOPSIS
     8  **docker build**
     9  [**--build-arg**[=*[]*]]
    10  [**--cpu-shares**[=*0*]]
    11  [**--cgroup-parent**[=*CGROUP-PARENT*]]
    12  [**--help**]
    13  [**-f**|**--file**[=*PATH/Dockerfile*]]
    14  [**-squash**] *Experimental*
    15  [**--force-rm**]
    16  [**--isolation**[=*default*]]
    17  [**--label**[=*[]*]]
    18  [**--no-cache**]
    19  [**--pull**]
    20  [**--compress**]
    21  [**-q**|**--quiet**]
    22  [**--rm**[=*true*]]
    23  [**-t**|**--tag**[=*[]*]]
    24  [**-m**|**--memory**[=*MEMORY*]]
    25  [**--memory-swap**[=*LIMIT*]]
    26  [**--network**[=*"default"*]]
    27  [**--shm-size**[=*SHM-SIZE*]]
    28  [**--cpu-period**[=*0*]]
    29  [**--cpu-quota**[=*0*]]
    30  [**--cpuset-cpus**[=*CPUSET-CPUS*]]
    31  [**--cpuset-mems**[=*CPUSET-MEMS*]]
    32  [**--ulimit**[=*[]*]]
    33  PATH | URL | -
    34  
    35  # DESCRIPTION
    36  This will read the Dockerfile from the directory specified in **PATH**.
    37  It also sends any other files and directories found in the current
    38  directory to the Docker daemon. The contents of this directory would
    39  be used by **ADD** commands found within the Dockerfile.
    40  
    41  Warning, this will send a lot of data to the Docker daemon depending
    42  on the contents of the current directory. The build is run by the Docker
    43  daemon, not by the CLI, so the whole context must be transferred to the daemon. 
    44  The Docker CLI reports "Sending build context to Docker daemon" when the context is sent to
    45  the daemon.
    46  
    47  When the URL to a tarball archive or to a single Dockerfile is given, no context is sent from
    48  the client to the Docker daemon. In this case, the Dockerfile at the root of the archive and
    49  the rest of the archive will get used as the context of the build.  When a Git repository is
    50  set as the **URL**, the repository is cloned locally and then sent as the context.
    51  
    52  # OPTIONS
    53  **-f**, **--file**=*PATH/Dockerfile*
    54     Path to the Dockerfile to use. If the path is a relative path and you are
    55     building from a local directory, then the path must be relative to that
    56     directory. If you are building from a remote URL pointing to either a
    57     tarball or a Git repository, then the path must be relative to the root of
    58     the remote context. In all cases, the file must be within the build context.
    59     The default is *Dockerfile*.
    60  
    61  **--squash**=*true*|*false*
    62     **Experimental Only**
    63     Once the image is built, squash the new layers into a new image with a single
    64     new layer. Squashing does not destroy any existing image, rather it creates a new
    65     image with the content of the squshed layers. This effectively makes it look
    66     like all `Dockerfile` commands were created with a single layer. The build
    67     cache is preserved with this method.
    68  
    69     **Note**: using this option means the new image will not be able to take
    70     advantage of layer sharing with other images and may use significantly more
    71     space.
    72  
    73     **Note**: using this option you may see significantly more space used due to
    74     storing two copies of the image, one for the build cache with all the cache
    75     layers in tact, and one for the squashed version.
    76  
    77  **--build-arg**=*variable*
    78     name and value of a **buildarg**.
    79  
    80     For example, if you want to pass a value for `http_proxy`, use
    81     `--build-arg=http_proxy="http://some.proxy.url"`
    82  
    83     Users pass these values at build-time. Docker uses the `buildargs` as the
    84     environment context for command(s) run via the Dockerfile's `RUN` instruction
    85     or for variable expansion in other Dockerfile instructions. This is not meant
    86     for passing secret values. [Read more about the buildargs instruction](https://docs.docker.com/engine/reference/builder/#arg)
    87  
    88  **--force-rm**=*true*|*false*
    89     Always remove intermediate containers, even after unsuccessful builds. The default is *false*.
    90  
    91  **--isolation**="*default*"
    92     Isolation specifies the type of isolation technology used by containers. 
    93  
    94  **--label**=*label*
    95     Set metadata for an image
    96  
    97  **--no-cache**=*true*|*false*
    98     Do not use cache when building the image. The default is *false*.
    99  
   100  **--help**
   101    Print usage statement
   102  
   103  **--pull**=*true*|*false*
   104     Always attempt to pull a newer version of the image. The default is *false*.
   105  
   106  **--compress**=*true*|*false*
   107      Compress the build context using gzip. The default is *false*.
   108  
   109  **-q**, **--quiet**=*true*|*false*
   110     Suppress the build output and print image ID on success. The default is *false*.
   111  
   112  **--rm**=*true*|*false*
   113     Remove intermediate containers after a successful build. The default is *true*.
   114  
   115  **-t**, **--tag**=""
   116     Repository names (and optionally with tags) to be applied to the resulting 
   117     image in case of success. Refer to **docker-tag(1)** for more information
   118     about valid tag names.
   119  
   120  **-m**, **--memory**=*MEMORY*
   121    Memory limit
   122  
   123  **--memory-swap**=*LIMIT*
   124     A limit value equal to memory plus swap. Must be used with the  **-m**
   125  (**--memory**) flag. The swap `LIMIT` should always be larger than **-m**
   126  (**--memory**) value.
   127  
   128     The format of `LIMIT` is `<number>[<unit>]`. Unit can be `b` (bytes),
   129  `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you don't specify a
   130  unit, `b` is used. Set LIMIT to `-1` to enable unlimited swap.
   131  
   132  **--network**=*NETWORK*
   133    
   134  
   135  **--shm-size**=*SHM-SIZE*
   136    Size of `/dev/shm`. The format is `<number><unit>`. `number` must be greater than `0`.
   137    Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes.
   138    If you omit the size entirely, the system uses `64m`.
   139  
   140  **--cpu-shares**=*0*
   141    CPU shares (relative weight).
   142  
   143    By default, all containers get the same proportion of CPU cycles.
   144    CPU shares is a 'relative weight', relative to the default setting of 1024.
   145    This default value is defined here: 
   146    ```
   147     cat /sys/fs/cgroup/cpu/cpu.shares
   148     1024
   149    ```
   150    You can change this proportion by adjusting the container's CPU share 
   151    weighting relative to the weighting of all other running containers.
   152  
   153    To modify the proportion from the default of 1024, use the **--cpu-shares**
   154    flag to set the weighting to 2 or higher.
   155  
   156        Container   CPU share    Flag             
   157        {C0}        60% of CPU  --cpu-shares=614 (614 is 60% of 1024)
   158        {C1}        40% of CPU  --cpu-shares=410 (410 is 40% of 1024)
   159  
   160    The proportion is only applied when CPU-intensive processes are running.
   161    When tasks in one container are idle, the other containers can use the
   162    left-over CPU time. The actual amount of CPU time used varies depending on
   163    the number of containers running on the system.
   164  
   165    For example, consider three containers, where one has **--cpu-shares=1024** and
   166    two others have **--cpu-shares=512**. When processes in all three
   167    containers attempt to use 100% of CPU, the first container would receive
   168    50% of the total CPU time. If you add a fourth container with **--cpu-shares=1024**,
   169    the first container only gets 33% of the CPU. The remaining containers
   170    receive 16.5%, 16.5% and 33% of the CPU.
   171  
   172  
   173        Container   CPU share   Flag                CPU time            
   174        {C0}        100%        --cpu-shares=1024   33%
   175        {C1}        50%         --cpu-shares=512    16.5%
   176        {C2}        50%         --cpu-shares=512    16.5%
   177        {C4}        100%        --cpu-shares=1024   33%
   178  
   179  
   180    On a multi-core system, the shares of CPU time are distributed across the CPU
   181    cores. Even if a container is limited to less than 100% of CPU time, it can
   182    use 100% of each individual CPU core.
   183  
   184    For example, consider a system with more than three cores. If you start one
   185    container **{C0}** with **--cpu-shares=512** running one process, and another container
   186    **{C1}** with **--cpu-shares=1024** running two processes, this can result in the following
   187    division of CPU shares:
   188  
   189        PID    container    CPU    CPU share
   190        100    {C0}         0      100% of CPU0
   191        101    {C1}         1      100% of CPU1
   192        102    {C1}         2      100% of CPU2
   193  
   194  **--cpu-period**=*0*
   195    Limit the CPU CFS (Completely Fair Scheduler) period.
   196  
   197    Limit the container's CPU usage. This flag causes the kernel to restrict the
   198    container's CPU usage to the period you specify.
   199  
   200  **--cpu-quota**=*0*
   201    Limit the CPU CFS (Completely Fair Scheduler) quota. 
   202  
   203    By default, containers run with the full CPU resource. This flag causes the
   204  kernel to restrict the container's CPU usage to the quota you specify.
   205  
   206  **--cpuset-cpus**=*CPUSET-CPUS*
   207    CPUs in which to allow execution (0-3, 0,1).
   208  
   209  **--cpuset-mems**=*CPUSET-MEMS*
   210    Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on
   211    NUMA systems.
   212  
   213    For example, if you have four memory nodes on your system (0-3), use `--cpuset-mems=0,1`
   214  to ensure the processes in your Docker container only use memory from the first
   215  two memory nodes.
   216  
   217  **--cgroup-parent**=*CGROUP-PARENT*
   218    Path to `cgroups` under which the container's `cgroup` are created.
   219  
   220    If the path is not absolute, the path is considered relative to the `cgroups` path of the init process.
   221  Cgroups are created if they do not already exist.
   222  
   223  **--ulimit**=[]
   224    Ulimit options
   225  
   226    For more information about `ulimit` see [Setting ulimits in a 
   227  container](https://docs.docker.com/engine/reference/commandline/run/#set-ulimits-in-container---ulimit)
   228  
   229  # EXAMPLES
   230  
   231  ## Building an image using a Dockerfile located inside the current directory
   232  
   233  Docker images can be built using the build command and a Dockerfile:
   234  
   235      docker build .
   236  
   237  During the build process Docker creates intermediate images. In order to
   238  keep them, you must explicitly set `--rm=false`.
   239  
   240      docker build --rm=false .
   241  
   242  A good practice is to make a sub-directory with a related name and create
   243  the Dockerfile in that directory. For example, a directory called mongo may
   244  contain a Dockerfile to create a Docker MongoDB image. Likewise, another
   245  directory called httpd may be used to store Dockerfiles for Apache web
   246  server images.
   247  
   248  It is also a good practice to add the files required for the image to the
   249  sub-directory. These files will then be specified with the `COPY` or `ADD`
   250  instructions in the `Dockerfile`.
   251  
   252  Note: If you include a tar file (a good practice), then Docker will
   253  automatically extract the contents of the tar file specified within the `ADD`
   254  instruction into the specified target.
   255  
   256  ## Building an image and naming that image
   257  
   258  A good practice is to give a name to the image you are building. Note that 
   259  only a-z0-9-_. should be used for consistency.  There are no hard rules here but it is best to give the names consideration. 
   260  
   261  The **-t**/**--tag** flag is used to rename an image. Here are some examples:
   262  
   263  Though it is not a good practice, image names can be arbitrary:
   264  
   265      docker build -t myimage .
   266  
   267  A better approach is to provide a fully qualified and meaningful repository,
   268  name, and tag (where the tag in this context means the qualifier after 
   269  the ":"). In this example we build a JBoss image for the Fedora repository 
   270  and give it the version 1.0:
   271  
   272      docker build -t fedora/jboss:1.0 .
   273  
   274  The next example is for the "whenry" user repository and uses Fedora and
   275  JBoss and gives it the version 2.1 :
   276  
   277      docker build -t whenry/fedora-jboss:v2.1 .
   278  
   279  If you do not provide a version tag then Docker will assign `latest`:
   280  
   281      docker build -t whenry/fedora-jboss .
   282  
   283  When you list the images, the image above will have the tag `latest`.
   284  
   285  You can apply multiple tags to an image. For example, you can apply the `latest`
   286  tag to a newly built image and add another tag that references a specific
   287  version.
   288  For example, to tag an image both as `whenry/fedora-jboss:latest` and
   289  `whenry/fedora-jboss:v2.1`, use the following:
   290  
   291      docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
   292  
   293  So renaming an image is arbitrary but consideration should be given to 
   294  a useful convention that makes sense for consumers and should also take
   295  into account Docker community conventions.
   296  
   297  
   298  ## Building an image using a URL
   299  
   300  This will clone the specified GitHub repository from the URL and use it
   301  as context. The Dockerfile at the root of the repository is used as
   302  Dockerfile. This only works if the GitHub repository is a dedicated
   303  repository.
   304  
   305      docker build github.com/scollier/purpletest
   306  
   307  Note: You can set an arbitrary Git repository via the `git://` scheme.
   308  
   309  ## Building an image using a URL to a tarball'ed context
   310  
   311  This will send the URL itself to the Docker daemon. The daemon will fetch the
   312  tarball archive, decompress it and use its contents as the build context.  The 
   313  Dockerfile at the root of the archive and the rest of the archive will get used
   314  as the context of the build. If you pass an **-f PATH/Dockerfile** option as well,
   315  the system will look for that file inside the contents of the tarball.
   316  
   317      docker build -f dev/Dockerfile https://10.10.10.1/docker/context.tar.gz
   318  
   319  Note: supported compression formats are 'xz', 'bzip2', 'gzip' and 'identity' (no compression).
   320  
   321  ## Specify isolation technology for container (--isolation)
   322  
   323  This option is useful in situations where you are running Docker containers on
   324  Windows. The `--isolation=<value>` option sets a container's isolation
   325  technology. On Linux, the only supported is the `default` option which uses
   326  Linux namespaces. On Microsoft Windows, you can specify these values:
   327  
   328  * `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.
   329  * `process`: Namespace isolation only.
   330  * `hyperv`: Hyper-V hypervisor partition-based isolation.
   331  
   332  Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`.
   333  
   334  # HISTORY
   335  March 2014, Originally compiled by William Henry (whenry at redhat dot com)
   336  based on docker.com source material and internal work.
   337  June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
   338  June 2015, updated by Sally O'Malley <somalley@redhat.com>