Skip to content

Instantly share code, notes, and snippets.

@AndBondStyle
Last active November 5, 2019 18:57
Show Gist options
  • Save AndBondStyle/97c53d1e87b614a9137f98baffc5a357 to your computer and use it in GitHub Desktop.
Save AndBondStyle/97c53d1e87b614a9137f98baffc5a357 to your computer and use it in GitHub Desktop.
Docker build from local git repository
# Bootstrap image
FROM alpine:latest as bootstrap
LABEL stage=bootstrap
WORKDIR /tmp
COPY . .
RUN apk update && apk add --update git
RUN git archive HEAD -v -o files.tar
RUN mkdir files && tar xf files.tar -C files
# Final image
FROM <insert-your-favourite-image>
WORKDIR /app
COPY --from=bootstrap /tmp/. .
# ...
@AndBondStyle
Copy link
Author

What's happening (line by line):

  • [3-5] Create temporary (intermediate) image, which will be deleted at the end of the build
  • [7] Copy everything (including local git repository) from host file system.
    This "everything" may contain secret credentials, trash files, etc. – this is perfectly OK until they're not in your git index
  • [8] Install git on intermediate image OS (alpine)
  • [9] Create an archive of only git-indexed files for latest commit at current branch
  • [10] ...and extract that archive to /tmp/files
  • [14-15] Prepare final image
  • [17] Copy previously extracted files from temp to final image
  • [18+] Write anything that suits your application

Inspiration: https://janakerman.co.uk/docker-git-clone/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment