Last active
November 5, 2019 18:57
-
-
Save AndBondStyle/97c53d1e87b614a9137f98baffc5a357 to your computer and use it in GitHub Desktop.
Docker build from local git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/. . | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 applicationInspiration: https://janakerman.co.uk/docker-git-clone/