question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SAM CDK -> sam build -> DockerImageCode.from_image_asset: Unable to set parent folder as the DockerContext

See original GitHub issue

Description:

I am having trouble with the following setup.

I have the following folder structure:

/root
  .Pipfile
  /shared_dependencies
  /project
    /runtime
      DockerFile

I need to set /root as the DockerContext and /root/project/runtime/DockerFile as the path to the Dockerfile

If I create a DockerImageCode instance and use it to create a DockerImageFunction like so:

code = DockerImageCode.from_image_asset(
  directory="/filesystem_path/root",
  file="/project/runtime/DockerFile",
)

DockerImageFunction(
  code=code,
)

cdk synth >| template.yaml will output a template and an AWS:Serverless:Function resource with the following metadata:

Metadata:
  aws:asset:path: /filesystem_path/root
  aws:asset:dockerfile-path: /project/runtime/DockerFile

My DockerFile contains the following:

COPY Pipfile .

To facilitate local development, I need to run sam build. When I do so, I get the following error:

COPY failed: file not found in build context or excluded by .dockerignore:
 stat Pipfile: file does not exist

Upon inspecting .aws-sam/build.toml, it would seem that the DockerContext is being set to /filesystem_path/root/project/runtime, the folder in which the DockerFile resides.

I would have assumed that the DockerContext would correspond to the directory parameter that gets passed into DockerImageCode.from_image_asset? (Which corresponds to aws:asset:path in the template)

As far as I can tell, there is no way of setting the DockerContext to any folder that is not the folder in which the DockerFile resides.

This pull request added the mapping logic for cdk image asset metadata -> sam build params.

Looking at the changes in this file, it would seem that the DockerContext will always be equal to the folder in which the DockerFile resides.

Is there a way of setting the DockerContext that I am not aware of? Or is it currently impossible?

I came across this issue which is now closed but does not offer a satisfactory resolution to this problem.

Additional environment details:

OS: Mac

sam --version -> SAM CLI, version 1.53.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mndevecicommented, Nov 11, 2022

Changes have been released with SAM CLI v1.62.0

1reaction
i18n-tribecommented, Jul 21, 2022

@jfuss I’ve submitted a PR to address this issue.

The code in the PR makes sam-cli behaviour consistent with how the cdk is working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assets - AWS Cloud Development Kit (AWS CDK) v2
Function construct, the code property lets you pass an asset (directory). ... You can define local files and directories as assets, and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found