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

Correctly ignore files from .dockerignore #508

Merged
merged 14 commits into from
Mar 7, 2023
Merged

Conversation

guineveresaenger
Copy link
Contributor

This PR fixes a bug discovered by a trial user:

The provider copied __pycache__ dirs into the image, but our dockerignore contains the line **/__pycache__/**

It appears the Docker client does not handle the dockerignore file on its own, so this PR adds logic to ignore file patterns read from the dockerignore file.
The .dockerignore file is always expected to be at the root of the build context, both by convention and by design.

We read the to-be-ignored patterns into a slice of strings, which can then be passed to the TarOptions as ExcludePatterns.
The provider will now build an image which properly excludes the ignore files.

Additionally this PR creates a separate function to obtain the ignore file slice, since the hash function uses this information too.
This also fixes a minor bug in the hash function.

  • Exclude files from dockerignore when uploading context tarball
  • clean up ignore patterns
  • Refactor ignorePatterns into separate function. Fix dockerignore path bug in hash function.

@guineveresaenger guineveresaenger self-assigned this Feb 28, 2023
@guineveresaenger guineveresaenger added 4.x.x kind/bug Some behavior is incorrect or out of spec labels Feb 28, 2023
@github-actions
Copy link

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

1 similar comment
@github-actions
Copy link

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@github-actions
Copy link

github-actions bot commented Mar 1, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

2 similar comments
@github-actions
Copy link

github-actions bot commented Mar 1, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@github-actions
Copy link

github-actions bot commented Mar 1, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

provider/provider.go Outdated Show resolved Hide resolved
examples/dockerfile-go/.dockerignore Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Mar 2, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

Copy link
Contributor

@jazzyfresh jazzyfresh left a comment

Choose a reason for hiding this comment

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

LGTM, made some comments but fine to merge as is

examples/examples_go_test.go Outdated Show resolved Hide resolved
examples/examples_nodejs_test.go Outdated Show resolved Hide resolved
provider/image.go Outdated Show resolved Hide resolved
dockerIgnore, err := os.ReadFile(dockerIgnorePath)
if err != nil {
if os.IsNotExist(err) {
// .dockerignore files are optional
Copy link
Contributor

Choose a reason for hiding this comment

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

great comment! i found it helpful

@github-actions
Copy link

github-actions bot commented Mar 2, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

2 similar comments
@github-actions
Copy link

github-actions bot commented Mar 2, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@github-actions
Copy link

github-actions bot commented Mar 2, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@github-actions
Copy link

github-actions bot commented Mar 2, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

Copy link
Contributor

@AaronFriel AaronFriel left a comment

Choose a reason for hiding this comment

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

One last question, otherwise the changes LGTM

provider/image.go Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Mar 3, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

… running in buildkit mode and attempting to dockerignore
@github-actions
Copy link

github-actions bot commented Mar 7, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

Comment on lines 105 to 106
"Due to limitations when running this provider in Buildkit mode, your build files may get copied " +
"into your image. Please ensure any copied file systems do not include build files."
Copy link
Contributor

Choose a reason for hiding this comment

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

This only applies to .dockerignore and the Dockerfile. Perhaps we should tone this message down, that seems very low impact.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how is this?

// warn user about accidentally copying build files
if build.BuilderVersion == defaultBuilder {
for _, pattern := range ignorePatterns {
if pattern == "!Dockerfile" || pattern == "!.Dockerignore" {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if these patterns will fire - .dockerignore is typically lowercase.

Rather than try to check if the pattern matches, should we store the ignorePatterns on line 97 in a different variable from the return value of buildCmd.TrimBuildFilesFromExcludes and check len(before) != len(after)?

Copy link
Contributor

Choose a reason for hiding this comment

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

	ignorePatterns, err := getIgnore(dockerIgnorePath)
	// copy ignore patterns to compare
	initialIgnorePatterns := append([]string, ignorePatterns...)
	
	ignorePatterns = buildCmd.TrimBuildFilesFromExcludes(ignorePatterns, img.Build.Dockerfile, false)

	// warn user about accidentally copying build files, link to GitHub Issue: 
	if build.BuilderVersion == defaultBuilder && len(initialIgnorePatterns) != len(ignorePatterns) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oooh, I like it! good idea!

Copy link
Contributor

@jazzyfresh jazzyfresh left a comment

Choose a reason for hiding this comment

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

LGTM!

@github-actions
Copy link

github-actions bot commented Mar 7, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@github-actions
Copy link

github-actions bot commented Mar 7, 2023

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@guineveresaenger guineveresaenger merged commit 83e176a into master Mar 7, 2023
@guineveresaenger guineveresaenger deleted the guin/dockerignore branch March 7, 2023 20:05
@AaronFriel AaronFriel added this to the 0.85 milestone Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x.x kind/bug Some behavior is incorrect or out of spec
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants