Dockerfile COPY skipping over git submodules

Is there some way to have a Dockerfile's COPY command skip over Git submodules?

I know I can set up a .dockerignore, but I am wondering if it's possible to skip Git submodules without a .dockerignore.


  • You might consider for that a multi-stage build:

    # First Stage: Clone repository without submodules
    FROM alpine AS clone-stage
    RUN apk add --no-cache git \
        && git clone --recurse-submodules=no /temp-dir
    # Second Stage: Copy the cloned data
    FROM your-base-image
    COPY --from=clone-stage /temp-dir /your-destination-dir

    No submodules in the final image.