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

Dockerfile in .dockerignore is not respected, leading to unnecessary full rebuilds #9407

Open
ComaVN opened this issue May 2, 2024 · 2 comments

Comments

@ComaVN
Copy link

ComaVN commented May 2, 2024

Expected behavior

When I:

  • have Dockerfile in .dockerignore
  • use COPY . . in my Dockerfile
    I expect the Dockerfile to NOT be copied into the image. This is indeed the behaviour when doing docker build .

Actual behavior

skaffold build DOES copy the Dockerfile into the image, causing a lot of unnecessary build steps to be done every time anything (even just whitespace) changes in the Dockerfile

Information

FROM golang:1.18 as builder
WORKDIR /code
COPY . .
# `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
ARG SKAFFOLD_GO_GCFLAGS
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -trimpath -o /app main.go


FROM alpine:3
# Define GOTRACEBACK to mark this container as using the Go language runtime
# for `skaffold debug` (https://skaffold.dev/docs/workflows/debug/).
ENV GOTRACEBACK=single
CMD ["./app"]
COPY --from=builder /app .

Steps to reproduce the behavior

  1. https://github.com/GoogleContainerTools/skaffold/blob/v2.11.1/examples/getting-started
  2. replace both COPY commands in the Dockerfile with a single COPY . .
  3. add a .dockerignore file containing just the line Dockerfile
  4. skaffold build
  5. Change anything at all in the Dockerfile, eg. adding an empty line
  6. skaffold build now needs to recompile the binary rather than use the cached version

Note that docker build does NOT show this behaviour: it will correctly NOT copy the Dockerfile into the image, so it can reuse the cached image layers.

I am aware that COPY . . might not be the best practice, and changing the Dockerfile should be an infrequent occurence so this does not have any great impact. However, I do think it's worth to keep the behaviour of skaffold build as much the same as docker build . as possible.

@ComaVN
Copy link
Author

ComaVN commented May 2, 2024

Note that other files in .dockerignore are properly ignored.

@ComaVN
Copy link
Author

ComaVN commented May 2, 2024

I just noticed another difference between docker build . and skaffold build: it seems that skaffold does NOT copy the .dockerignore file, while docker build does! It's not even possible to do COPY .dockerignore . using skaffold (although I'm not sure why you'd ever want that).

$ docker build .
(...)
 > [builder 4/5] RUN ls -la ; false:                                                                                                                                                          
0.117 total 32                                                                                                                                                                                
0.117 drwxr-xr-x 1 root root 4096 May  2 12:08 .                                                                                                                                              
0.117 drwxr-xr-x 1 root root 4096 May  2 12:08 ..                                                                                                                                             
0.117 -rw-rw-r-- 1 root root   11 May  2 12:00 .dockerignore                                                                                                                                  
0.117 -rw-rw-r-- 1 root root  571 Mar 14 12:48 README.md
0.117 -rw-rw-r-- 1 root root   82 Mar 14 12:48 go.mod
0.117 -rw-rw-r-- 1 root root  133 Mar 14 12:48 k8s-pod.yaml
0.117 -rw-rw-r-- 1 root root  128 Mar 14 12:48 main.go
0.117 -rw-rw-r-- 1 root root  130 May  2 11:56 skaffold.yaml
$ skaffold build
(...)
Step 4/10 : RUN ls -la ; false
 ---> Running in 444c2c1e6f30
total 32
drwxr-xr-x 1 root root 4096 May  2 12:09 .
drwxr-xr-x 1 root root 4096 May  2 12:09 ..
-rw-rw-r-- 1 root root  470 May  2 12:05 Dockerfile
-rw-rw-r-- 1 root root  571 Mar 14 12:48 README.md
-rw-rw-r-- 1 root root   82 Mar 14 12:48 go.mod
-rw-rw-r-- 1 root root  133 Mar 14 12:48 k8s-pod.yaml
-rw-rw-r-- 1 root root  128 Mar 14 12:48 main.go
-rw-rw-r-- 1 root root  130 May  2 11:56 skaffold.yaml

Diff with getting-started example:

diff --git a/examples/getting-started/.dockerignore b/examples/getting-started/.dockerignore
new file mode 100644
index 000000000..94143827e
--- /dev/null
+++ b/examples/getting-started/.dockerignore
@@ -0,0 +1 @@
+Dockerfile
diff --git a/examples/getting-started/Dockerfile b/examples/getting-started/Dockerfile
index 67f8c3cbb..3798e28db 100644
--- a/examples/getting-started/Dockerfile
+++ b/examples/getting-started/Dockerfile
@@ -1,7 +1,7 @@
 FROM golang:1.18 as builder
 WORKDIR /code
-COPY main.go .
-COPY go.mod .
+COPY . .
+RUN ls -la ; false
 # `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
 ARG SKAFFOLD_GO_GCFLAGS
 RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -trimpath -o /app main.go

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

No branches or pull requests

1 participant