Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 62d274d

Browse files
committedNov 18, 2023
Run separate web server, registry watcher and build servers in docker-compose
1 parent 801e478 commit 62d274d

File tree

7 files changed

+200
-156
lines changed

7 files changed

+200
-156
lines changed
 

‎.github/workflows/ci.yml

+8-40
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@ jobs:
3838

3939
- name: Launch postgres
4040
run: |
41-
cp .env.sample .env
42-
mkdir -p ${DOCSRS_PREFIX}/public-html
43-
docker compose up -d db
44-
# Give the database enough time to start up
45-
sleep 5
46-
# Make sure the database is actually working
47-
psql "${DOCSRS_DATABASE_URL}"
41+
touch .docker.env
42+
docker compose up --wait --wait-timeout 30 db
4843
4944
- name: run database migrations
5045
run: cargo run -- database migrate
@@ -60,9 +55,6 @@ jobs:
6055
--check \
6156
-- --all-targets --all-features
6257
63-
- name: Clean up the database
64-
run: docker compose down --volumes
65-
6658
build:
6759
runs-on: ubuntu-latest
6860
steps:
@@ -110,13 +102,8 @@ jobs:
110102

111103
- name: Launch postgres and min.io
112104
run: |
113-
cp .env.sample .env
114-
mkdir -p ${DOCSRS_PREFIX}/public-html
115-
docker compose up -d db s3
116-
# Give the database enough time to start up
117-
sleep 5
118-
# Make sure the database is actually working
119-
psql "${DOCSRS_DATABASE_URL}"
105+
touch .docker.env
106+
docker compose up --wait --wait-timeout 30 db s3
120107
121108
- name: run tests
122109
shell: bash
@@ -127,9 +114,6 @@ jobs:
127114
$f || exit 1
128115
done
129116
130-
- name: Clean up the database
131-
run: docker compose down --volumes
132-
133117
GUI_test:
134118
runs-on: ubuntu-latest
135119
needs: build
@@ -145,20 +129,12 @@ jobs:
145129

146130
- name: Launch postgres and min.io
147131
run: |
148-
cp .env.sample .env
149-
mkdir -p ${DOCSRS_PREFIX}/public-html
150-
docker compose up -d db s3
151-
# Give the database enough time to start up
152-
sleep 5
153-
# Make sure the database is actually working
154-
psql "${DOCSRS_DATABASE_URL}"
132+
touch .docker.env
133+
docker compose up --wait --wait-timeout 30 db s3
155134
156135
- name: Run GUI tests
157136
run: ./dockerfiles/run-gui-tests.sh
158137

159-
- name: Clean up the database
160-
run: docker compose down --volumes
161-
162138
build_tests:
163139
runs-on: ubuntu-latest
164140
needs: build
@@ -174,13 +150,8 @@ jobs:
174150

175151
- name: Launch postgres and min.io
176152
run: |
177-
cp .env.sample .env
178-
mkdir -p ${DOCSRS_PREFIX}/public-html
179-
docker compose up -d db s3
180-
# Give the database enough time to start up
181-
sleep 5
182-
# Make sure the database is actually working
183-
psql "${DOCSRS_DATABASE_URL}"
153+
touch .docker.env
154+
docker compose up --wait --wait-timeout 30 db s3
184155
185156
- name: slow tests
186157
env:
@@ -193,9 +164,6 @@ jobs:
193164
$f --ignored --test-threads=1 || exit 1
194165
done
195166
196-
- name: Clean up the database
197-
run: docker compose down --volumes
198-
199167
fmt:
200168
name: Rustfmt
201169
runs-on: ubuntu-latest

‎.github/workflows/docker.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v4
1111

12-
- name: Build the Docker image
13-
run: docker build -t docs-rs -f dockerfiles/Dockerfile .
12+
- run: docker build --target web-server -f dockerfiles/Dockerfile .
13+
- run: docker build --target build-server -f dockerfiles/Dockerfile .
14+
- run: docker build --target registry-watcher -f dockerfiles/Dockerfile .
15+
- run: docker build --target cli -f dockerfiles/Dockerfile .

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/ignored
22
/.env
3+
/.docker.env
34
/src/web/badge/Cargo.lock
45
target
56
*.css

