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

Scaffolded .dockerignore does not do what it claims #2073

Closed
busser opened this issue Mar 9, 2021 · 4 comments · Fixed by #2081
Closed

Scaffolded .dockerignore does not do what it claims #2073

busser opened this issue Mar 9, 2021 · 4 comments · Fixed by #2081
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Milestone

Comments

@busser
Copy link
Contributor

busser commented Mar 9, 2021

When running make docker-build for an operator scaffolded with kubebuilder v3, the entire directory is included in the Docker build context.

I expected only Go source files to be included in the Docker build context. Here is the scaffolded .dockerignore file:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
!**/*.go
!**/*.mod
!**/*.sum

To reproduce, build a dummy Docker image containing the entire build context:

docker image build -t build-context -f - . <<EOF
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .
EOF

Now run it:

docker container run --rm build-context

Or explore the images contents:

docker container run --rm -it build-context /bin/sh

I've traced the issue to this source code:

const dockerignorefileTemplate = `# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
!**/*.go
!**/*.mod
!**/*.sum
`

For reference, this is my Docker version:

Client: Docker Engine - Community
 Cloud integration: 1.0.7
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        2291f61
 Built:             Mon Dec 28 16:12:42 2020
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8891c58
  Built:            Mon Dec 28 16:15:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

/kind bug

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Mar 9, 2021
@busser
Copy link
Contributor Author

busser commented Mar 9, 2021

It seems to me that the .dockerignore file has unexpected behavior. It does not handle a directory wildcard ** properly when it is right after an exclamation !.

For instance, If I replace the .dockerignore's contents with these:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
*
!**/*.go

The contents of the controllers/ directory are not included in the Docker build context. However, with this .dockerignore file:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
*
!controllers/**/*.go

The Go files in the controllers/ are properly included.

@busser
Copy link
Contributor Author

busser commented Mar 9, 2021

I figure that the easiest way to work around this issue is to change the scaffolded .dockerignore file to ignore all files except those referenced in the scaffolded Dockerfile. The .dockerignore file would thus contain:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore all files which are not go type
*
!go.mod
!go.sum
!main.go
!controllers/**/*.go
!api/**/*.go

If that's OK with you, I can open a PR to make the change.

@camilamacedo86
Copy link
Member

It is great. Please, feel free to collab with the solution.

@camilamacedo86 camilamacedo86 added the triage/accepted Indicates an issue or PR is ready to be actively worked on. label Mar 9, 2021
@camilamacedo86 camilamacedo86 added this to the 3.2.0 milestone Mar 9, 2021
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants