Skip to content

Commit 8050b40

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

File tree

7 files changed

+205
-145
lines changed

7 files changed

+205
-145
lines changed

.github/workflows/ci.yml

+6-29
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: run database migrations
5651
run: cargo run -- database migrate
@@ -68,10 +63,7 @@ jobs:
6863
--database-url $DOCSRS_DATABASE_URL \
6964
--target-version 0
7065
71-
- name: Clean up the database
72-
run: docker compose down --volumes
7366
74-
test:
7567
env:
7668
SQLX_OFFLINE: 1
7769
runs-on: ubuntu-latest
@@ -92,13 +84,8 @@ jobs:
9284

9385
- name: Launch postgres and min.io
9486
run: |
95-
cp .env.sample .env
96-
mkdir -p ${DOCSRS_PREFIX}/public-html
97-
docker compose up -d db s3
98-
# Give the database enough time to start up
99-
sleep 5
100-
# Make sure the database is actually working
101-
psql "${DOCSRS_DATABASE_URL}"
87+
touch .docker.env
88+
docker compose up --wait --wait-timeout 30 db s3
10289
10390
- name: run workspace tests
10491
run: |
@@ -110,9 +97,6 @@ jobs:
11097
run: |
11198
cargo test --locked -- --ignored --test-threads=1
11299
113-
- name: Clean up the database
114-
run: docker compose down --volumes
115-
116100
GUI_test:
117101
runs-on: ubuntu-latest
118102
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

+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

+25-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,32 @@ 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
137+
```
138+
139+
If the command needs the crates.io-index clone then it must be run from within
140+
a `registry-watcher` container:
141+
142+
```sh
143+
docker compose run --rm registry-watcher queue set-last-seen-reference --head
133144
```
134145

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

152163
#### Rebuilding Containers
153164

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

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

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

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.
186+
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.
176187

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

@@ -200,11 +211,11 @@ cargo run -- start-web-server
200211
```sh
201212
# Builds <CRATE_NAME> <CRATE_VERSION> and adds it into database
202213
# 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`
214+
# For example, `docker compose run --rm builder-a build crate regex 1.1.6`
204215
cargo run -- build crate <CRATE_NAME> <CRATE_VERSION>
205216

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

209220
# Builds every crate on crates.io and adds them into database
210221
# (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 --build --wait`
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: {}

0 commit comments

Comments
 (0)