Skip to content

Commit 3890f8b

Browse files
Nemo157syphar
authored andcommitted
Run separate web server, registry watcher and build servers in docker-compose
1 parent 98d36bc commit 3890f8b

File tree

7 files changed

+218
-155
lines changed

7 files changed

+218
-155
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ jobs:
4444

4545
- name: Launch postgres
4646
run: |
47-
cp .env.sample .env
48-
mkdir -p ${DOCSRS_PREFIX}/public-html
49-
docker compose up -d db
50-
# Give the database enough time to start up
51-
sleep 5
52-
# Make sure the database is actually working
53-
psql "${DOCSRS_DATABASE_URL}"
47+
touch .docker.env
48+
docker compose up --wait --wait-timeout 30 db
5449
5550
- name: install SQLX CLI
5651
run: cargo install sqlx-cli --no-default-features --features postgres
@@ -68,9 +63,6 @@ jobs:
6863
--database-url $DOCSRS_DATABASE_URL \
6964
--target-version 0
7065
71-
- name: Clean up the database
72-
run: docker compose down --volumes
73-
7466
test:
7567
env:
7668
SQLX_OFFLINE: 1
@@ -88,13 +80,8 @@ jobs:
8880

8981
- name: Launch postgres and min.io
9082
run: |
91-
cp .env.sample .env
92-
mkdir -p ${DOCSRS_PREFIX}/public-html
93-
docker compose up -d db s3
94-
# Give the database enough time to start up
95-
sleep 5
96-
# Make sure the database is actually working
97-
psql "${DOCSRS_DATABASE_URL}"
83+
touch .docker.env
84+
docker compose up --wait --wait-timeout 30 db s3
9885
9986
- name: run workspace tests
10087
run: |
@@ -106,9 +93,6 @@ jobs:
10693
run: |
10794
cargo test --locked -- --ignored --test-threads=1
10895
109-
- name: Clean up the database
110-
run: docker compose down --volumes
111-
11296
GUI_test:
11397
runs-on: ubuntu-latest
11498
steps:
@@ -121,19 +105,12 @@ jobs:
121105

122106
- name: Launch postgres and min.io
123107
run: |
124-
cp .env.sample .env
125-
mkdir -p ${DOCSRS_PREFIX}/public-html
126-
docker compose up -d db s3
127-
# Give the database enough time to start up
128-
sleep 5
129-
# Make sure the database is actually working
130-
psql "${DOCSRS_DATABASE_URL}"
108+
touch .docker.env
109+
docker compose up --wait --wait-timeout 30 db s3
131110
132111
- name: Run GUI tests
133112
run: ./dockerfiles/run-gui-tests.sh
134113

135-
- name: Clean up the database
136-
run: docker compose down --volumes
137114

138115
fmt:
139116
name: Rustfmt

.github/workflows/docker.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v5
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mkdir -p ignored/cratesfyi-prefix/crates.io-index
7070
# Builds the docs.rs binary
7171
SQLX_OFFLINE=1 cargo build
7272
# Start the external services.
73-
docker compose up -d db s3
73+
docker compose up --wait db s3
7474
# anything that doesn't run via docker-compose needs the settings defined in
7575
# .env. Either via `. ./.env` as below, or via any dotenv shell integration.
7676
. ./.env
@@ -133,21 +133,32 @@ which uses docker-compose for the web server as well.
133133
This will not cache dependencies - in particular, you'll have to rebuild all 400 whenever the lockfile changes -
134134
but makes sure that you're in a known environment so you should have fewer problems getting started.
135135

136-
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):
136+
You'll need to `touch .docker.env` first, this file can have any environment
137+
variable overrides you want to use in docker containers.
138+
139+
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):
140+
137141
```sh
142+
# update the toolchain
143+
docker compose run --rm builder-a build update-toolchain
138144
# run a build for a single crate
139-
docker compose run web build crate regex 1.3.1
140-
# or build essential files
141-
docker compose run web build add-essential-files
142-
# rebuild the web container when you changed code.
143-
docker compose up -d web --build
145+
docker compose run --rm builder-a build crate regex 1.3.1
146+
# rebuild containers when you changed code.
147+
docker compose up --wait --build
144148
```
145149

146-
You can also run other commands like the setup above from within the container:
150+
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:
147151

