Skip to content

Commit

Permalink
Correctly ignore files from .dockerignore (#508)
Browse files Browse the repository at this point in the history
* Exclude files from dockerignore when uploading context tarball

* clean up ignore patterns

* Refactor ignorePatterns into separate function. Fix dockerignore path bug in hash function.

* dockerignore details

* unskipping dotnet test

* Do not ignore Dockerfile

* Improve readability for dockerfile exists condition

* address review feedback

* undo excluding Dockerfiles

* Do not dockerignore  and

* Add . to includeFiles

* Add warning for build files potentially being copied to an image when running in buildkit mode and attempting to dockerignore

* use length of ignorefiles to send warning to user

* tone down message
  • Loading branch information
guineveresaenger authored Mar 7, 2023
1 parent e6c4038 commit 83e176a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
1 change: 0 additions & 1 deletion examples/examples_dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestNginxCs(t *testing.T) {
}

func TestDotNet(t *testing.T) {

test := getCsharpBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "dotnet"),
Expand Down
1 change: 0 additions & 1 deletion examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestNginx(t *testing.T) {
}

func TestDockerfileWithMultipleTargets(t *testing.T) {

test := getJsOptions(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "dockerfile-with-targets"),
Expand Down
36 changes: 32 additions & 4 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net"

"github.com/docker/distribution/reference"
"github.com/moby/buildkit/session"
"github.com/moby/moby/registry"
"net"
"path/filepath"

"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
Expand All @@ -23,6 +23,8 @@ import (
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"

buildCmd "github.com/docker/cli/cli/command/image/build"
)

const defaultDockerfile = "Dockerfile"
Expand Down Expand Up @@ -90,8 +92,34 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context,
return "", nil, err
}

// make the build context
tar, err := archive.TarWithOptions(img.Build.Context, &archive.TarOptions{})
// make the build context and ensure to exclude dockerignore file patterns
dockerIgnorePath := filepath.Join(build.Context, ".dockerignore")
initialIgnorePatterns, err := getIgnore(dockerIgnorePath)
// un-ignore build files so the docker daemon can use them
ignorePatterns := buildCmd.TrimBuildFilesFromExcludes(
initialIgnorePatterns,
img.Build.Dockerfile,
false,
)

// warn user about accidentally copying build files
if build.BuilderVersion == defaultBuilder && len(initialIgnorePatterns) != len(ignorePatterns) {
msg := "It looks like you are trying to dockerignore a build file such as `Dockerfile` or `.dockerignore`. " +
"To avoid accidentally copying these files to your image, please ensure any copied file systems do not " +
"include `Dockerfile` or `.dockerignore`."
err = p.host.Log(ctx, "warning", urn, msg)
if err != nil {
return "", nil, err
}
}

if err != nil {
return "", nil, err
}

tar, err := archive.TarWithOptions(img.Build.Context, &archive.TarOptions{
ExcludePatterns: ignorePatterns,
})
if err != nil {
return "", nil, err
}
Expand Down
35 changes: 20 additions & 15 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,22 +483,10 @@ func (accumulator *contextHashAccumulator) hexSumContext() string {

func hashContext(dockerContextPath string, dockerfile string) (string, error) {
// exclude all files listed in dockerignore
dockerIgnorePath := dockerfile + ".dockerignore"
dockerIgnore, err := os.ReadFile(dockerIgnorePath)
if err != nil {
if os.IsNotExist(err) {
dockerIgnorePath = filepath.Join(dockerContextPath, ".dockerignore")
dockerIgnore, err = os.ReadFile(dockerIgnorePath)
if err != nil && !os.IsNotExist(err) {
return "", fmt.Errorf("unable to read %s file: %w", dockerIgnorePath, err)
}
} else {
return "", fmt.Errorf("unable to read %s file: %w", dockerIgnorePath, err)
}
}
ignorePatterns, err := dockerignore.ReadAll(bytes.NewReader(dockerIgnore))
dockerIgnorePath := filepath.Join(dockerContextPath, ".dockerignore")
ignorePatterns, err := getIgnore(dockerIgnorePath)
if err != nil {
return "", fmt.Errorf("unable to parse %s file: %w", dockerIgnorePath, err)
return "", err
}
ignoreMatcher, err := fileutils.NewPatternMatcher(ignorePatterns)
if err != nil {
Expand Down Expand Up @@ -554,3 +542,20 @@ func hashContext(dockerContextPath string, dockerfile string) (string, error) {
// create a hash of the entire input of the hash accumulator
return accumulator.hexSumContext(), nil
}

func getIgnore(dockerIgnorePath string) ([]string, error) {
var ignorePatterns []string
dockerIgnore, err := os.ReadFile(dockerIgnorePath)
if err != nil {
if os.IsNotExist(err) {
// .dockerignore files are optional
return ignorePatterns, nil
}
return ignorePatterns, fmt.Errorf("unable to read %s file: %w", dockerIgnorePath, err)
}
ignorePatterns, err = dockerignore.ReadAll(bytes.NewReader(dockerIgnore))
if err != nil {
return ignorePatterns, fmt.Errorf("unable to parse %s file: %w", ".dockerignore", err)
}
return ignorePatterns, nil
}

0 comments on commit 83e176a

Please sign in to comment.