From c595ec56eb295688c85964c6a9f5e07bfc081b44 Mon Sep 17 00:00:00 2001
From: Rich Wareham <rjw57@cam.ac.uk>
Date: Mon, 7 Jan 2019 10:43:46 +0000
Subject: [PATCH] docker-compose.yml: rework tox

Re-work the tox deployment so that we mount the application directory as
a read-only volume rather than copying the file in when building the
image. This has the following advantages:

1. We do not build a new image each time we run tests. (This is nicer to
   those of us with finite disk space.)
2. Running tests is slightly faster since a new image is not being
   built.
3. The application directory is mounted read-only so we catch code which
   accidentally writes to the current directory.
4. We can use the upstream tox image rather than rolling our own derived
   image.

In order to make to happy to run in a read-only environment, we specify
that a) it does not run sdist to write an .egg-info directory and b)
that coverage data is stored in /tmp rather than the application
directory.
---
 Dockerfile         | 3 ---
 README.md          | 2 +-
 README.rst         | 2 +-
 docker-compose.yml | 7 ++++---
 tox.ini            | 7 +++++++
 5 files changed, 13 insertions(+), 8 deletions(-)
 delete mode 100644 Dockerfile

diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index c7c4f6f..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,3 +0,0 @@
-FROM themattrix/tox-base
-ADD . .
-RUN tox
diff --git a/README.md b/README.md
index eedcadf..442d0ad 100644
--- a/README.md
+++ b/README.md
@@ -159,5 +159,5 @@ Tox is configured to run on a container with a matrix execution of different ver
 It will also show the coverage and any possible PEP8 violations.
 
 ```shell
-$ docker-compose up --build tox
+$ docker-compose up
 ```
diff --git a/README.rst b/README.rst
index 15ade7b..14985f8 100644
--- a/README.rst
+++ b/README.rst
@@ -199,4 +199,4 @@ It will also show the coverage and any possible PEP8 violations.
 
 .. code:: shell
 
-        $ docker-compose up --build tox
+        $ docker-compose up
diff --git a/docker-compose.yml b/docker-compose.yml
index 78bece8..ad1f790 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -6,14 +6,15 @@
 version: '3.2'
 services:
   tox:
-    build:
-      context: .
-      dockerfile: Dockerfile
+    image: themattrix/tox-base
+    entrypoint: tox
     environment:
       - TOXINI_WORK_DIR=/tmp/tox-data/work
       - TOXINI_ARTEFACT_DIR=/tmp/tox-data/artefacts
+      - TOXINI_COVERAGE_FILE=/tmp/tox-coverage
     volumes:
       - tox-data:/tmp/tox-data
+      - ./:/app:ro
 
 volumes:
   # A persistent volume for tox to store its stuff. This allows caching of
diff --git a/tox.ini b/tox.ini
index a4511d6..e443d5f 100644
--- a/tox.ini
+++ b/tox.ini
@@ -17,6 +17,9 @@ envlist =
     flake8
 # Allow overriding toxworkdir via environment variable
 toxworkdir={env:TOXINI_WORK_DIR:{toxinidir}/.tox}
+# Do not attempt to create .egg-info directories in the application root as it
+# is mounted as a read-only volume.
+skipsdist=true
 
 # The "_vars" section is ignored by tox but we place some useful shared
 # variables in it to avoid needless repetition.
@@ -27,6 +30,10 @@ toxworkdir={env:TOXINI_WORK_DIR:{toxinidir}/.tox}
 build_root={env:TOXINI_ARTEFACT_DIR:{toxinidir}/build}
 
 [testenv]
+setenv=
+#   Override the coverage dtaa file location since the application root is
+#   mounted read-only.
+    COVERAGE_FILE={env:TOXINI_COVERAGE_FILE:{toxinidir}/.coverage}
 # Additional dependencies
 deps=
     coverage
-- 
GitLab