Skip to content

Commit d5c6f33

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

File tree

7 files changed

+198
-143
lines changed

7 files changed

+198
-143
lines changed

.github/workflows/ci.yml

+6-30
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 10 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 10 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
build_tests:
134118
runs-on: ubuntu-latest
135119
needs: build
@@ -145,13 +129,8 @@ 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 10 db s3
155134
156135
- name: slow tests
157136
env:
@@ -164,9 +143,6 @@ jobs:
164143
$f --ignored --test-threads=1 || exit 1
165144
done
166145
167-
- name: Clean up the database
168-
run: docker compose down --volumes
169-
170146
fmt:
171147
name: Rustfmt
172148
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

+15-11
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
@@ -106,14 +106,18 @@ which uses docker-compose for the web server as well.
106106
This will not cache dependencies - in particular, you'll have to rebuild all 400 whenever the lockfile changes -
107107
but makes sure that you're in a known environment so you should have fewer problems getting started.
108108

109-
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):
109+
You'll need to `touch .docker.env` first, this file can have any environment
110+
variable overrides you want to use in docker containers.
111+
112+
You can also use the `cli` container to run builds on systems which don't support running builds directly (mostly on Mac OS or Windows):
113+
110114
```sh
111115
# run a build for a single crate
112-
docker compose run web build crate regex 1.3.1
116+
docker compose run --rm cli build crate regex 1.3.1
113117
# or build essential files
114-
docker compose run web build add-essential-files
115-
# rebuild the web container when you changed code.
116-
docker compose up -d web --build
118+
docker compose run --rm cli build add-essential-files
119+
# rebuild containers when you changed code.
120+
docker compose up --wait --build
117121
```
118122

119123
You can also run other commands like the setup above from within the container:
@@ -142,7 +146,7 @@ Three services are defined:
142146

143147
#### Rebuilding Containers
144148

145-
To rebuild the site, run `docker compose build`.
149+
To rebuild the site, run `docker compose --profile all build`.
146150
Note that docker-compose caches the build even if you change the source code,
147151
so this will be necessary anytime you make changes.
148152

@@ -163,7 +167,7 @@ This is probably because you have `git.autocrlf` set to true,
163167

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

166-
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.
170+
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 cli build [...]` workaround described above.
167171

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

@@ -191,11 +195,11 @@ cargo run -- start-web-server
191195
```sh
192196
# Builds <CRATE_NAME> <CRATE_VERSION> and adds it into database
193197
# This is the main command to build and add a documentation into docs.rs.
194-
# For example, `docker compose run web build crate regex 1.1.6`
198+
# For example, `docker compose run --rm cli build crate regex 1.1.6`
195199
cargo run -- build crate <CRATE_NAME> <CRATE_VERSION>
196200

197-
# alternatively, via the web container
198-
docker compose run web build crate <CRATE_NAME> <CRATE_VERSION>
201+
# alternatively, within docker-compose containers
202+
docker compose run --rm cli build crate <CRATE_NAME> <CRATE_VERSION>
199203

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

docker-compose.yml

+109-38
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,120 @@
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+
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
112+
- "/var/run/docker.sock:/var/run/docker.sock"
113+
environment: *environment
114+
env_file:
115+
- .docker.env
116+
profiles:
117+
- all
33118

34119
db:
35120
build:
@@ -44,10 +129,8 @@ services:
44129
# Use a non-standard port on the host to avoid conflicting with existing postgres servers
45130
- "127.0.0.1:15432:5432"
46131
healthcheck:
47-
test: ["CMD", "pg_isready", "--username", "cratesfyi"]
48-
interval: 10s
49-
timeout: 5s
50-
retries: 10
132+
<< : *healthcheck-interval
133+
test: pg_isready --username cratesfyi
51134

52135
s3:
53136
image: minio/minio
@@ -65,17 +148,8 @@ services:
65148
MINIO_ROOT_USER: cratesfyi
66149
MINIO_ROOT_PASSWORD: secret_key
67150
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
151+
<< : *healthcheck-interval
152+
test: curl --silent --fail localhost:9000/minio/health/ready
79153

80154
prometheus:
81155
build:
@@ -84,11 +158,8 @@ services:
84158
ports:
85159
- "127.0.0.1:9090:9090"
86160
healthcheck:
87-
test:
88-
["CMD", "curl", "--silent", "--fail", "localhost:9090/-/ready"]
89-
interval: 10s
90-
timeout: 5s
91-
retries: 10
161+
<< : *healthcheck-interval
162+
test: promtool check healthy
92163

93164
volumes:
94165
postgres-data: {}

0 commit comments

Comments
 (0)