Skip to content

Improve Dockerfile #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.rustwide
/ignored
**/target
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CRATESFYI_GITHUB_USERNAME=
CRATESFYI_GITHUB_ACCESSTOKEN=
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ jobs:

- name: Test docs.rs
run: cargo test -- --test-threads=1

docker:
name: Docker
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@master
with:
fetch-depth: 2

- name: Build the Docker image
run: docker build -t docsrs .

- name: Upload the Docker image to ECR
uses: rust-lang/simpleinfra/github-actions/upload-docker-image@master
with:
image: docsrs
repository: docsrs
region: us-west-1
aws_access_key_id: "${{ secrets.aws_access_key_id }}"
aws_secret_access_key: "${{ secrets.aws_secret_access_key }}"
if: github.ref == 'refs/heads/master'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/ignored
/.env
target
*.css
*.css.map
Expand Down
78 changes: 47 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,58 +1,74 @@
FROM rust:slim
# To produce a smaller image this Dockerfile contains two separate stages: in
# the first one all the build dependencies are installed and docs.rs is built,
# while in the second one just the runtime dependencies are installed, with the
# binary built in the previous stage copied there.
#
# As of 2019-10-29 this reduces the image from 2.8GB to 500 MB.

### STEP 1: Install dependencies ###
# Install packaged dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential git curl cmake gcc g++ pkg-config libmagic-dev \
libssl-dev zlib1g-dev sudo docker.io
#################
# Build stage #
#################

### STEP 2: Create user ###
ENV HOME=/home/cratesfyi
RUN adduser --home $HOME --disabled-login --disabled-password --gecos "" cratesfyi
FROM ubuntu:bionic AS build

### STEP 3: Setup build environment as new user ###
ENV CRATESFYI_PREFIX=/home/cratesfyi/prefix
RUN mkdir $CRATESFYI_PREFIX && chown cratesfyi:cratesfyi "$CRATESFYI_PREFIX"
# Install packaged dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
build-essential git curl cmake gcc g++ pkg-config libmagic-dev \
libssl-dev zlib1g-dev ca-certificates

USER cratesfyi
RUN mkdir -vp "$CRATESFYI_PREFIX"/documentations "$CRATESFYI_PREFIX"/public_html "$CRATESFYI_PREFIX"/sources
RUN git clone https://github.com/rust-lang/crates.io-index.git "$CRATESFYI_PREFIX"/crates.io-index
RUN git --git-dir="$CRATESFYI_PREFIX"/crates.io-index/.git branch crates-index-diff_last-seen
# Install the stable toolchain with rustup
RUN curl https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init >/tmp/rustup-init && \
chmod +x /tmp/rustup-init && \
/tmp/rustup-init -y --no-modify-path --default-toolchain stable --profile minimal
ENV PATH=/root/.cargo/bin:$PATH

### STEP 4: Build the project ###
# Build the dependencies in a separate step to avoid rebuilding all of them
# every time the source code changes. This takes advantage of Docker's layer
# caching, and it works by copying the Cargo.{toml,lock} with dummy source code
# and doing a full build with it.
RUN mkdir -p ~/docs.rs ~/docs.rs/src/web/badge
WORKDIR $HOME/docs.rs
COPY --chown=cratesfyi Cargo.lock Cargo.toml ./
COPY --chown=cratesfyi src/web/badge src/web/badge/
RUN mkdir -p /build/src/web/badge
WORKDIR /build
COPY Cargo.lock Cargo.toml ./
COPY src/web/badge src/web/badge/
RUN echo "fn main() {}" > src/main.rs && \
echo "fn main() {}" > build.rs

RUN cargo fetch
RUN cargo build --release

### STEP 5: Build the website ###
# Dependencies are now cached, copy the actual source code and do another full
# build. The touch on all the .rs files is needed, otherwise cargo assumes the
# source code didn't change thanks to mtime weirdness.
RUN rm -rf src build.rs

COPY --chown=cratesfyi build.rs build.rs
COPY .git .git
COPY build.rs build.rs
RUN touch build.rs
COPY --chown=cratesfyi src src/
COPY src src/
RUN find src -name "*.rs" -exec touch {} \;
COPY --chown=cratesfyi templates/style.scss templates/
COPY templates/style.scss templates/