‎README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mkdir -p ignored/cratesfyi-prefix/crates.io-index
6464
# Builds the docs.rs binary
6565
cargo build
6666
# Start the external services.
67-
docker compose up -d db s3
67+
docker compose up --wait db s3
6868
# anything that doesn't run via docker-compose needs the settings defined in
6969
# .env. Either via `. ./.env` as below, or via any dotenv shell integration.
7070
. ./.env
@@ -115,21 +115,25 @@ which uses docker-compose for the web server as well.
115115
This will not cache dependencies - in particular, you'll have to rebuild all 400 whenever the lockfile changes -
116116
but makes sure that you're in a known environment so you should have fewer problems getting started.
117117

118-
You can also use the `web` container to run builds on systems which don't support running builds directly (mostly on Mac OS or Windows):
118+
You'll need to `touch .docker.env` first, this file can have any environment
119+
variable overrides you want to use in docker containers.
120+
121+
You can also use the `builder-a` container to run builds on systems which don't support running builds directly (mostly on Mac OS or Windows):
122+
119123
```sh
124+
# update the toolchain
125+
docker compose run --rm builder-a build update-toolchain
120126
# run a build for a single crate
121-
docker compose run web build crate regex 1.3.1
122-
# or build essential files
123-
docker compose run web build add-essential-files
124-
# rebuild the web container when you changed code.
125-
docker compose up -d web --build
127+
docker compose run --rm builder-a build crate regex 1.3.1
128+
# rebuild containers when you changed code.
129+
docker compose up --wait --build
126130
```
127131

128-
You can also run other commands like the setup above from within the container:
132+
You can also run other non-build commands like the setup steps above, or queueing crates for the background builders from within the `cli` container:
129133

130134
```sh
131135
docker compose run --rm cli database migrate
132-
docker compose run --rm cli build update-toolchain
136+
docker compose run --rm cli queue add regex 1.3.1
133137
```
134138

135139
Note that running tests is not supported when using pure docker-compose.
@@ -151,7 +155,7 @@ Three services are defined:
151155

152156
#### Rebuilding Containers
153157

154-
To rebuild the site, run `docker compose build`.
158+
To rebuild the site, run `docker compose --profile all build`.
155159
Note that docker-compose caches the build even if you change the source code,
156160
so this will be necessary anytime you make changes.
157161

@@ -172,7 +176,7 @@ This is probably because you have `git.autocrlf` set to true,
172176

173177
##### I see the error `/opt/rustwide/cargo-home/bin/cargo: cannot execute binary file: Exec format error` when running builds.
174178

175-
You are most likely not on a Linux platform. Running builds directly is only supported on `x86_64-unknown-linux-gnu`. On other platforms you can use the `docker compose run web build [...]` workaround described above.
179+
You are most likely not on a Linux platform. Running builds directly is only supported on `x86_64-unknown-linux-gnu`. On other platforms you can use the `docker compose run --rm builder-a build [...]` workaround described above.
176180

