Skip to content

Commit 5eefa4d

Browse files
authored
Update docker-compose (#386)
* Provide a convenience script * Add easy suport for profiles * Remove version since it's deprecated for docker 2+ * Always mount source as read-only * Bake PYTHONPATH into images that need it * Mount code only in dev mode * Provide script for compose down regardless of profile * Use dev compose to mount code directories * Make adding the mount volumes optional through environment variable * Mount script as read-only * Add documentation on the added scripts * Mount the override configuration for the metadata catalogue * Add echo for symmetry with up * Mount the override file instead of the defaults * override should be ignored if it is a directory When the file does not exist on the host machine, docker-compose will create the "file" as a directory when mounting. This is harmless so long as we handle that case correctly. * Add a colored message to remind the user if local changes are mounted * Rename AIOD_MOUNT to USE_LOCAL_DEV to allow broader scope Also require set to "true" rather than any value, to avoid accidental usage. * Move some additional dev settings to the dedicated compose file
1 parent 24407ee commit 5eefa4d

File tree

9 files changed

+115
-29
lines changed

9 files changed

+115
-29
lines changed

.env

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
DOCKER_PYTHONPATH=/app
1+
# DEV
2+
# Override USE_LOCAL_DEV to "true" to automatically mount your
3+
# source code to the containers when using `scripts/up.sh`.
4+
USE_LOCAL_DEV=
25

36
# REST API
47
AIOD_REST_PORT=8000

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ WORKDIR /app
1010
# warnings.
1111
ENV PATH="${PATH}:/home/apprunner/.local/bin"
1212

13+
ENV PYTHONPATH="/app"
1314

1415
# Install python packages globally, so that it can also be used from cron dockers (running as root)
1516
COPY ./pyproject.toml /app/pyproject.toml

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ mysql> SHOW DATABASES;
106106

107107
Now, you can visit the server from your browser at `localhost:8000/docs`.
108108

109+
109110
### Changing the configuration
110111
You may need to change the configuration locally, for example if you want different ports to be used.
111112
Do not change files, instead add overrides.
@@ -119,6 +120,7 @@ AIOD_LOGSTASH_PORT=5001
119120
Then also specify this when you invoke docker compose, e.g.:
120121
`docker compose --env-file=.env --env-file=override.env up`
121122
Note that **order is important**, later environment files will override earlier ones.
123+
You may also use the `./scripts/up.sh` script to achieve this (see ["Shorthands"](#shorthands) below).
122124

123125
#### Config.toml
124126
The main application supports configuration options through a `toml` file.
@@ -134,8 +136,19 @@ docker compose --profile examples --profile huggingface-datasets --profile openm
134136
docker compose --profile examples --profile huggingface-datasets --profile openml --profile zenodo-datasets down
135137
```
136138

137-
Make sure you use the same profile for `up` and `down`, otherwise some containers might keep
138-
running.
139+
Make sure you use the same profile for `up` and `down`, or use `./scripts/down.sh` (see below),
140+
otherwise some containers might keep running.
141+
142+
### Shorthands
143+
We provide two auxiliary scripts for launching docker containers and bringing them down.
144+
The first, `./scripts/up.sh` invokes `docker compose up -d` and takes any number of profiles to launch as parameters.
145+
It will also ensure that the changes of the configurations (see above) are observed.
146+
If `USE_LOCAL_DEV` is set to `true` (e.g., in `override.env`) then your local source code will be mounted on the containers,
147+
this is useful for local development but should not be used in production.
148+
E.g., with `USE_LOCAL_DEV` set to `true`, `./scripts/up.sh` resolves to:
149+
`docker compose --env-file=.env --env-file=override.env -f docker-compose.yaml -f docker-compose.dev.yaml --profile examples up -d`
150+
151+
The second script is a convenience for bringing down all services, including all profiles: `./scripts/down.sh`
139152

140153
#### Local Installation
141154

docker-compose.dev.yaml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
app:
3+
volumes:
4+
- ./src:/app:ro
5+
command: >
6+
python main.py
7+
--build-db if-absent
8+
--reload
9+
10+
fill-db-with-examples:
11+
volumes:
12+
- ./src:/app:ro
13+
- ./connectors:/opt/connectors/script:ro
14+
15+
deletion:
16+
volumes:
17+
- ./src:/app:ro
18+
19+
huggingface-dataset-connector:
20+
volumes:
21+
- ./src:/app:ro
22+
- ./connectors/huggingface/:/opt/connectors/script:ro
23+
24+
keycloak:
25+
command: >
26+
start
27+
--hostname-url http://${HOSTNAME}/aiod-auth
28+
--hostname-admin-url http://${HOSTNAME}/aiod-auth
29+
--http-relative-path=/aiod-auth
30+
--http-enabled=true
31+
--hostname-strict-https=false
32+
--import-realm
33+
34+
openml-connector:
35+
volumes:
36+
- ./src:/app:ro
37+
- ./connectors/openml/:/opt/connectors/script:ro
38+
39+
zenodo-dataset-connector:
40+
volumes:
41+
- ./src:/app:ro
42+
- ./connectors/zenodo/:/opt/connectors/script:ro
43+
44+
es_logstash_setup:
45+
volumes:
46+
- ./src:/app:ro

docker-compose.yaml

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
2-
version: '3.9'
3-
41
services:
52
app:
63
build:
74
context: ./
85
dockerfile: Dockerfile
96
image: aiod_metadata_catalogue
107
container_name: apiserver
8+
volumes:
9+
- ./src/config.override.toml:/app/config.override.toml:ro
1110
environment:
1211
- KEYCLOAK_CLIENT_SECRET=$KEYCLOAK_CLIENT_SECRET
1312
- ES_USER=$ES_USER
1413
- ES_PASSWORD=$ES_PASSWORD
1514
ports:
1615
- ${AIOD_REST_PORT}:8000
17-
volumes:
18-
- ./src:/app:ro
1916
command: >
2017
python main.py
2118
--build-db if-absent
22-
--reload
2319
healthcheck:
2420
test: ["CMD", "python", "-c", "import requests; requests.get('http://localhost:8000')"]
2521
start_interval: 1s
@@ -38,11 +34,9 @@ services:
3834
environment:
3935
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
4036
- KEYCLOAK_CLIENT_SECRET=$KEYCLOAK_CLIENT_SECRET
41-
- PYTHONPATH=$DOCKER_PYTHONPATH
4237
volumes:
43-
- ./src:/app:ro
4438
- ${DATA_PATH}/connectors:/opt/connectors/data
45-
- ./connectors:/opt/connectors/script:ro
39+
- ./src/config.override.toml:/app/config.override.toml:ro
4640
command: >
4741
/bin/bash -c "/opt/connectors/script/fill-examples.sh"
4842
depends_on:
@@ -56,8 +50,8 @@ services:
5650
image: aiod_deletion
5751
container_name: deletion
5852
volumes:
59-
- ./src:/app
6053
- ${DATA_PATH}/deletion:/opt/deletion/data
54+
- ./src/config.override.toml:/app/config.override.toml:ro
6155
command: >
6256
/bin/bash -c "/opt/deletion/script/entry.sh"
6357
depends_on:
@@ -70,11 +64,9 @@ services:
7064
container_name: huggingface-dataset-connector
7165
environment:
7266
- KEYCLOAK_CLIENT_SECRET=$KEYCLOAK_CLIENT_SECRET
73-
- PYTHONPATH=$DOCKER_PYTHONPATH
7467
volumes:
75-
- ./src:/app:ro
7668
- ${DATA_PATH}/connectors:/opt/connectors/data
77-
- ./connectors/huggingface/:/opt/connectors/script:ro
69+
- ./src/config.override.toml:/app/config.override.toml:ro
7870
command: >
7971
/bin/bash -c "/opt/connectors/script/datasets.sh"
8072
depends_on:
@@ -89,12 +81,10 @@ services:
8981
image: aiod_openml_connector
9082
container_name: openml-connector
9183
environment:
92-
- PYTHONPATH=$DOCKER_PYTHONPATH
9384
- KEYCLOAK_CLIENT_SECRET=$KEYCLOAK_CLIENT_SECRET
9485
volumes:
95-
- ./src:/app:ro
9686
- ${DATA_PATH}/connectors:/opt/connectors/data
97-
- ./connectors/openml/:/opt/connectors/script:ro
87+
- ./src/config.override.toml:/app/config.override.toml:ro
9888
command: >
9989
/bin/bash -c "/opt/connectors/script/entry.sh"
10090
depends_on:
@@ -109,12 +99,10 @@ services:
10999
image: aiod_zenodo_connector
110100
container_name: zenodo-dataset-connector
111101
environment:
112-
- PYTHONPATH=$DOCKER_PYTHONPATH
113102
- KEYCLOAK_CLIENT_SECRET=$KEYCLOAK_CLIENT_SECRET
114103
volumes:
115-
- ./src:/app
116104
- ${DATA_PATH}/connectors:/opt/connectors/data
117-
- ./connectors/zenodo/:/opt/connectors/script
105+
- ./src/config.override.toml:/app/config.override.toml:ro
118106
command: >
119107
/bin/bash -c "/opt/connectors/script/entry.sh"
120108
depends_on:
@@ -149,11 +137,9 @@ services:
149137
- ${DATA_PATH}/keycloak/themes:/opt/keycloak/themes
150138
command: >
151139
start
152-
--hostname-url http://${HOSTNAME}/aiod-auth
153-
--hostname-admin-url http://${HOSTNAME}/aiod-auth
140+
--proxy edge
141+
--hostname ${HOSTNAME}
154142
--http-relative-path=/aiod-auth
155-
--http-enabled=true
156-
--hostname-strict-https=false
157143
--import-realm
158144
159145
nginx:
@@ -198,9 +184,8 @@ services:
198184
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
199185
- ES_USER=$ES_USER
200186
- ES_PASSWORD=$ES_PASSWORD
201-
- PYTHONPATH=$DOCKER_PYTHONPATH
202187
volumes:
203-
- ./src:/app
188+
- ./src/config.override.toml:/app/config.override.toml:ro
204189
- ./logstash:/logstash
205190
command: >
206191
/bin/bash -c "python setup/logstash_setup/generate_logstash_config_files.py &&

scripts/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ Unless noted otherwise, all scripts should be run without arguments while the AI
55

66
## Script Descriptions
77

8+
## `up.sh`
9+
Shorthand for `docker compose up -d` which takes as arguments any profile names you also want to up.
10+
Will automatically specify to use both `.env` and `override.env` and also mount local source code if `AIOD_MOUNT` is set.
11+
12+
## `down.sh`
13+
Shorthand to bring down all docker containers regardless of profile.
14+
815
### `clean.sh`
916

1017
> [!WARNING]

scripts/down.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
command='docker compose --profile "*" down'
4+
echo "${command}"
5+
eval "${command}"

scripts/up.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
profiles=""
4+
for arg in "$@"; do
5+
profiles+="--profile $arg "
6+
done
7+
8+
NC='\033[0m' # No Color
9+
CYAN='\033[1;36m'
10+
GREEN='\033[0;32m'
11+
12+
source .env
13+
source override.env
14+
15+
if [[ "${USE_LOCAL_DEV}" == "true" ]]; then
16+
compose_with_dev="-f docker-compose.dev.yaml"
17+
echo -e "Launching ${CYAN}with${NC} local changes."
18+
else
19+
compose_with_dev=""
20+
echo -e "Launching ${GREEN}without${NC} local changes."
21+
fi
22+
23+
command="docker compose --env-file=.env --env-file=override.env -f docker-compose.yaml ${compose_with_dev} ${profiles} up -d"
24+
echo "${command}"
25+
eval "${command}"
26+

src/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
DEFAULT_CONFIG = tomllib.load(fh)
88

99
OVERRIDE_CONFIG_PATH = pathlib.Path(__file__).parent / "config.override.toml"
10-
if OVERRIDE_CONFIG_PATH.exists():
10+
if OVERRIDE_CONFIG_PATH.exists() and OVERRIDE_CONFIG_PATH.is_file():
1111
with open(OVERRIDE_CONFIG_PATH, "rb") as fh:
1212
OVERRIDE_CONFIG = tomllib.load(fh)
1313
else:

0 commit comments

Comments
 (0)