Skip to content

Commit d38fc5c

Browse files
authored
Merge pull request #455 from rust-lang/improve-dockerfile
Improve Dockerfile
2 parents d29b3b8 + dda2eb3 commit d38fc5c

10 files changed

+962
-44
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.rustwide
2+
/ignored
23
**/target

.env.sample

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CRATESFYI_GITHUB_USERNAME=
2+
CRATESFYI_GITHUB_ACCESSTOKEN=

.github/workflows/ci.yml

+22
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ jobs:
2121

2222
- name: Test docs.rs
2323
run: cargo test -- --test-threads=1
24+
25+
docker:
26+
name: Docker
27+
runs-on: ubuntu-latest
28+
steps:
29+
30+
- uses: actions/checkout@master
31+
with:
32+
fetch-depth: 2
33+
34+
- name: Build the Docker image
35+
run: docker build -t docsrs .
36+
37+
- name: Upload the Docker image to ECR
38+
uses: rust-lang/simpleinfra/github-actions/upload-docker-image@master
39+
with:
40+
image: docsrs
41+
repository: docsrs
42+
region: us-west-1
43+
aws_access_key_id: "${{ secrets.aws_access_key_id }}"
44+
aws_secret_access_key: "${{ secrets.aws_secret_access_key }}"
45+
if: github.ref == 'refs/heads/master'

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/ignored
2+
/.env
13
target
24
*.css
35
*.css.map

Dockerfile

+47-31
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,74 @@
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.
27

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+
#################
811

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
1213

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
1618

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
2124

22-
### STEP 4: Build the project ###
2325
# Build the dependencies in a separate step to avoid rebuilding all of them
2426
# every time the source code changes. This takes advantage of Docker's layer
2527
# caching, and it works by copying the Cargo.{toml,lock} with dummy source code
2628
# 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/
3133
RUN echo "fn main() {}" > src/main.rs && \
3234
echo "fn main() {}" > build.rs
3335

3436
RUN cargo fetch
3537
RUN cargo build --release
3638

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

43-
COPY --chown=cratesfyi build.rs build.rs
44+
COPY .git .git
45+
COPY build.rs build.rs
4446
RUN touch build.rs
45-
COPY --chown=cratesfyi src src/
47+
COPY src src/
4648
RUN find src -name "*.rs" -exec touch {} \;
47-
COPY --chown=cratesfyi templates/style.scss templates/
49+
COPY templates/style.scss templates/
4850

4951
RUN cargo build --release
5052

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
5371

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"]
5874
CMD ["daemon", "--foreground"]

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ Make sure you have docker-compose and are able to download ~10GB data on the fir
6060
```sh
6161
git clone https://github.com/rust-lang/docs.rs.git docs.rs
6262
cd docs.rs
63+
cp .env.sample .env
6364
docker-compose up # This may take a half hour or more on the first run
6465
```
6566

67+
If you need to store big files in the repository's directory it's recommended to
68+
put them in the `ignored/` subdirectory, which is ignored both by git and
69+
Docker.
70+
6671
### CLI
6772

6873
#### Starting web server
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Copyright 2015 The Rust Project Developers. See the COPYRIGHT
3+
* file at the top-level directory of this distribution and at
4+
* http://rust-lang.org/COPYRIGHT.
5+
*
6+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
* option. This file may not be copied, modified, or distributed
10+
* except according to those terms.
11+
*/
12+
13+
/* General structure and fonts */
14+
15+
body {
16+
background-color: white;
17+
color: black;
18+
}
19+
20+
h1, h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
21+
color: black;
22+
}
23+
h1.fqn {
24+
border-bottom-color: #D5D5D5;
25+
}
26+
h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
27+
border-bottom-color: #DDDDDD;
28+
}
29+
.in-band, code {
30+
background-color: white;
31+
}
32+
33+
.docblock code {
34+
background-color: #F5F5F5;
35+
}
36+
pre {
37+
background-color: #F5F5F5;
38+
}
39+
40+
.sidebar .location {
41+
background: #e1e1e1;
42+
color: #333;
43+
}
44+
45+
.block a:hover {
46+
background: #F5F5F5;
47+
}
48+
49+
.line-numbers span { color: #c67e2d; }
50+
.line-numbers .line-highlighted {
51+
background-color: #f6fdb0 !important;
52+
}
53+
54+
:target { background: #FDFFD3; }
55+
.content .highlighted {
56+
color: #000 !important;
57+
background-color: #ccc;
58+
}
59+
.content .highlighted a, .content .highlighted span { color: #000 !important; }
60+
.content .highlighted.trait { background-color: #fece7e; }
61+
.content .highlighted.mod { background-color: #afc6e4; }
62+
.content .highlighted.enum { background-color: #b4d1b9; }
63+
.content .highlighted.struct { background-color: #e7b1a0; }
64+
.content .highlighted.fn { background-color: #c6afb3; }
65+
.content .highlighted.method { background-color: #c6afb3; }
66+
.content .highlighted.tymethod { background-color: #c6afb3; }
67+
.content .highlighted.type { background-color: #c6afb3; }
68+
69+
.docblock h1, .docblock h2, .docblock h3, .docblock h4, .docblock h5 {
70+
border-bottom-color: #DDD;
71+
}
72+
73+
.docblock table {
74+
border-color: #ddd;
75+
}
76+
77+
.docblock table td {
78+
border-top-color: #ddd;
79+
border-bottom-color: #ddd;
80+
}
81+
82+
.docblock table th {
83+
border-top-color: #ddd;
84+
border-bottom-color: #ddd;
85+
}
86+
87+
.content a.primitive { color: #39a7bf; }
88+
.content span.externcrate, span.mod, .content a.mod, block a.current.mod { color: #4d76ae; }
89+
.content span.fn, .content a.fn, .block a.current.fn,
90+
.content span.method, .content a.method, .block a.current.method,
91+
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
92+
.content .fnname { color: #8c6067; }
93+
94+
pre.rust .comment { color: #8E908C; }
95+
pre.rust .doccomment { color: #4D4D4C; }
96+
97+
nav {
98+
border-bottom-color: #e0e0e0;
99+
}
100+
nav.main .current {
101+
border-top-color: #000;
102+
border-bottom-color: #000;
103+
}
104+
nav.main .separator {
105+
border-color: 1px solid #000;
106+
}
107+
a {
108+
color: #000;
109+
}
110+
111+
.docblock a, .stability a {
112+
color: #3873AD;
113+
}
114+
115+
a.test-arrow {
116+
color: #f5f5f5;
117+
}
118+
119+
.content span.trait, .content a.trait, .block a.current.trait { color: #7c5af3; }
120+
121+
.search-input {
122+
color: #555;
123+
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent;
124+
background-color: white;
125+
}
126+
127+
em.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
128+
em.stab.deprecated { background: #F3DFFF; border-color: #7F0087; }

0 commit comments

Comments
 (0)