ScanSkill

compose file build

This compose file build is a configuration option that is applied at build time. It can be specified as either an object with the path specified under context or as a string containing a path to the build context. Optionally, it may have dockerfile and args.

Example

build: ./dir

# OR,

build:
  context: ./dir
  dockerfile: Dockerfile-alternate
  args:
    buildno: 1

If you specify image with build then the image build will be named with the image name and optional tag specified in image. The image will be built from the ./dir.

build: ./dir
image: app:tag

Note: In the version 1 file formatbuild is different as:

  • Only the string form (build: .) is allowed - not the object form.
  • Using build together with image is not allowed. Attempting to do so results in an error.

context

The docker compose context is either a path to a directory containing Dockerfile or a URL to a git repository. When the value passed is a relative path, it’s interpreted as relative to the location of the compose file.

build:
  context: ./dir

dockerfile

It is used if you use an alternative Dockerfile.

Compose will use an alternative docker file to build with. A build path must also be specified.

build:
  context: .
  dockerfile: Dockerfile-alternate

args

This is used to add build arguments, which are environment variables accessible only during the build process.

  • Specify the arguments in your Dockerfile:
**ARG username
ARG secret

RUN echo "User name: $username"
RUN script-requiring-password.sh "$secret"**
  • Specify arguments under the build key:
# Using mapping
**build:
  context: .
  args:
    username: 1
    secret: secret

# Using list 
build:
  context: .
  args:
    - username=1
    - secret=secret**

You can omit value, in which case its value at build time is the environment value where compose is running.

args:
  - username
  - secret