RUN cargo build --release

ADD templates templates/
ADD css $CRATESFYI_PREFIX/public_html
##################
# Output stage #
##################

FROM ubuntu:bionic AS output

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
git \
libmagic1 \
docker.io \
ca-certificates

RUN mkdir -p /opt/docsrs/prefix

COPY --from=build /build/target/release/cratesfyi /usr/local/bin
COPY css /opt/docsrs/prefix/public_html
COPY templates /opt/docsrs/templates
COPY docker-entrypoint.sh /opt/docsrs/entrypoint.sh

ENV DOCS_RS_DOCKER=true
COPY docker-entrypoint.sh ./
USER root
ENTRYPOINT ["./docker-entrypoint.sh"]
WORKDIR /opt/docsrs
ENTRYPOINT ["/opt/docsrs/entrypoint.sh"]
CMD ["daemon", "--foreground"]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ Make sure you have docker-compose and are able to download ~10GB data on the fir
```sh
git clone https://github.com/rust-lang/docs.rs.git docs.rs
cd docs.rs
cp .env.sample .env
docker-compose up # This may take a half hour or more on the first run
```

If you need to store big files in the repository's directory it's recommended to
put them in the `ignored/` subdirectory, which is ignored both by git and
Docker.

### CLI

#### Starting web server
Expand Down
128 changes: 128 additions & 0 deletions css/main-20160526-1.10.0-nightly-97e3a2401.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright 2015 The Rust Project Developers. See the COPYRIGHT
* file at the top-level directory of this distribution and at
* http://rust-lang.org/COPYRIGHT.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/

/* General structure and fonts */

body {
background-color: white;
color: black;
}

h1, h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
color: black;
}
h1.fqn {
border-bottom-color: #D5D5D5;
}
h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
border-bottom-color: #DDDDDD;
}
.in-band, code {
background-color: white;
}

.docblock code {
background-color: #F5F5F5;
}
pre {
background-color: #F5F5F5;
}

.sidebar .location {
background: #e1e1e1;
color: #333;
}

.block a:hover {
background: #F5F5F5;
}

.line-numbers span { color: #c67e2d; }
.line-numbers .line-highlighted {
background-color: #f6fdb0 !important;
}

:target { background: #FDFFD3; }
.content .highlighted {
color: #000 !important;
background-color: #ccc;
}
.content .highlighted a, .content .highlighted span { color: #000 !important; }
.content .highlighted.trait { background-color: #fece7e; }
.content .highlighted.mod { background-color: #afc6e4; }
.content .highlighted.enum { background-color: #b4d1b9; }
.content .highlighted.struct { background-color: #e7b1a0; }
.content .highlighted.fn { background-color: #c6afb3; }
.content .highlighted.method { background-color: #c6afb3; }
.content .highlighted.tymethod { background-color: #c6afb3; }
.content .highlighted.type { background-color: #c6afb3; }

.docblock h1, .docblock h2, .docblock h3, .docblock h4, .docblock h5 {
border-bottom-color: #DDD;
}

.docblock table {
border-color: #ddd;
}

.docblock table td {
border-top-color: #ddd;
border-bottom-color: #ddd;
}

.docblock table th {
border-top-color: #ddd;
border-bottom-color: #ddd;
}

.content a.primitive { color: #39a7bf; }
.content span.externcrate, span.mod, .content a.mod, block a.current.mod { color: #4d76ae; }
.content span.fn, .content a.fn, .block a.current.fn,
.content span.method, .content a.method, .block a.current.method,
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
.content .fnname { color: #8c6067; }

pre.rust .comment { color: #8E908C; }
pre.rust .doccomment { color: #4D4D4C; }

nav {
border-bottom-color: #e0e0e0;
}
nav.main .current {
border-top-color: #000;
border-bottom-color: #000;
}
nav.main .separator {
border-color: 1px solid #000;
}
a {
color: #000;
}

.docblock a, .stability a {
color: #3873AD;
}

a.test-arrow {
color: #f5f5f5;
}

.content span.trait, .content a.trait, .block a.current.trait { color: #7c5af3; }

.search-input {
color: #555;
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent;
background-color: white;
}

em.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
em.stab.deprecated { background: #F3DFFF; border-color: #7F0087; }
Loading