Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DockerImageAsset is missing build context #3342

Closed
1 of 5 tasks
KnisterPeter opened this issue Jul 18, 2019 · 13 comments
Closed
1 of 5 tasks

DockerImageAsset is missing build context #3342

KnisterPeter opened this issue Jul 18, 2019 · 13 comments
Assignees
Labels
@aws-cdk/assets Related to the @aws-cdk/assets package closing-soon This issue will automatically close in 4 days unless further comments are made. feature-request A feature should be added or improved.

Comments

@KnisterPeter
Copy link
Contributor

  • I'm submitting a ...

    • πŸͺ² bug report
    • πŸš€ feature request
    • πŸ“š construct library gap
    • ☎️ security issue or vulnerability => Please see policy
    • ❓ support request => Please see note at the top of this template.
  • What is the current behavior?
    If the current behavior is a πŸͺ²bugπŸͺ²: Please provide the steps to reproduce

Right now there is not option to specify the build context for the docker build.
E.g. if the Dockerfile requires files from .. the build context need to be at least '..' instead of '.' which is the default.

  • What is the expected behavior (or behavior of feature suggested)?

There should be an option to specify the context. This could be tricky, since CDK stages the files which would then mean to stage the entire build context.

  • What is the motivation / use case for changing the behavior or adding this feature?

It is an advanced use case for building docker images in for example mono-repositories where there are dependencies into other folders.

  • Please tell us about your environment:

    • CDK CLI Version: 0.36.0
    • Module Version: 0.36.0
    • OS: all
    • Language: TypeScript
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

@KnisterPeter KnisterPeter added the needs-triage This issue or PR still needs to be triaged. label Jul 18, 2019
@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 28, 2019

I'm going to need more context. DockerImageAsset takes a directory parameter. Does that not suffice?

@rix0rrr rix0rrr added @aws-cdk/assets Related to the @aws-cdk/assets package response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Aug 28, 2019
@KnisterPeter
Copy link
Contributor Author

If the folder your docker file is in requires files outsite site folder (up in the fs hierarchy) you need to define a build context.
By default this context is the Directory where the Dockerfile lives. But that isn't required.

https://docs.docker.com/engine/reference/commandline/build/

@SomayaB SomayaB added feature-request A feature should be added or improved. and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Oct 17, 2019
@mbruning24
Copy link
Contributor

@rix0rrr a good use case for this (at least IMO) is when you have multiple Dockerfiles and they each use the same build context. I'm building a selenium testing environment where I have chrome and firefox images that I want to utilize the same set of nodejs scripts to handle running (protractor, cucumber, etc etc etc) and I want to manage them both in the same CDK project without having to copy the files.

In that case it would be awesome to either pass a custom dockerfile location (like #3829):

{ directory: '.', dockerfile: 'lib/xyz/chrome/Dockerfile' }
...
{ directory: '.', dockerfile: 'lib/xyz/firefox/Dockerfile' }

Or to allow overriding contextPath when docker images are built.

@eladb
Copy link
Contributor

eladb commented Nov 4, 2019

@SomayaB please assign all asset issues to me :)

@eladb eladb assigned SomayaB and unassigned rix0rrr Nov 4, 2019
@SomayaB SomayaB assigned eladb and unassigned SomayaB Nov 4, 2019
@kingwill101
Copy link

i believe this would be a great solution to the issue i'm currently having https://stackoverflow.com/questions/58838213/allowing-stack-to-create-multiple-ecr-images

@eladb eladb assigned iliapolo and unassigned eladb Jan 23, 2020
@eladb
Copy link
Contributor

eladb commented Jan 23, 2020

@iliapolo do you think this is something or this will make asset staging hell?

@iliapolo
Copy link
Contributor

iliapolo commented Mar 9, 2020

@eladb I think exposing the context directory might introduce a slue of unnecessary problems. I believe the issues discussed here can be better solved otherwise.

@mbruning24 We have now added support for custom docker files: (#3829). Does this solve the issue you were facing?

@glenfordwilliams Its been a while since your latest comment and I think the issue you addressed should now simply work. The buildArgs are considered when creating the asset hash, which means that a different image is created for each buildArgs combo. Note that we now don't create a separate ECR repo for every image, but rather put all of them in a shared one.

But I think this should still be ok because i believe the initial problem was that it didn't actually create separate images based on the build args, right?

@iliapolo iliapolo added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 11, 2020
@SomayaB SomayaB added closing-soon This issue will automatically close in 4 days unless further comments are made. and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 19, 2020
@SomayaB
Copy link
Contributor

SomayaB commented Mar 25, 2020

Closing for now since there hasn't been a response in a while. Feel free to reopen.

@Kilowhisky
Copy link

Kilowhisky commented Mar 23, 2023

I know this is closed but this just happened to me today. For example if you are trying to build a Docker image from a VS C# project, the context has to be set to .. . See https://learn.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022#docker-build

To build a containerized solution from the command line, you can usually use the command docker build for each project in the solution. You provide the build context argument. The build context for a Dockerfile is the folder on the local machine that's used as the working folder to generate the image. For example, it's the folder that you copy files from when you copy to the container. In .NET Core projects, use the folder that contains the solution file (.sln). Expressed as a relative path, this argument is typically ".." for a Dockerfile in a project folder, and the solution file in its parent folder. For .NET Framework projects, the build context is the project folder, not the solution folder.

It is currently not possible to do this with DockerImageAsset if you are using C# as the CDK language. Which means, you cannot easily build VisualStudio based Dockerfile projects.

@mluksch
Copy link

mluksch commented Dec 24, 2023

Would be great, if this could get reopened again.
There is currently no way afaik to separate the "build context" from the "dockerfile" in cdk with DockerImageCode. With the status quo the Dockerfile has to be placed within the build context. But this makes it difficult to write a custom Construct that already contains a Dockerfile but where you just want to parameterize the build context with the source code on the consuming side of the custom Construct.

// LambdaWebApp = a Custom Construct imported from an npm-dependency
import { LambdaWebApp } from "common-infra-lib";

  new LambdaWebApp(this, "test", {
    code: cdk.aws_lambda.DockerImageCode.fromImageAsset(
        // This is the build context with the source code:
         "..", {
         // This Dockerfile should rather get provided by the custom construct in the npm-dependency 
         // itself, but this isnt possible yet because the Dockerfile has to be placed in the build context here:
         file: "ProdDockerfile",
    }),
    subdomain: "test",
  });

@andrew-hossack
Copy link

Any updates on this would be greatly appreciated.

@xbreid
Copy link

xbreid commented Mar 24, 2024

+1

@fernicolosi
Copy link

I was having the same issue and a combination of directory and file properties did the trick for me:

import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";

const image = new DockerImageAsset(this, "Image", {
  directory: "../../../", <--- This is the path to your docker build context 
  file: "[PATH-TO-DOCKERFILE]/Dockerfile",
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assets Related to the @aws-cdk/assets package closing-soon This issue will automatically close in 4 days unless further comments are made. feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests