aws / aws/aws-cdk

(aws-ecr-assets): Add support to DockerImageAsset for absolute/relative paths outside of context directory for Dockerfiles (file parameter)

Open
#14,683 4 comments 5 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecr-assets feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

Add support to [`DockerImageAsset`](https://github.com/aws/aws-cdk/blob/v1.103.0/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts) so that the `file` parameter can refer to Dockerfiles outside of the build context.

One of the causes of this restriction is this [check](https://github.com/aws/aws-cdk/blob/v1.103.0/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts#L114), which (effectively) prevents referencing a Dockerfile outside of the context directory. This is because the image is built from within the `cdk.out` directory, which changes the location the relative path points to.

### Use Case

Using one or more standardised Dockerfiles that do not change module to module, with this feature the Dockerfile could be maintained centrally in a repository.

For example, currently you might have a layout like this, where each Dockerfile is identical:
```
lambdas/
lambda1/
Dockerfile
lambda2/
Dockerfile
batch-jobs/
job1/
Dockerfile
```
Which could instead be:
```
dockerfiles/
Dockerfile
lambdas/
lambda1/
lambda2/
batch-jobs/
job1/
```

I think this could enabling developers that want to extend the CDK. For services such as ECS, Batch or Lambda, the capability to be able to easily bundle Dockerfiles in third party libraries would be very useful and help to avoid manual steps that require a developer to copy and paste boilerplate files to get up and running.

### Proposed Solution

I can think of a couple of approaches.

Modify the existing check to see if `path` exists as an absolute path [here](https://github.com/aws/aws-cdk/blob/v1.103.0/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts#L114), and then either:
a. Refer to the Dockerfile using it's absolute path when building the image
or
b. Copy the Dockerfile as an asset and refer to it using it's relative path when building the image

### Other

* [x] :wave: I may be able to implement this feature request
* [ ] :warning: This feature might incur a breaking change

I believe I could complete this feature request using approach a, but I think I would need some pointers for approach b.

---

This is a :rocket: Feature Request

Contributor guide

Open the contributing guide

Research direction

Start in packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts, especially the check around line 114 and the DockerImageAsset build path. Trace how builds run from cdk.out and how the file parameter is resolved. Done means absolute and relative Dockerfile paths outside the context work without breaking the existing context behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, typescript
Domain
devops, infrastructure
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.