I want to do a docker multi-stage build but rm/ignore the .git folder, to save space on the docker image.
FROM ubuntu as first
WORKDIR /app
RUN git clone <repo>
FROM golang as second
WORKDIR app
COPY --from=first /app .
is there is some --exclude option for COPY? Here is a related issue: https://forums.docker.com/t/dockerignore-in-multi-stage-builds/57169
another possibility is to remove the .git folder manually:
FROM ubuntu as first
WORKDIR /app
RUN git clone <repo>
RUN rm -rf .git
I assume the multi-stage build copies the "final layer" from the other stage?
One of the ways to exclude files from the build is to use a .dockerignore file. However, this is probably not what you need as you're running a git clone during the image preparation, so you will actually need the .git
folder.
If you'd like to use a multistage build then what you will need to copy are the artifacts, not the layers, of the previous build to the next one.
Another idea is to run a shallow clone - git clone --depth=1
- this should significantly reduce the size of the repository.