Skip to content

Instantly share code, notes, and snippets.

@carlosonunez
Last active February 4, 2024 15:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosonunez/b6af15062661bf9dfcb86880a94fd629 to your computer and use it in GitHub Desktop.
Save carlosonunez/b6af15062661bf9dfcb86880a94fd629 to your computer and use it in GitHub Desktop.
ugit, but even smaller!
FROM alpine:3.18 as base
ENV ESSENTIAL="tar sh"
ENV DEPS="ugit git fzf tput nl cut bash sed awk tr env xargs"
RUN apk add --no-cache \
bash \
coreutils \
git \
ncurses \
ncurses-terminfo \
curl \
tar
# Download fzf binary from GitHub, pin to 0.46.0, ugit requires minimum 0.21.0
RUN curl -L -o fzf.tar.gz https://github.com/junegunn/fzf/releases/download/0.46.0/fzf-0.46.0-linux_amd64.tar.gz && \
tar -xzf fzf.tar.gz && \
mv fzf /usr/bin/
# Copy only the ugit script into the container at /app
COPY ugit .
# Set permissions and move the script to path
RUN chmod +x ugit && mv ugit /usr/bin/
# ldd all of ugit's dependencies and gzip them with tar
RUN mkdir /app; files=$(for file in $(which $DEPS $ESSENTIAL /bin/ls); do echo "$file" && ldd "$file"; done | \
sed -E 's/ \(.*$//; s/^.* => //' | \
tr -d ' \t' | \
sort -u | tr '\n' ' '); \
files="$files /usr/share/terminfo/x/xterm"; \
tar -Phcvzf /app/app.tar.gz $files
# Use an intermediate stage to create the filesystem
FROM scratch AS app
COPY --from=base /app/app.tar.gz /app.tar.gz
COPY --from=base /lib/ld-musl-aarch64.so.1 /lib/ld-musl-aarch64.so.1
COPY --from=base /lib/libacl.so.1 /lib/libacl.so.1
COPY --from=base /bin/sh /bin/sh
COPY --from=base /usr/bin/tar /usr/bin/tar
COPY --from=base /bin/gzip /bin/gzip
# Finally, flatten everything into a single layer to improve
# download and decompression performance and untar on container start.
# Use $@ to accept arguments from `docker container create`
# Startup latency is minimal since the archive isn't very large
FROM scratch
COPY --from=app / /
ENV TERM=xterm
ENTRYPOINT [ "/bin/sh", \
"-c", \
"tar -Pxzf /app.tar.gz && /usr/bin/ugit $@" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment