Skip to content

Commit

Permalink
Config (Traefik): Configure a Traefik reverse proxy Docker container …
Browse files Browse the repository at this point in the history
…to use muscle.local and backend.muscle.local domains (#717)

* config(traefik): Configure traefik and the corresponding host names (muscle.local & backend.muscle.local)

* config: Add (idempotent) script to add host entries (muscle.local & backend.muscle.local) to the hosts file

* chore: Remove unused network configuration in docker-compose.yaml

* chore: Rename add hosts entries file

* docs(traefik): Document the Traefik configuration for the frontend and backend the docker compose configuration file

* docker(traefik): Update traefik image version to always use latest
  • Loading branch information
drikusroor authored Jan 23, 2024
1 parent 7981e54 commit a9111d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ DJANGO_SUPERUSER_USERNAME=admin # do not use in production!
DJANGO_SUPERUSER_PASSWORD=admin # do not use in production!
DJANGO_SUPERUSER_EMAIL=mail@example.com # do not use in production!
AML_LOCATION_PROVIDER=http://ip2country:5000/{} # address of the ip2country container, don't change
ALLOWED_HOSTS=localhost # needs to be changed when running in production
AML_ALLOWED_HOSTS="backend.muscle.local" # needs to be changed when running in production

REACT_APP_API_ROOT=http://localhost:8000 # address of the server, don't change
REACT_APP_API_ROOT=http://backend.muscle.local # address of the server, don't change
REACT_APP_EXPERIMENT_SLUG=gmsi # experiment slug that the frontend redirects to
REACT_APP_AML_HOME=https://www.amsterdammusiclab.nl # website you will be redirected to if you do not agree with an informed consent form
REACT_APP_LOGO_URL= # optional: link to logo
Expand Down
27 changes: 27 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ services:
ports:
- 8854:5000
server:
labels:
# Enables Traefik for the Django backend service,
# routes HTTP requests with the host 'backend.muscle.local' to this service,
# and sets 'web' as the (http, insecure) network entry point for these requests.
- "traefik.enable=true"
- "traefik.http.routers.server.rule=Host(`backend.muscle.local`)"
- "traefik.http.routers.server.entrypoints=web"
build:
context: ./backend
dockerfile: DockerfileDevelop
Expand All @@ -40,6 +47,7 @@ services:
source: ./backend
target: /server
environment:
- AML_ALLOWED_HOSTS=${AML_ALLOWED_HOSTS}
- AML_DEBUG=${AML_DEBUG}
- DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}
- DJANGO_SUPERUSER_USERNAME=${DJANGO_SUPERUSER_USERNAME}
Expand All @@ -55,6 +63,13 @@ services:
- 8000:8000
command: bash -c "python manage.py migrate && python manage.py bootstrap && python manage.py runserver 0.0.0.0:8000"
client:
labels:
# Enables Traefik for the React frontend service,
# routes HTTP requests with the host 'muscle.local' to this service,
# and sets 'web' as the (http, insecure) network entry point for these requests.
- "traefik.enable=true"
- "traefik.http.routers.client.rule=Host(`muscle.local`)"
- "traefik.http.routers.client.entrypoints=web"
build:
context: ./frontend
dockerfile: DockerfileDevelop
Expand Down Expand Up @@ -85,6 +100,18 @@ services:
ports:
- 3000:3000
command: sh -c "yarn scss && yarn start"
traefik:
image: traefik:latest
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080" # Traefik dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock
volumes:
db_data:
db_backup:
27 changes: 27 additions & 0 deletions scripts/add-hosts-entries
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Define host entries
HOST_ENTRIES=(
"# MUSCLE"
"127.0.0.1 muscle.local"
"127.0.0.1 backend.muscle.local"
"# END MUSCLE"
)

# Path to the hosts file
HOSTS_FILE="/etc/hosts"

# Function to add an entry if it doesn't exist
add_host_entry() {
local entry=$1
if ! grep -q "$entry" "$HOSTS_FILE"; then
echo "$entry" | sudo tee -a "$HOSTS_FILE"
else
echo "Entry already exists: $entry"
fi
}

# Add each host entry
for entry in "${HOST_ENTRIES[@]}"; do
add_host_entry "$entry"
done

0 comments on commit a9111d9

Please sign in to comment.