Skip to content

Commit

Permalink
Update to cosmopolitan 3.1.1, simplify bootstrap
Browse files Browse the repository at this point in the history
We now only use "uname" from busybox... Everything else is pulled from
cosmos binaries themselves.
  • Loading branch information
ajbouh committed Nov 29, 2023
1 parent 2e4c670 commit e741857
Showing 1 changed file with 91 additions and 27 deletions.
118 changes: 91 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,101 @@
# syntax=docker/dockerfile:1-labs

FROM busybox as unpack-cosmos
ADD --checksum=sha256:ce256ededf106748a09f13bf47ace9ef0e6f115d963353d3d63c21302c5f28f4 https://github.com/jart/cosmopolitan/releases/download/3.0.1/cosmos-3.0.1.zip /dl/
FROM busybox as busybox

# define a starting point "scratch" image that can run APEs
FROM scratch as cosmos-scratch
COPY --from=busybox /bin/uname /usr/bin/
ADD --chmod=0755 https://cosmo.zip/pub/cosmos/bin/assimilate-x86_64.elf /usr/bin/
ADD --chmod=0755 https://cosmo.zip/pub/cosmos/bin/dash /bin/sh
RUN ["/usr/bin/assimilate-x86_64.elf", "-c", "/bin/sh"]
ADD --checksum=sha256:abf3b1bb7182935bf48d98dc143c51ee563d29a1fd2c3930ff5a8d8c8d823817 --chmod=0755 https://justine.lol/ape.elf /usr/bin/ape
ENV PATH=/bin:/usr/bin

# download and unpack all the cosmos binaries
FROM cosmos-scratch as unpack-cosmos
ADD --chmod=0755 https://cosmo.zip/pub/cosmos/bin/unzip /usr/bin/
RUN ["/usr/bin/assimilate-x86_64.elf", "-c", "/usr/bin/unzip"]
ADD https://cosmo.zip/pub/cosmos/zip/cosmos.zip /dl/

# list of binaries that must be assimilated and manifest for /bin as described in https://justine.lol/cosmos.txt (as of 2023-11-29)
WORKDIR /opt/cosmos
RUN ["/bin/unzip", "/dl/cosmos-3.0.1.zip"]
RUN ["/bin/cp", "bin/dash", "bin/sh"]
RUN unzip /dl/cosmos.zip
WORKDIR /opt/cosmos/bin
RUN /usr/bin/assimilate-x86_64.elf -c dd \
&& /usr/bin/assimilate-x86_64.elf -c cp \
&& /usr/bin/assimilate-x86_64.elf -c mv \
&& /usr/bin/assimilate-x86_64.elf -c echo \
&& /usr/bin/assimilate-x86_64.elf -c uname \
&& /usr/bin/assimilate-x86_64.elf -c mkdir \
&& /usr/bin/assimilate-x86_64.elf -c chmod \
&& /usr/bin/assimilate-x86_64.elf -c gzip \
&& /usr/bin/assimilate-x86_64.elf -c printf \
# assimilate these so the build works on github actions...
&& /usr/bin/assimilate-x86_64.elf -c rmdir \
&& /usr/bin/assimilate-x86_64.elf -c ln \
&& /usr/bin/assimilate-x86_64.elf -c tar \
&& /usr/bin/assimilate-x86_64.elf -c unzip
RUN ./mv '[' bash cat chgrp chmod chown cksum cp date df echo false grep kill ln ls mkdir mknod mktemp mv nice printenv pwd rm rmdir sed sleep stat sync touch true uname /bin/ \
&& /bin/mv * /usr/bin/
WORKDIR /
RUN rmdir /opt/cosmos/bin /opt/cosmos

FROM busybox as unpack-cosmo
ADD --checksum=sha256:2872d2f06ef5fd13a206d3ba7a9ef29eb9bd8ebfe9600a35d5c55a88ffd112df https://github.com/jart/cosmopolitan/releases/download/3.0.1/cosmopolitan-3.0.1.tar.gz /dl/
ADD --checksum=sha256:e222b38b53b999e3310a2e172a75992a28b1594af5c1e954c913fc54405c1135 https://github.com/jart/cosmopolitan/releases/download/3.0.1/cosmocc-0.0.16.zip /dl/
WORKDIR /opt/cosmo
RUN ["/bin/tar", "-xf", "/dl/cosmopolitan-3.0.1.tar.gz", "--strip-components=1"]
RUN ["/bin/unzip", "/dl/cosmocc-0.0.16.zip"]
WORKDIR /usr/bin
RUN ["/bin/ln", "-s", "/opt/cosmo/bin/cosmocc", "cc"]
RUN ["/bin/ln", "-s", "/opt/cosmo/bin/cosmocc", "gcc"]
ENTRYPOINT ["/bin/sh", "-c", "exec \"$@\"", "sh"]
CMD ["/bin/bash"]

