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

feat(ecr-assets): custom docker files #5652

Merged
merged 5 commits into from
Jan 6, 2020
Merged

Conversation

iliapolo
Copy link
Contributor

@iliapolo iliapolo commented Jan 5, 2020

Added ability to provide a custom Dockerfile in the relevant asset constructs.

Those are:

Things to note

  1. The file property is assumed to be a relative path inside the asset directory. While its possible to specify a Dockerfile outside the context path, in our case, we want to keep things as self-contained as possible inside the asset directory. If a different need arrises, we will address it then.

  2. If the property is not defined, we use Dockerfile as the default. This happens in the constructor of DockerImageAsset.

  3. Resolving and validating the Dockerfile is done as soon as possible --> in the constructor. I didn't want the resolving logic to appear twice, so the prepareContainerAsset function simply adds the --file option to the docker command as is (if given).

  4. Prior to this change, users had to have a Dockerfile (hardcoded) in their asset directory.

Closes #3829


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@iliapolo
Copy link
Contributor Author

iliapolo commented Jan 5, 2020

@eladb Question:

I've changed the way we invoke docker build command. Is there an integration test covering this? How/When are those executed?

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Minor comments.

packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-ecs/lib/images/asset-image.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cx-api/lib/assets.ts Outdated Show resolved Hide resolved
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 6, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@mergify mergify bot merged commit 1b25a4b into master Jan 6, 2020
@mergify mergify bot deleted the epolon/custom-docker-files branch January 6, 2020 08:58
@@ -96,6 +106,7 @@ export class DockerImageAsset extends Construct implements assets.IAsset {
directoryName: staging.stagedPath,
dockerBuildArgs: props.buildArgs,
dockerBuildTarget: props.target,
dockerFile: file,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a relative path since the docker directory is copied into cloud assembly ("staging"). Since we resolve dir above, it results in an absolute path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think props.file would just work here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually done intentionally. The idea was to do the validation as soon as possible (like the other validations). To validate - I have to join the directory and the file. If I were to pass only the relative path here, it means the cli would have to join again - resulting in duplicate logic. I think its also inline with the notion of letting the framework do the work and keeping the cli as simple as possible.

I think what I missed though is that the final command would look something like:

docker build --file ${props.directory}/${props.file} /generated/path/to/cloudassemby/....

Which means the command relies on resources outside the cloudassemly. Is this what you meant by:

needs to be a relative path since the docker directory is copied into cloud assembly ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that the validation is done early, but the value passed to "docker build" needs to be relative to the docker directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the value can't be relative because docker will fail - it doesn't assume you pass a relative path to the context when you use --file.

The path passed to docker build --file should be absolute, it just needs to be derived from the asset path inside the cloud assembly. What has to be relative is the property passed to the asset, since otherwise, the manifest in the cloud-assembly will contain an absolute path of the machine the assembly was created on.


// THEN
const assetMetadata = stack.node.metadata.find(({ type }) => type === ASSET_METADATA);
test.deepEqual(assetMetadata && assetMetadata.data.file, path.join(directoryPath, 'Dockerfile.Custom'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion should have just been:

Suggested change
test.deepEqual(assetMetadata && assetMetadata.data.file, path.join(directoryPath, 'Dockerfile.Custom'));
test.deepEqual(assetMetadata && assetMetadata.data.file, 'Dockerfile.Custom');

@nalhabash
Copy link

Has anyone been able to use custom dockerfiles since the 1.20 release ?
Trying right now in my stack, with v1.21.1.

const nginxImage = new DockerImageAsset(this, 'NginxDockerImage', {
            directory: __dirname + '/../../app/',
            target: 'some_target',
            repositoryName: `some_repo`,
            file: 'Dockerfile.nginx',
        });

But I am getting this error Error response from daemon: failed to reach build target some_target in Dockerfile.

I have another php image that I build from a Dockerfile file, and it works fine.

@iliapolo
Copy link
Contributor Author

Hi @nalhabash

We indeed had an issue regarding this feature in 1.20.0 and 1.21.0, but version 1.21.1 should work properly. You can have a look here for some more information.

Regarding your specific issue, the error you are getting means that the Dockerfile.nginx file is missing the some_target target. Are you sure you defined it?

If possible, can you share your project structure and the contents of your Dockerfile.nginx file? This will help us figure out this issue quickly.

Thanks

@nalhabash
Copy link

Hi !
I don't know what is going on, I reverted to reproduce the issue and it looks like it works now. It might be that I somehow forgot to run npm run build or something silly like that, I don't know.
Still thanks for replying @iliapolo !

@iliapolo
Copy link
Contributor Author

Awesome, glad all is well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support custom dockerfile names
4 participants