148152
```sh
149153
docker compose run --rm cli database migrate
150-
docker compose run --rm cli build update-toolchain
154+
docker compose run --rm cli queue add regex 1.3.1
155+
```
156+
157+
If the command needs the crates.io-index clone then it must be run from within
158+
a `registry-watcher` container:
159+
160+
```sh
161+
docker compose run --rm registry-watcher queue set-last-seen-reference --head
151162
```
152163

153164
Note that running tests is not supported when using pure docker-compose.
@@ -169,7 +180,7 @@ Three services are defined:
169180

170181
#### Rebuilding Containers
171182

172-
To rebuild the site, run `docker compose build`.
183+
To rebuild the site, run `docker compose --profile all build`.
173184
Note that docker-compose caches the build even if you change the source code,
174185
so this will be necessary anytime you make changes.
175186

@@ -190,7 +201,7 @@ This is probably because you have `git.autocrlf` set to true,
190201

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

193-
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.
204+
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.
194205

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

@@ -218,11 +229,11 @@ cargo run -- start-web-server
218229
```sh
219230
# Builds <CRATE_NAME> <CRATE_VERSION> and adds it into database
220231
# This is the main command to build and add a documentation into docs.rs.
221-
# For example, `docker compose run web build crate regex 1.1.6`
232+
# For example, `docker compose run --rm builder-a build crate regex 1.1.6`
222233
cargo run -- build crate <CRATE_NAME> <CRATE_VERSION>
223234

224-
# alternatively, via the web container
225-
docker compose run web build crate <CRATE_NAME> <CRATE_VERSION>
235+
# alternatively, within docker-compose containers
236+
docker compose run --rm builder-a build crate <CRATE_NAME> <CRATE_VERSION>
226237

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

docker-compose.yml

Lines changed: 118 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,126 @@
1-
version: "3"
1+
x-healthcheck: &healthcheck-interval
2+
interval: 10s
3+
timeout: 1s
4+
start_interval: 1s
5+
start_period: 10s
6+
7+
x-environment: &environment
8+
RUST_BACKTRACE: true
9+
10+
DOCSRS_PREFIX: /opt/docsrs/prefix
11+
DOCSRS_COMPILER_METRICS_PATH: /opt/docsrs/prefix/metrics
12+
13+
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
14+
DOCSRS_MIN_POOL_IDLE: 1
15+
DOCSRS_MIN_POOL_SIZE: 2
16+
DOCSRS_MAX_POOL_SIZE: 10
17+
DOCSRS_MAX_LEGACY_POOL_SIZE: 10
18+
19+
DOCSRS_STORAGE_BACKEND: s3
20+
21+
S3_ENDPOINT: http://s3:9000
22+
AWS_ACCESS_KEY_ID: cratesfyi
23+
AWS_SECRET_ACCESS_KEY: secret_key
24+
25+
DOCSRS_RENDER_THREADS: 2
26+
27+
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
28+
DOCSRS_DOCKER: true
29+
DOCSRS_DOCKER_IMAGE: ghcr.io/rust-lang/crates-build-env/linux-micro
30+
DOCSRS_BUILD_CPU_LIMIT: 2
31+
DOCSRS_INCLUDE_DEFAULT_TARGETS: false
32+
33+
x-build: &build
34+
context: .
35+
dockerfile: ./dockerfiles/Dockerfile
36+
args:
37+
PROFILE: dev
38+
PROFILE_DIR: debug
39+
40+
x-builder: &builder
41+
build:
42+
<<: *build
43+
target: build-server
44+
platform: "linux/amd64"
45+
depends_on:
46+
- db
47+
- s3
48+
environment: *environment
49+
env_file:
50+
- .docker.env
51+
healthcheck:
52+
<<: *healthcheck-interval
53+
test: curl --silent --fail localhost:3000/about/metrics
54+
255
services:
356
web:
457
build:
5-
context: .
6-
dockerfile: ./dockerfiles/Dockerfile
7-
args:
8-
PROFILE: dev
9-
PROFILE_DIR: debug
58+
<<: *build
59+
target: web-server
1060
platform: "linux/amd64"
1161
depends_on:
1262
- db
1363
- s3
1464
ports:
15-
- "3000:3000"
16-
# for metrics
17-
expose: ["3000"]
65+
- "3000:80"
66+
environment: *environment
67+
env_file:
68+
- .docker.env
69+
healthcheck:
70+
<<: *healthcheck-interval
71+
test: curl --silent --fail localhost:80/about/metrics
72+
73+
# Include the registry watcher with
74+
# `docker compose --profile watch up --build --wait`
75+
registry-watcher:
76+
build:
77+
<<: *build
78+
target: registry-watcher
79+
platform: "linux/amd64"
80+
depends_on:
81+
- db
1882
volumes:
19-
- "/var/run/docker.sock:/var/run/docker.sock"
20-
- ".rustwide-docker:/opt/docsrs/rustwide"
2183
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
22-
- "./ignored/cratesfyi-prefix/metrics:/opt/docsrs/prefix/metrics"
2384
- "./static:/opt/docsrs/static:ro"
24-
environment:
25-
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
26-
DOCSRS_COMPILER_METRICS_PATH: /opt/docsrs/prefix/metrics
27-
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
28-
DOCSRS_STORAGE_BACKEND: s3
29-
S3_ENDPOINT: http://s3:9000
30-
AWS_ACCESS_KEY_ID: cratesfyi
31-
AWS_SECRET_ACCESS_KEY: secret_key
32-
DOCSRS_MAX_LEGACY_POOL_SIZE: 10
33-
DOCSRS_MAX_POOL_SIZE: 10
34-
DOCSRS_MIN_POOL_IDLE: 1
85+
environment: *environment
3586
env_file:
36-
- .env
87+
- .docker.env
88+
profiles:
89+
- watch
90+
- all
3791
healthcheck:
38-
test: ["CMD", "curl", "--silent", "--fail", "localhost:3000"]
39-
interval: 10s
40-
timeout: 5s
41-
retries: 10
92+
<<: *healthcheck-interval
93+
test: curl --silent --fail localhost:3000/about/metrics
94+
95+
builder-a:
96+
<<: *builder
97+
volumes:
98+
- ".rustwide-docker/builder-a:/opt/docsrs/rustwide"
99+
- "./ignored/cratesfyi-prefix/metrics:/opt/docsrs/prefix/metrics"
100+
- "/var/run/docker.sock:/var/run/docker.sock"
101+
102+
builder-b:
103+
<<: *builder
104+
volumes:
105+
- ".rustwide-docker/builder-b:/opt/docsrs/rustwide"
106+
- "./ignored/cratesfyi-prefix/metrics:/opt/docsrs/prefix/metrics"
107+
- "/var/run/docker.sock:/var/run/docker.sock"
108+
109+
cli:
110+
build:
111+
<<: *build
112+
target: cli
113+
platform: "linux/amd64"
114+
depends_on:
115+
- db
116+
- s3
117+
volumes:
118+
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
119+
environment: *environment
120+
env_file:
121+
- .docker.env
122+
profiles:
123+
- all
42124

43125
db:
44126
build:
@@ -53,10 +135,8 @@ services:
53135
# Use a non-standard port on the host to avoid conflicting with existing postgres servers
54136
- "127.0.0.1:15432:5432"
55137
healthcheck:
56-
test: ["CMD", "pg_isready", "--username", "cratesfyi"]
57-
interval: 10s
58-
timeout: 5s
59-
retries: 10
138+
<<: *healthcheck-interval
139+
test: pg_isready --username cratesfyi
60140

61141
s3:
62142
image: minio/minio
@@ -74,17 +154,8 @@ services:
74154
MINIO_ROOT_USER: cratesfyi
75155
MINIO_ROOT_PASSWORD: secret_key
76156
healthcheck:
77-
test:
78-
[
79-
"CMD",
80-
"curl",
81-
"--silent",
82-
"--fail",
83-
"localhost:9000/minio/health/ready",
84-
]
85-
interval: 10s
86-
timeout: 5s
87-
retries: 10
157+
<<: *healthcheck-interval
158+
test: mc ready local
88159

89160
prometheus:
90161
build:
@@ -93,11 +164,8 @@ services:
93164
ports:
94165
- "127.0.0.1:9090:9090"
95166
healthcheck:
96-
test:
97-
["CMD", "curl", "--silent", "--fail", "localhost:9090/-/ready"]
98-
interval: 10s
99-
timeout: 5s
100-
retries: 10
167+
<<: *healthcheck-interval
168+
test: promtool check healthy
101169

102170
gui_tests:
103171
build:
@@ -108,6 +176,8 @@ services:
108176
- "host.docker.internal:host-gateway"
109177
volumes:
110178
- "${PWD}:/build/out"
179+
profiles:
180+
- all
111181

112182
volumes:
113183
postgres-data: {}

0 commit comments

Comments
 (0)