|
1 |
| -FROM rust:slim |
| 1 | +# To produce a smaller image this Dockerfile contains two separate stages: in |
| 2 | +# the first one all the build dependencies are installed and docs.rs is built, |
| 3 | +# while in the second one just the runtime dependencies are installed, with the |
| 4 | +# binary built in the previous stage copied there. |
| 5 | +# |
| 6 | +# As of 2019-10-29 this reduces the image from 2.8GB to 500 MB. |
2 | 7 |
|
3 |
| -### STEP 1: Install dependencies ### |
4 |
| -# Install packaged dependencies |
5 |
| -RUN apt-get update && apt-get install -y --no-install-recommends \ |
6 |
| - build-essential git curl cmake gcc g++ pkg-config libmagic-dev \ |
7 |
| - libssl-dev zlib1g-dev sudo docker.io |
| 8 | +################# |
| 9 | +# Build stage # |
| 10 | +################# |
8 | 11 |
|
9 |
| -### STEP 2: Create user ### |
10 |
| -ENV HOME=/home/cratesfyi |
11 |
| -RUN adduser --home $HOME --disabled-login --disabled-password --gecos "" cratesfyi |
| 12 | +FROM ubuntu:bionic AS build |
12 | 13 |
|
13 |
| -### STEP 3: Setup build environment as new user ### |
14 |
| -ENV CRATESFYI_PREFIX=/home/cratesfyi/prefix |
15 |
| -RUN mkdir $CRATESFYI_PREFIX && chown cratesfyi:cratesfyi "$CRATESFYI_PREFIX" |
| 14 | +# Install packaged dependencies |
| 15 | +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 16 | + build-essential git curl cmake gcc g++ pkg-config libmagic-dev \ |
| 17 | + libssl-dev zlib1g-dev ca-certificates |
16 | 18 |
|
17 |
| -USER cratesfyi |
18 |
| -RUN mkdir -vp "$CRATESFYI_PREFIX"/documentations "$CRATESFYI_PREFIX"/public_html "$CRATESFYI_PREFIX"/sources |
19 |
| -RUN git clone https://github.com/rust-lang/crates.io-index.git "$CRATESFYI_PREFIX"/crates.io-index |
20 |
| -RUN git --git-dir="$CRATESFYI_PREFIX"/crates.io-index/.git branch crates-index-diff_last-seen |
| 19 | +# Install the stable toolchain with rustup |
| 20 | +RUN curl https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init >/tmp/rustup-init && \ |
| 21 | + chmod +x /tmp/rustup-init && \ |
| 22 | + /tmp/rustup-init -y --no-modify-path --default-toolchain stable --profile minimal |
| 23 | +ENV PATH=/root/.cargo/bin:$PATH |
21 | 24 |
|
22 |
| -### STEP 4: Build the project ### |
23 | 25 | # Build the dependencies in a separate step to avoid rebuilding all of them
|
24 | 26 | # every time the source code changes. This takes advantage of Docker's layer
|
25 | 27 | # caching, and it works by copying the Cargo.{toml,lock} with dummy source code
|
26 | 28 | # and doing a full build with it.
|
27 |
| -RUN mkdir -p ~/docs.rs ~/docs.rs/src/web/badge |
28 |
| -WORKDIR $HOME/docs.rs |
29 |
| -COPY --chown=cratesfyi Cargo.lock Cargo.toml ./ |
30 |
| -COPY --chown=cratesfyi src/web/badge src/web/badge/ |
| 29 | +RUN mkdir -p /build/src/web/badge |
| 30 | +WORKDIR /build |
| 31 | +COPY Cargo.lock Cargo.toml ./ |
| 32 | +COPY src/web/badge src/web/badge/ |
31 | 33 | RUN echo "fn main() {}" > src/main.rs && \
|
32 | 34 | echo "fn main() {}" > build.rs
|
33 | 35 |
|
34 | 36 | RUN cargo fetch
|
35 | 37 | RUN cargo build --release
|
36 | 38 |
|
37 |
| -### STEP 5: Build the website ### |
38 | 39 | # Dependencies are now cached, copy the actual source code and do another full
|
39 | 40 | # build. The touch on all the .rs files is needed, otherwise cargo assumes the
|
40 | 41 | # source code didn't change thanks to mtime weirdness.
|
41 | 42 | RUN rm -rf src build.rs
|
42 | 43 |
|
43 |
| -COPY --chown=cratesfyi build.rs build.rs |
| 44 | +COPY .git .git |
| 45 | +COPY build.rs build.rs |
44 | 46 | RUN touch build.rs
|
45 |
| -COPY --chown=cratesfyi src src/ |
| 47 | +COPY src src/ |
46 | 48 | RUN find src -name "*.rs" -exec touch {} \;
|
47 |
| -COPY --chown=cratesfyi templates/style.scss templates/ |
| 49 | +COPY templates/style.scss templates/ |
48 | 50 |
|
49 | 51 | RUN cargo build --release
|
50 | 52 |
|
51 |
| -ADD templates templates/ |
52 |
| -ADD css $CRATESFYI_PREFIX/public_html |
| 53 | +################## |
| 54 | +# Output stage # |
| 55 | +################## |
| 56 | + |
| 57 | +FROM ubuntu:bionic AS output |
| 58 | + |
| 59 | +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ |
| 60 | + git \ |
| 61 | + libmagic1 \ |
| 62 | + docker.io \ |
| 63 | + ca-certificates |
| 64 | + |
| 65 | +RUN mkdir -p /opt/docsrs/prefix |
| 66 | + |
| 67 | +COPY --from=build /build/target/release/cratesfyi /usr/local/bin |
| 68 | +COPY css /opt/docsrs/prefix/public_html |
| 69 | +COPY templates /opt/docsrs/templates |
| 70 | +COPY docker-entrypoint.sh /opt/docsrs/entrypoint.sh |
53 | 71 |
|
54 |
| -ENV DOCS_RS_DOCKER=true |
55 |
| -COPY docker-entrypoint.sh ./ |
56 |
| -USER root |
57 |
| -ENTRYPOINT ["./docker-entrypoint.sh"] |
| 72 | +WORKDIR /opt/docsrs |
| 73 | +ENTRYPOINT ["/opt/docsrs/entrypoint.sh"] |
58 | 74 | CMD ["daemon", "--foreground"]
|
0 commit comments