# DockerImageAsset

An asset that represents a Docker image.

The image will be created in build time and uploaded to an ECR repository.

Extends: Construct › DockerImageAsset
Implements: IConstruct, IDependable

- Source: [`aws-ecr-assets/lib/image-asset.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ecr-assets/lib/image-asset.ts#L390)
- Type last changed: v2.196.0 · 16 May 2025

## Example

```ts
import { DockerImageAsset } from 'aws-cdk-lib/aws-ecr-assets';

const asset = new DockerImageAsset(this, 'MyBuildImage', {
  directory: path.join(__dirname, 'my-image'),
  buildContexts: {
    mycontext: path.join(__dirname, 'path/to/context'),
    alpine: 'docker-image://alpine:latest',
  },
});
```

## Initializer

```ts
new DockerImageAsset(scope: Construct, id: string, props: DockerImageAssetProps)
```

## Construct Props

- `assetName`: string — Unique identifier of the docker image asset and its potential revisions.

  **Default:** no asset name

  Required if using AppScopedStagingSynthesizer.

- `buildArgs`: { [key: string]: string } — Build args to pass to the `docker build` command.

  **Default:** no build args are passed

  Since Docker build arguments are resolved before deployment, keys and
  values cannot refer to unresolved tokens (such as `lambda.functionArn` or
  `queue.queueUrl`).

- `buildContexts`: { [key: string]: string } — Build contexts to pass to the `docker build` command.

  **Default:** no additional build contexts

  Build contexts can be used to specify additional directories or images
  to use during the build. Each entry specifies a named build context
  and its source (a directory path, a URL, or a docker image).

  Since Docker build contexts are resolved before deployment, keys and
  values cannot refer to unresolved tokens (such as `lambda.functionArn` or
  `queue.queueUrl`).

- `buildSecrets`: { [key: string]: string } — Build secrets.

  **Default:** no build secrets

  Docker BuildKit must be enabled to use build secrets.

  ```ts
  import { DockerBuildSecret } from 'aws-cdk-lib';

  const buildSecrets = {
    'MY_SECRET': DockerBuildSecret.fromSrc('file.txt')
  };
  ```

- `buildSsh`: string — SSH agent socket or keys to pass to the `docker build` command.

  **Default:** no --ssh flag

  Docker BuildKit must be enabled to use the ssh flag

- `cacheDisabled`: boolean — Disable the cache and pass `--no-cache` to the `docker build` command.

  **Default:** cache is used

- `cacheFrom`: [DockerCacheOption](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.DockerCacheOption.html)[] — Cache from options to pass to the `docker build` command.

  **Default:** no cache from options are passed to the build command

- `cacheTo`: [DockerCacheOption](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.DockerCacheOption.html) — Cache to options to pass to the `docker build` command.

  **Default:** no cache to options are passed to the build command

- `directory`: string (required) — The directory where the Dockerfile is stored.

  Any directory inside with a name that matches the CDK output folder (cdk.out by default) will be excluded from the asset

- `displayName`: string — A display name for this asset.

  **Default:** Stack-relative construct path

  If supplied, the display name will be used in locations where the asset
  identifier is printed, like in the CLI progress information. If the same
  asset is added multiple times, the display name of the first occurrence is
  used.

  If `assetName` is given, it will also be used as the default `displayName`.
  Otherwise, the default is the construct path of the ImageAsset construct,
  with respect to the enclosing stack. If the asset is produced by a
  construct helper function (such as `lambda.Code.fromAssetImage()`), this
  will look like `MyFunction/AssetImage`.

  We use the stack-relative construct path so that in the common case where
  you have multiple stacks with the same asset, we won't show something like
  `/MyBetaStack/MyFunction/Code` when you are actually deploying to
  production.

- `exclude`: string[] — File paths matching the patterns will be excluded.

  **Default:** nothing is excluded

  See `ignoreMode` to set the matching behavior.
  Has no effect on Assets bundled using the `bundling` property.

- `extraHash`: string — Extra information to encode into the fingerprint (e.g. build instructions and other inputs).

  **Default:** hash is only based on source content

- `file`: string = 'Dockerfile' — Path to the Dockerfile (relative to the directory).

- `followSymlinks`: [SymlinkFollowMode](/cdk/api/v2/docs/aws-cdk-lib.SymlinkFollowMode.html) = SymlinkFollowMode.NEVER — A strategy for how to handle symlinks.

- `ignoreMode`: [IgnoreMode](/cdk/api/v2/docs/aws-cdk-lib.IgnoreMode.html) = IgnoreMode.GLOB — The ignore behavior to use for `exclude` patterns.

- `invalidation`: [DockerImageAssetInvalidationOptions](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.DockerImageAssetInvalidationOptions.html) — Options to control which parameters are used to invalidate the asset hash.

  **Default:** hash all parameters

- `networkMode`: [NetworkMode](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.NetworkMode.html) — Networking mode for the RUN commands during build.

  **Default:** no networking mode specified (the default networking mode `NetworkMode.DEFAULT` will be used)

  Support docker API 1.25+.

- `outputs`: string[] — Outputs to pass to the `docker build` command.

  **Default:** no outputs are passed to the build command (default outputs are used)

- `platform`: [Platform](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.Platform.html) — Platform to build for.

  **Default:** no platform specified (the current machine architecture will be used)

  _Requires Docker Buildx_.

- `target`: string — Docker target to build to.

  **Default:** no target

## Properties

- `assetHash`: string — A hash of this asset, which is available at construction time.

  As this is a plain string, it
  can be used in construct IDs in order to enforce creation of a new resource when the content
  hash has changed.

- `imageTag`: string — The tag of this asset when it is uploaded to ECR.

  The tag may differ from the assetHash if a stack synthesizer adds a dockerTagPrefix.

- `imageUri`: string — The full URI of the image (including a tag).

  Use this reference to pull
  the asset.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `repository`: [IRepository](/cdk/api/v2/docs/aws-cdk-lib.aws_ecr.IRepository.html) — Repository where the image is stored.

## Methods

- `addResourceMetadata(resource: CfnResource, resourceProperty: string): void` — Adds CloudFormation template metadata to the specified resource with information that indicates which resource property is mapped to this local asset.

  This can be used by tools such as SAM CLI to provide local
  experience such as local invocation and debugging of Lambda functions.

  Asset metadata will only be included if the stack is synthesized with the
  "aws:cdk:enable-asset-metadata" context key defined, which is the default
  behavior when synthesizing via the CDK Toolkit.

  **Parameters**

  - `resource` — The CloudFormation resource which is using this asset [disable-awslint:ref-via-interface].
  - `resourceProperty` — The property name where this asset is referenced.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
