Skip to content

Commit 0366ef5

Browse files
committed
Run separate web server, registry watcher and build servers in docker-compose
1 parent 801e478 commit 0366ef5

File tree

7 files changed

+202
-156
lines changed

7 files changed

+202
-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

+109-38
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,118 @@
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+
volumes:
110+
- ".rustwide-docker/cli:/opt/docsrs/rustwide"
111+
environment: *environment
112+
env_file:
113+
- .docker.env
114+
profiles:
115+
- all
33116

34117
db:
35118
build:
@@ -44,10 +127,8 @@ services:
44127
# Use a non-standard port on the host to avoid conflicting with existing postgres servers
45128
- "127.0.0.1:15432:5432"
46129
healthcheck:
47-
test: ["CMD", "pg_isready", "--username", "cratesfyi"]
48-
interval: 10s
49-
timeout: 5s
50-
retries: 10
130+
<< : *healthcheck-interval
131+
test: pg_isready --username cratesfyi
51132

52133
s3:
53134
image: minio/minio
@@ -65,17 +146,8 @@ services:
65146
MINIO_ROOT_USER: cratesfyi
66147
MINIO_ROOT_PASSWORD: secret_key
67148
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
149+
<< : *healthcheck-interval
150+
test: mc ready local
79151

80152
prometheus:
81153
build:
@@ -84,11 +156,8 @@ services:
84156
ports:
85157
- "127.0.0.1:9090:9090"
86158
healthcheck:
87-
test:
88-
["CMD", "curl", "--silent", "--fail", "localhost:9090/-/ready"]
89-
interval: 10s
90-
timeout: 5s
91-
retries: 10
159+
<< : *healthcheck-interval
160+
test: promtool check healthy
92161

93162
gui_tests:
94163
build:
@@ -99,6 +168,8 @@ services:
99168
- "host.docker.internal:host-gateway"
100169
volumes:
101170
- "${PWD}:/build/out"
171+
profiles:
172+
- all
102173

103174
volumes:
104175
postgres-data: {}

0 commit comments

Comments
 (0)