Skip to content

Commit 86cd302

Browse files
Merge pull request #961 from vdice/fix/dockerignore
fix(docker.go): read .dockerignore file for appropriate build exclusions
2 parents dae845a + 0c0b063 commit 86cd302

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/build/provider/docker.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
portercontext "get.porter.sh/porter/pkg/context"
1111
"get.porter.sh/porter/pkg/manifest"
1212
"github.com/docker/cli/cli/command"
13+
clibuild "github.com/docker/cli/cli/command/image/build"
1314
cliflags "github.com/docker/cli/cli/flags"
1415
"github.com/docker/docker/api/types"
1516
"github.com/docker/docker/pkg/archive"
@@ -43,7 +44,12 @@ func (b *DockerBuilder) BuildInvocationImage(manifest *manifest.Manifest) error
4344
"BUNDLE_DIR": &build.BUNDLE_DIR,
4445
},
4546
}
46-
tar, err := archive.TarWithOptions(path, &archive.TarOptions{})
47+
48+
excludes, err := clibuild.ReadDockerignore(path)
49+
if err != nil {
50+
return err
51+
}
52+
tar, err := archive.TarWithOptions(path, &archive.TarOptions{ExcludePatterns: excludes})
4753
if err != nil {
4854
return err
4955
}

tests/build_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// +build integration
2+
3+
package tests
4+
5+
import (
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
"get.porter.sh/porter/pkg/porter"
12+
)
13+
14+
func TestBuild_withDockerignore(t *testing.T) {
15+
p := porter.NewTestPorter(t)
16+
p.SetupIntegrationTest()
17+
defer p.CleanupIntegrationTest()
18+
p.Debug = false
19+
20+
p.TestConfig.TestContext.AddTestDirectory(filepath.Join(p.TestDir, "testdata/bundles/outputs-example"), ".")
21+
22+
// Create .dockerignore file which ignores the Dockerfile
23+
err := p.FileSystem.WriteFile(".dockerignore", []byte("Dockerfile"), 0644)
24+
require.NoError(t, err)
25+
26+
// Verify Porter uses the .dockerignore file
27+
opts := porter.BuildOptions{}
28+
err = p.Build(opts)
29+
require.EqualError(t, err, "unable to build CNAB invocation image: Error response from daemon: Cannot locate specified Dockerfile: Dockerfile")
30+
}

0 commit comments

Comments
 (0)