From 1b925f3367497247b8e63d8b26ae7e5d5979ec5d Mon Sep 17 00:00:00 2001
From: Hal Blackburn <hwtb2@cam.ac.uk>
Date: Thu, 27 Feb 2025 15:04:55 +0000
Subject: [PATCH] refactor: make the guidebook working_dir configurable

When the workspace/git checkout is a container volume, it can't be
mounted with a regular bind mount, as the compose config needs to know
the volume name, mount point, and subdirectory within the mount point to
use as the working directory.

This commit allows these 3 options to be configured using environment
variables, so that a devcontainer config can override the guidebook
service mounts to reference the devcontainer's volume. The defaults
continue to use the local bind mount, so using the compose config without
additional configuration will work as before.
---
 Dockerfile          | 10 +++++++++-
 docker-compose.yaml |  5 ++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 441d682e..2a1b1f47 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1.4
+
 FROM plantuml/plantuml-server:jetty AS plantuml
 COPY compose/jetty-logging.properties /var/lib/jetty/resources/jetty-logging.properties
 ENV JAVA_OPTIONS=-Djava.util.logging.config.file=/var/lib/jetty/resources/jetty-logging.properties
@@ -11,10 +13,16 @@ WORKDIR /workspace/
 COPY requirements.txt .
 RUN pip install -r requirements.txt
 
+COPY --chmod=755 <<EOF /docker-entrypoint.sh
+#!/usr/bin/env sh
 # We use git to determine the edit history fo pages to provide a "last updated" timestamp. Work
 # around a git feature which guards against certain types of attacks from untrusted users.
 #
 # More information: https://www.kenmuse.com/blog/avoiding-dubious-ownership-in-dev-containers/
-RUN git config --global --add safe.directory /workspace
+git config --global --add safe.directory .
+
+exec "\$@"
+EOF
+ENTRYPOINT ["/docker-entrypoint.sh"]
 
 CMD ["mkdocs",  "serve", "-a", "0.0.0.0:8000"]
diff --git a/docker-compose.yaml b/docker-compose.yaml
index a7e26458..6dc90832 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -2,9 +2,8 @@ services:
   guidebook:
     build: .
     volumes:
-      - type: bind
-        source: .
-        target: /workspace
+      - ${WORKSPACE_SOURCE:-.}:${WORKSPACE_TARGET:-/workspace}
+    working_dir: ${WORKSPACE_ROOT:-/workspace}
     ports:
       - "8000:8000"
     depends_on:
-- 
GitLab