177181
See [rustwide#41](https://github.com/rust-lang/rustwide/issues/41) for more details about supporting more platforms directly.
178182

@@ -200,11 +204,11 @@ cargo run -- start-web-server
200204
```sh
201205
# Builds <CRATE_NAME> <CRATE_VERSION> and adds it into database
202206
# This is the main command to build and add a documentation into docs.rs.
203-
# For example, `docker compose run web build crate regex 1.1.6`
207+
# For example, `docker compose run --rm builder-a build crate regex 1.1.6`
204208
cargo run -- build crate <CRATE_NAME> <CRATE_VERSION>
205209

206-
# alternatively, via the web container
207-
docker compose run web build crate <CRATE_NAME> <CRATE_VERSION>
210+
# alternatively, within docker-compose containers
211+
docker compose run --rm builder-a build crate <CRATE_NAME> <CRATE_VERSION>
208212

209213
# Builds every crate on crates.io and adds them into database
210214
# (beware: this may take months to finish)

‎docker-compose.yml

+107-38
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,116 @@
1-
version: "3"
1+
version: "3.4"
2+
3+
x-healthcheck: &healthcheck-interval
4+
interval: 1s
5+
timeout: 1s
6+
start_period: 10s
7+
# TODO: https://github.com/docker/compose/issues/10461
8+
# interval: 10s
9+
# start_interval: 1s
10+
11+
x-environment: &environment
12+
RUST_BACKTRACE: true
13+
14+
DOCSRS_PREFIX: /opt/docsrs/prefix
15+
16+
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
17+
DOCSRS_MIN_POOL_SIZE: 2
18+
DOCSRS_MAX_POOL_SIZE: 10
19+
20+
DOCSRS_STORAGE_BACKEND: s3
21+
22+
S3_ENDPOINT: http://s3:9000
23+
AWS_ACCESS_KEY_ID: cratesfyi
24+
AWS_SECRET_ACCESS_KEY: secret_key
25+
26+
DOCSRS_RENDER_THREADS: 2
27+
28+
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
29+
DOCSRS_DOCKER: true
30+
DOCSRS_DOCKER_IMAGE: ghcr.io/rust-lang/crates-build-env/linux-micro
31+
DOCSRS_BUILD_CPU_LIMIT: 2
32+
DOCSRS_INCLUDE_DEFAULT_TARGETS: false
33+
34+
x-builder: &builder
35+
build:
36+
context: .
37+
dockerfile: ./dockerfiles/Dockerfile
38+
target: build-server
39+
depends_on:
40+
- db
41+
- s3
42+
environment: *environment
43+
env_file:
44+
- .docker.env
45+
healthcheck:
46+
<< : *healthcheck-interval
47+
test: curl --silent --fail localhost:3000/about/metrics
48+
249
services:
350
web:
451
build:
552
context: .
653
dockerfile: ./dockerfiles/Dockerfile
54+
target: web-server
755
platform: "linux/amd64"
856
depends_on:
957
- db
1058
- s3
1159
ports:
12-
- "3000:3000"
13-
# for metrics
14-
expose: ["3000"]
60+
- "3000:80"
61+
environment: *environment
62+
env_file:
63+
- .docker.env
64+
healthcheck:
65+
<< : *healthcheck-interval
66+
test: curl --silent --fail localhost:80/about/metrics
67+
68+
# Include the registry watcher with `docker compose --profile watch up -d`
69+
registry-watcher:
70+
build:
71+
context: .
72+
dockerfile: ./dockerfiles/Dockerfile
73+
target: registry-watcher
74+
platform: "linux/amd64"
75+
depends_on:
76+
- db
1577
volumes:
16-
- "/var/run/docker.sock:/var/run/docker.sock"
17-
- ".rustwide-docker:/opt/docsrs/rustwide"
1878
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
19-
environment:
20-
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
21-
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
22-
DOCSRS_STORAGE_BACKEND: s3
23-
S3_ENDPOINT: http://s3:9000
24-
AWS_ACCESS_KEY_ID: cratesfyi
25-
AWS_SECRET_ACCESS_KEY: secret_key
79+
environment: *environment
2680
env_file:
27-
- .env
81+
- .docker.env
82+
profiles:
83+
- watch
84+
- all
2885
healthcheck:
29-
test: ["CMD", "curl", "--silent", "--fail", "localhost:3000"]
30-
interval: 10s
31-
timeout: 5s
32-
retries: 10
86+
<< : *healthcheck-interval
87+
test: curl --silent --fail localhost:3000/about/metrics
88+
89+
builder-a:
90+
<< : *builder
91+
volumes:
92+
- ".rustwide-docker/builder-a:/opt/docsrs/rustwide"
93+
- "/var/run/docker.sock:/var/run/docker.sock"
94+
95+
builder-b:
96+
<< : *builder
97+
volumes:
98+
- ".rustwide-docker/builder-b:/opt/docsrs/rustwide"
99+
- "/var/run/docker.sock:/var/run/docker.sock"
100+
101+
cli:
102+
build:
103+
context: .
104+
dockerfile: ./dockerfiles/Dockerfile
105+
target: cli
106+
depends_on:
107+
- db
108+
- s3
109+
environment: *environment
110+
env_file:
111+
- .docker.env
112+
profiles:
113+
- all
33114

34115
db:
35116
build:
@@ -44,10 +125,8 @@ services:
44125
# Use a non-standard port on the host to avoid conflicting with existing postgres servers
45126
- "127.0.0.1:15432:5432"
46127
healthcheck:
47-
test: ["CMD", "pg_isready", "--username", "cratesfyi"]
48-
interval: 10s
49-
timeout: 5s
50-
retries: 10
128+
<< : *healthcheck-interval
129+
test: pg_isready --username cratesfyi
51130

52131
s3:
53132
image: minio/minio
@@ -65,17 +144,8 @@ services:
65144
MINIO_ROOT_USER: cratesfyi
66145
MINIO_ROOT_PASSWORD: secret_key
67146
healthcheck:
68-
test:
69-
[
70-
"CMD",
71-
"curl",
72-
"--silent",
73-
"--fail",
74-
"localhost:9000/minio/health/ready",
75-
]
76-
interval: 10s
77-
timeout: 5s
78-
retries: 10
147+
<< : *healthcheck-interval
148+
test: mc ready local
79149

80150
prometheus:
81151
build:
@@ -84,11 +154,8 @@ services:
84154
ports:
85155
- "127.0.0.1:9090:9090"
86156
healthcheck:
87-
test:
88-
["CMD", "curl", "--silent", "--fail", "localhost:9090/-/ready"]
89-
interval: 10s
90-
timeout: 5s
91-
retries: 10
157+
<< : *healthcheck-interval
158+
test: promtool check healthy
92159

93160
gui_tests:
94161
build:
@@ -99,6 +166,8 @@ services:
99166
- "host.docker.internal:host-gateway"
100167
volumes:
101168
- "${PWD}:/build/out"
169+
profiles:
170+
- all
102171

103172
volumes:
104173
postgres-data: {}

‎dockerfiles/Dockerfile

+62-24
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,80 @@ RUN apt-get update \
6565
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
6666
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
6767
ca-certificates \
68+
curl \
6869
tini \
6970
&& rm -rf /var/lib/apt/lists/*
7071

72+
WORKDIR /srv/docsrs
73+
74+
# Tini is a small init binary to properly handle signals
75+
ENTRYPOINT ["/usr/bin/tini", "/usr/local/bin/cratesfyi", "--"]
76+
CMD ["start-web-server", "0.0.0.0:80"]
77+
7178
COPY --from=build /build/target/release/cratesfyi /usr/local/bin
7279
COPY static /srv/docsrs/static
7380
COPY templates /srv/docsrs/templates
7481
COPY vendor /srv/docsrs/vendor
7582

76-
WORKDIR /srv/docsrs
83+
########################
84+
# Build server stage #
85+
########################
86+
87+
FROM ubuntu:22.04 AS build-server
88+
89+
RUN apt-get update \
90+
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
91+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
92+
ca-certificates \
93+
tini \
94+
curl \
95+
docker.io \
96+
build-essential \
97+
gcc \
98+
pkg-config \
99+
libssl-dev \
100+
&& rm -rf /var/lib/apt/lists/*
101+
77102
# Tini is a small init binary to properly handle signals
78-
CMD ["/usr/bin/tini", "/usr/local/bin/cratesfyi", "start-web-server", "0.0.0.0:80"]
103+
ENTRYPOINT ["/usr/bin/tini", "/usr/local/bin/cratesfyi", "--"]
104+
CMD ["start-build-server"]
79105

80-
##################
81-
# Output stage #
82-
##################
106+
COPY --from=build /build/target/release/cratesfyi /usr/local/bin
107+
108+
############################
109+
# Registry watcher stage #
110+
############################
83111

84-
FROM ubuntu:22.04 AS output
112+
FROM ubuntu:22.04 AS registry-watcher
85113

86-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
87-
git \
88-
libmagic1 \
89-
docker.io \
90-
ca-certificates \
91-
build-essential \
92-
gcc \
93-
pkg-config \
94-
libssl-dev
114+
RUN apt-get update \
115+
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
116+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
117+
ca-certificates \
118+
tini \
119+
curl \
120+
git \
121+
&& rm -rf /var/lib/apt/lists/*
122+
123+
# Tini is a small init binary to properly handle signals
124+
ENTRYPOINT ["/usr/bin/tini", "/usr/local/bin/cratesfyi", "--"]
125+
CMD ["start-registry-watcher", "--repository-stats-updater=enabled", "--cdn-invalidator=enabled"]
126+
127+
COPY --from=build /build/target/release/cratesfyi /usr/local/bin
128+
129+
###############
130+
# CLI stage #
131+
###############
132+
133+
FROM ubuntu:22.04 AS cli
134+
135+
RUN apt-get update \
136+
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
137+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
138+
ca-certificates \
139+
tini \
140+
&& rm -rf /var/lib/apt/lists/*
95141

96-
RUN mkdir -p /opt/docsrs/prefix
142+
ENTRYPOINT ["/usr/bin/tini", "/usr/local/bin/cratesfyi", "--"]
97143

98144
COPY --from=build /build/target/release/cratesfyi /usr/local/bin
99-
COPY static /opt/docsrs/static
100-
COPY templates /opt/docsrs/templates
101-
COPY dockerfiles/entrypoint.sh /opt/docsrs/
102-
COPY vendor /opt/docsrs/vendor
103-
104-
WORKDIR /opt/docsrs
105-
ENTRYPOINT ["/opt/docsrs/entrypoint.sh"]
106-
CMD ["daemon", "--registry-watcher=disabled"]

‎dockerfiles/entrypoint.sh

-38
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.