You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(assets): support networking mode for DockerImageAsset (#18114)
As we are not allowed to specify networking mode for DockerImageAsset, users deploying cdk on containerized environment like Kubernetes will not be able to bundle assets without the build option `--network host`.
With this support, we are allowed to:
* [x] bundle image assets on a specific networking mode with the new `networkMode` property of `DockerImageAsset` from `aws-ecr-assets`.
* [x] bundle DockerImageFunction from aws-lambda on a specific networking mode.
* [x] bundle container images for AWS Fargate from on a specific networking mode.
Close#15516.
---
## The possible values of `--network`
According to Docker CLI, the default value for `--network` will be `default` if omitted.
```shell
$ docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
...
--network string Set the networking mode for the RUN instructions during build (default "default")
...
```
According to the [Docker Official Docs- API 1.25](https://docs.docker.com/engine/api/v1.25/#operation/ImageBuild):
> supported standard values are: `bridge`, `host`, `none`, and `container:<name|id>`. Any other value is taken as a custom network's name to which this container should connect to.
But according to [Source Code - docker/engine BuildKit](https://github.com/docker/engine/blob/8955d8da8951695a98eb7e15bead19d402c6eb27/builder/builder-next/builder.go#L308-L314), the value `bridge` is not accepted by BuildKit & should use the value `default` instead.
Therefore, the static values for `NetworkMode` are `default`, `host` & `none`, with 2 static functions `NetworkMode.fromContainer()` to construct a `container:<name|id>` & `NetworkMode.custom()` to construct a custom networking mode.
```
$ DOCKER_BUILDKIT=1 docker build --network=bridge .
Error response from daemon: network mode "bridge" not supported by buildkit
```
References:
* [Docker Official Docs- API 1.25](https://docs.docker.com/engine/api/v1.25/#operation/ImageBuild)
* [Docker Official Docs - Use the default bridge network](https://docs.docker.com/network/bridge/#use-the-default-bridge-network)
* [Source Code - docker/engine BuildKit](https://github.com/docker/engine/blob/8955d8da8951695a98eb7e15bead19d402c6eb27/builder/builder-next/builder.go#L308-L314)
---
## Builder experience with `aws-ecr-assets`
Specify `networkMode` with `DEFAULT` or `HOST` for docker image assets
```ts
new assets.DockerImageAsset(stack, 'DockerImage', {
directory: path.join(__dirname, 'demo-image'),
networkMode: NetworkMode.HOST,
});
```
## Builder experience with `aws-ecs`
Specify `networkMode` with `DEFAULT` or `HOST` for container image
```ts
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromAsset(path.join(__dirname, '../demo-image'), {
networkMode: NetworkMode.DEFAULT,
}),
portMappings: [{
containerPort: 8000,
}],
});
```
## Builder experience with `aws-lambda`
Specify `networkMode` with `DEFAULT` or `HOST` from docker image assets
```ts
new DockerImageFunction(this, 'MyLambda', {
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler'), {
networkMode: NetworkMode.DEFAULT,
}),
});
```
0 commit comments