Skip to content

Commit ab6028b

Browse files
committed
Run separate web server, registry watcher and build servers in docker-compose
1 parent fb15c29 commit ab6028b

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
@@ -42,13 +42,8 @@ jobs:
4242

4343
- name: Launch postgres
4444
run: |
45-
cp .env.sample .env
46-
mkdir -p ${DOCSRS_PREFIX}/public-html
47-
docker compose up -d db
48-
# Give the database enough time to start up
49-
sleep 5
50-
# Make sure the database is actually working
51-
psql "${DOCSRS_DATABASE_URL}"
45+
touch .docker.env
46+
docker compose up --wait --wait-timeout 30 db
5247
5348
- name: install SQLX CLI
5449
run: cargo install sqlx-cli --no-default-features --features postgres
@@ -66,9 +61,6 @@ jobs:
6661
--database-url $DOCSRS_DATABASE_URL \
6762
--target-version 0
6863
69-
- name: Clean up the database
70-
run: docker compose down --volumes
71-
7264
test:
7365
env:
7466
SQLX_OFFLINE: 1
@@ -91,13 +83,8 @@ jobs:
9183

9284
- name: Launch postgres and min.io
9385
run: |
94-
cp .env.sample .env
95-
mkdir -p ${DOCSRS_PREFIX}/public-html
96-
docker compose up -d db s3
97-
# Give the database enough time to start up
98-
sleep 5
99-
# Make sure the database is actually working
100-
psql "${DOCSRS_DATABASE_URL}"
86+
touch .docker.env
87+
docker compose up --wait --wait-timeout 30 db s3
10188
10289
- name: run workspace tests
10390
run: |
@@ -109,9 +96,6 @@ jobs:
10996
run: |
11097
cargo test --locked -- --ignored --test-threads=1
11198
112-
- name: Clean up the database
113-
run: docker compose down --volumes
114-
11599
GUI_test:
116100
runs-on: ubuntu-latest
117101
steps:
@@ -129,19 +113,12 @@ jobs:
129113

130114
- name: Launch postgres and min.io
131115
run: |
132-
cp .env.sample .env
133-
mkdir -p ${DOCSRS_PREFIX}/public-html
134-
docker compose up -d db s3
135-
# Give the database enough time to start up
136-
sleep 5
137-
# Make sure the database is actually working
138-
psql "${DOCSRS_DATABASE_URL}"
116+
touch .docker.env
117+
docker compose up --wait --wait-timeout 30 db s3
139118
140119
- name: Run GUI tests
141120
run: ./dockerfiles/run-gui-tests.sh
142121

143-
- name: Clean up the database
144-
run: docker compose down --volumes
145122

146123
fmt:
147124
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@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

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
@@ -121,21 +121,32 @@ which uses docker-compose for the web server as well.
121121
This will not cache dependencies - in particular, you'll have to rebuild all 400 whenever the lockfile changes -
122122
but makes sure that you're in a known environment so you should have fewer problems getting started.
123123

124-
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):
124+
You'll need to `touch .docker.env` first, this file can have any environment
125+
variable overrides you want to use in docker containers.
126+
127+
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):
128+
125129
```sh
130+
# update the toolchain
131+
docker compose run --rm builder-a build update-toolchain
126132
# run a build for a single crate
127-
docker compose run web build crate regex 1.3.1
128-
# or build essential files
129-
docker compose run web build add-essential-files
130-
# rebuild the web container when you changed code.
131-
docker compose up -d web --build
133+
docker compose run --rm builder-a build crate regex 1.3.1
134+
# rebuild containers when you changed code.
135+
docker compose up --wait --build
132136
```
133137

134-
You can also run other commands like the setup above from within the container:
138+
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:
135139

136140
```sh
137141
docker compose run --rm cli database migrate
138-
docker compose run --rm cli build update-toolchain
142+
docker compose run --rm cli queue add regex 1.3.1
143+
```
144+
145+
If the command needs the crates.io-index clone then it must be run from within
146+
a `registry-watcher` container:
147+
148+
```sh
149+
docker compose run --rm registry-watcher queue set-last-seen-reference --head
139150
```
140151

141152
Note that running tests is not supported when using pure docker-compose.
@@ -157,7 +168,7 @@ Three services are defined:
157168

158169
#### Rebuilding Containers
159170

160-
To rebuild the site, run `docker compose build`.
171+
To rebuild the site, run `docker compose --profile all build`.
161172
Note that docker-compose caches the build even if you change the source code,
162173
so this will be necessary anytime you make changes.
163174

@@ -178,7 +189,7 @@ This is probably because you have `git.autocrlf` set to true,
178189

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

181-
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.
192+
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.
182193

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

@@ -206,11 +217,11 @@ cargo run -- start-web-server
206217
```sh
207218
# Builds <CRATE_NAME> <CRATE_VERSION> and adds it into database
208219
# This is the main command to build and add a documentation into docs.rs.
209-
# For example, `docker compose run web build crate regex 1.1.6`
220+
# For example, `docker compose run --rm builder-a build crate regex 1.1.6`
210221
cargo run -- build crate <CRATE_NAME> <CRATE_VERSION>
211222

212-
# alternatively, via the web container
213-
docker compose run web build crate <CRATE_NAME> <CRATE_VERSION>
223+
# alternatively, within docker-compose containers
224+
docker compose run --rm builder-a build crate <CRATE_NAME> <CRATE_VERSION>
214225

215226
# Builds every crate on crates.io and adds them into database
216227
# (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)