FROM busybox as busybox
# download and unpack the cosmo source code
FROM cosmos-scratch as unpack-cosmo
ADD --checksum=sha256:8d1058afcd6f32f5e7edb708c0a3014d544a4b17d35449be71fbfdd2a1eb39ba https://github.com/jart/cosmopolitan/releases/download/3.1.1/cosmopolitan-3.1.1.tar.gz /dl/
WORKDIR /opt/cosmo
COPY --from=unpack-cosmos /usr/bin/tar /usr/bin/gzip /usr/bin/
RUN /usr/bin/tar --strip-components=1 -xf /dl/cosmopolitan-3.1.1.tar.gz

# Create the final image from scratch
FROM scratch
# We need sh and uname for cosmos at runtime.
COPY --from=busybox /bin/uname /bin/
ADD --checksum=sha256:e67d07bb3010cad678c02fbc787c360340060467ebb39d681b58389df40fc904 --chmod=0755 https://justine.lol/dash /bin/sh
ADD --checksum=sha256:2789991dd41483961a753040ffc083c0c5ff24b84c09a02892e5c584a3f8effa --chmod=0755 https://justine.lol/ape.elf /usr/bin/ape
# download and unpack the cosmocc toolchain
FROM cosmos-scratch as unpack-cosmocc
ADD --checksum=sha256:fa982741f52a2199194b9f06229729eb1eb220d065b0a65cca6dec3b36a9c7df https://github.com/jart/cosmopolitan/releases/download/3.1.1/cosmocc-3.1.1.zip /dl/
WORKDIR /opt/cosmocc
COPY --from=unpack-cosmos /usr/bin/unzip /usr/bin/
RUN /usr/bin/unzip /dl/cosmocc-3.1.1.zip

# define the final image in as few layers as possible
FROM cosmos-scratch as cosmos
LABEL org.opencontainers.image.source https://github.com/ajbouh/cosmos
COPY --from=unpack-cosmos /bin /bin
COPY --from=unpack-cosmos /usr/bin /usr/bin
COPY --from=unpack-cosmo /opt/cosmo /opt/cosmo
COPY --from=unpack-cosmo /usr/bin/cc /usr/bin/gcc /usr/bin/
COPY --from=unpack-cosmos /opt/cosmos /opt/cosmos
COPY --from=unpack-cosmocc /opt/cosmocc /opt/cosmocc
ENV PATH=/bin:/usr/bin:/opt/cosmocc/bin
RUN /bin/ln -s /opt/cosmocc/bin/cosmocc /usr/bin/cc \
&& /bin/ln -s /opt/cosmocc/bin/cosmocc /usr/bin/gcc

ENV PATH=/bin:/usr/bin:/opt/cosmo/bin:/opt/cosmos/bin
# RUN ["/opt/cosmo/bin/cosmocc", "--update"]
ENTRYPOINT ["/bin/sh", "-c", "exec \"$@\"", "sh"]
CMD ["/bin/bash"]

ENTRYPOINT ["/bin/sh", "-c"]
CMD ["/opt/cosmos/bin/bash"]
LABEL org.opencontainers.image.source https://github.com/ajbouh/cosmos
# FROM cosmos-scratch as solo
# # can be /usr/bin/python /usr/bin/qjs /usr/bin/sqlite3 /usr/bin/lua
# ARG COSMOS_EXE=/usr/bin/python
# LABEL org.opencontainers.image.source https://github.com/ajbouh/cosmos
# COPY --from=unpack-cosmos ${COSMOS_EXE} ${COSMOS_EXE}
# ENV PATH=/bin:/usr/bin
# ENV COSMOS_EXE="${COSMOS_EXE}"
# ENTRYPOINT ["/bin/sh", "-c", "exec \"$@\"", "sh"]
# CMD "${COSMOS_EXE}"

# FROM cosmos-scratch as python-pip-cli
# ARG COSMOS_EXE=/usr/bin/python
# ARG COSMOS_PIP_INSTALL="huggingface_hub[cli]"
# ARG COSMOS_PIP_BINSTUB="huggingface-cli"
# LABEL org.opencontainers.image.source https://github.com/ajbouh/cosmos
# COPY --from=unpack-cosmos ${COSMOS_EXE} ${COSMOS_EXE}
# ENV PATH=/bin:/usr/bin
# ENV COSMOS_EXE="${COSMOS_EXE}"
# ENV COSMOS_PIP_BINSTUB="${COSMOS_PIP_BINSTUB}"
# RUN python -m pip install --no-cache-dir ${COSMOS_PIP_INSTALL}
# ENTRYPOINT ["/bin/sh", "-c", "exec \"$@\"", "sh"]
# CMD "${COSMOS_EXE}" "/ahgamut/testing/superconfigure/cosmos/x86_64/bin/${COSMOS_PIP_BINSTUB}"

# FROM cosmos-scratch as mistral-7b-instruct-v0.1-Q4_K_M-main
# LABEL org.opencontainers.image.source https://github.com/ajbouh/cosmos
# COPY --chmod=0755 mistral-7b-instruct-v0.1-Q4_K_M-main.llamafile /usr/bin/mistral-7b-instruct-v0.1-Q4_K_M-main.llamafile
# ENV PATH=/bin:/usr/bin
# ENTRYPOINT ["/bin/sh", "-c", "exec \"$@\"", "sh", "/usr/bin/mistral-7b-instruct-v0.1-Q4_K_M-main.llamafile"]

0 comments on commit e741857

Please sign in to comment.