From 88a5322c88d35d525e89b3bd969c4ab3a23adb09 Mon Sep 17 00:00:00 2001
From: Nigel Gott <nigel@baserow.io>
Date: Fri, 8 Oct 2021 15:18:24 +0000
Subject: [PATCH] Resolve "Remove Container Names and allow Media URL to be
 configurable"

---
 .gitattributes                       |  5 +++-
 backend/docker/docker-entrypoint.sh  |  4 ++-
 changelog.md                         |  4 +++
 dev.sh                               | 44 ++++++++++++++++++----------
 docker-compose.yml                   | 12 +-------
 docs/guides/baserow-docker-how-to.md | 18 ++++++++----
 6 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/.gitattributes b/.gitattributes
index 9b1e73302..4713f2d37 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -6,4 +6,7 @@
 # Prettier does not allow windows file endings by default
 *.js text eol=lf
 *.vue text eol=lf
-*.scss text eol=lf
\ No newline at end of file
+*.scss text eol=lf
+
+# Prevent merge conflicts for additions to the change log
+changelog.md merge=union
\ No newline at end of file
diff --git a/backend/docker/docker-entrypoint.sh b/backend/docker/docker-entrypoint.sh
index ea22f93f5..380ff8520 100644
--- a/backend/docker/docker-entrypoint.sh
+++ b/backend/docker/docker-entrypoint.sh
@@ -100,7 +100,9 @@ case "$1" in
         exec python /baserow/backend/src/baserow/manage.py shell
     ;;
     lint)
-        exec make lint-python
+        CMD="make lint-python"
+        echo "$CMD"
+        exec bash --init-file <(echo "history -s $CMD; $CMD")
     ;;
     celery)
         exec celery -A baserow "${@:2}"
diff --git a/changelog.md b/changelog.md
index f90de136d..d7cb87938 100644
--- a/changelog.md
+++ b/changelog.md
@@ -4,6 +4,10 @@
 
 * Fixed a bug where the frontend would fail hard if a table with no views was accessed.
 * Tables can now be opened in new browser tabs.
+* **Breaking Change**: Baserow's `docker-compose.yml` now allows setting the MEDIA_URL 
+  env variable. If using MEDIA_PORT you now need to set MEDIA_URL also.
+* **Breaking Change**: Baserow's `docker-compose.yml` container names have changed to
+  no longer be hardcoded to prevent naming clashes.
 
 ## Released (2021-10-05)
 
diff --git a/dev.sh b/dev.sh
index 2a7c0607a..77863a8d8 100755
--- a/dev.sh
+++ b/dev.sh
@@ -51,6 +51,23 @@ new_tab() {
   fi
 }
 
+launch_tab_and_attach(){
+  tab_name=$1
+  service_name=$2
+  container_name=$(docker inspect -f '{{.Name}}' "$(docker-compose ps -q "$service_name")" | cut -c2-)
+  command="docker logs $container_name && docker attach $container_name"
+  new_tab "$tab_name" "$command"
+}
+
+launch_tab_and_exec(){
+  tab_name=$1
+  service_name=$2
+  exec_command=$3
+  container_name=$(docker inspect -f '{{.Name}}' "$(docker-compose ps -q "$service_name")" | cut -c2-)
+  command="docker exec -it $container_name $exec_command"
+  new_tab "$tab_name" "$command"
+}
+
 show_help() {
     echo """
 ./dev.sh starts the baserow development environment and by default attempts to
@@ -60,6 +77,7 @@ Usage: ./dev.sh [optional start dev commands] [optional docker-compose up comman
 
 The ./dev.sh Commands are:
 restart         : Stop the dev environment first before relaunching.
+restart_wipe    : Stop the dev environment, delete the db and relaunch.
 down            : Down the dev environment and don't up after.
 kill            : Kill the dev environment and don't up after.
 build_only      : Build the dev environment and don't up after.
@@ -244,21 +262,17 @@ docker-compose -f docker-compose.yml -f docker-compose.dev.yml run "$@"
 fi
 
 if [ "$dont_attach" != true ] && [ "$up" = true ] ; then
-  new_tab "Backend" \
-          "docker logs backend && docker attach backend"
 
-  new_tab "Backend celery" \
-          "docker logs celery && docker attach celery"
+  launch_tab_and_attach "backend" "backend"
+  launch_tab_and_attach "web frontend" "web-frontend"
+  launch_tab_and_attach "celery" "celery"
+  launch_tab_and_attach "export worker" "celery-export-worker"
+  launch_tab_and_attach "beat worker" "celery-beat-worker"
 
-  new_tab "Backend celery export worker" \
-          "docker logs celery-export-worker && docker attach celery-export-worker"
-
-  new_tab "Backend celery beat worker" \
-          "docker logs celery-beat-worker && docker attach celery-beat-worker"
-
-  new_tab "Web frontend" \
-          "docker logs web-frontend && docker attach web-frontend"
-
-  new_tab "Web frontend lint" \
-          "docker exec -it web-frontend /bin/bash /baserow/web-frontend/docker/docker-entrypoint.sh lint-fix"
+  launch_tab_and_exec "web frontend lint" \
+          "web-frontend" \
+          "/bin/bash /baserow/web-frontend/docker/docker-entrypoint.sh lint-fix"
+  launch_tab_and_exec "backend lint" \
+          "backend" \
+          "/bin/bash /baserow/backend/docker/docker-entrypoint.sh lint"
 fi
diff --git a/docker-compose.yml b/docker-compose.yml
index aa4c09317..b5b8535d7 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,7 +2,6 @@ version: "3"
 
 services:
   db:
-    container_name: db
     # Please ensure the postgres-client's major version in the backend image is kept in
     # sync with this major version so pg_dump remains compatible.
     image: postgres:11.3
@@ -16,13 +15,11 @@ services:
       - pgdata:/var/lib/postgresql/data
 
   redis:
-    container_name: redis
     image: redis:6.0
     networks:
       local:
 
   mjml:
-    container_name: mjml
     image: liminspace/mjml-tcpserver:0.10
     # mjml is based off the node image which creates a non root node user we can run as
     user: "1000:1000"
@@ -30,7 +27,6 @@ services:
       local:
 
   backend:
-    container_name: backend
     build:
       dockerfile: ./backend/Dockerfile
       context: .
@@ -44,7 +40,7 @@ services:
       - DATABASE_PASSWORD=${DATABASE_PASSWORD:-baserow}
       - DATABASE_NAME=${DATABASE_NAME:-baserow}
       - ADDITIONAL_APPS
-      - MEDIA_URL=http://localhost:${MEDIA_PORT:-4000}/media/
+      - MEDIA_URL=${MEDIA_URL:-http://localhost:4000/media/}
       - EMAIL_SMTP
       - EMAIL_SMTP_HOST
       - EMAIL_SMTP_PORT
@@ -65,7 +61,6 @@ services:
       local:
 
   celery:
-    container_name: celery
     image: baserow_backend:latest
     environment:
       - ADDITIONAL_APPS
@@ -88,7 +83,6 @@ services:
       local:
 
   celery-export-worker:
-    container_name: celery-export-worker
     image: baserow_backend:latest
     build:
       dockerfile: ./backend/Dockerfile
@@ -111,7 +105,6 @@ services:
       local:
 
   celery-beat-worker:
-    container_name: celery-beat-worker
     image: baserow_backend:latest
     build:
       dockerfile: ./backend/Dockerfile
@@ -134,7 +127,6 @@ services:
       local:
 
   web-frontend:
-    container_name: web-frontend
     build:
       context: .
       dockerfile: ./web-frontend/Dockerfile
@@ -152,7 +144,6 @@ services:
 
   # A nginx container purely to serve up django's MEDIA files.
   media:
-    container_name: media
     build: media
     ports:
       - "${HOST_PUBLISH_IP:-127.0.0.1}:${MEDIA_PORT:-4000}:80"
@@ -167,7 +158,6 @@ services:
   # owned by different users. Ensure that we chown them to the user appropriate for the
   # environment here.
   media-volume-fixer:
-    container_name: media-volume-fixer
     image: bash:4.4
     command: chown 9999:9999 -R /baserow/media
     volumes:
diff --git a/docs/guides/baserow-docker-how-to.md b/docs/guides/baserow-docker-how-to.md
index 4bf429d0d..63bd8ca5a 100644
--- a/docs/guides/baserow-docker-how-to.md
+++ b/docs/guides/baserow-docker-how-to.md
@@ -26,18 +26,24 @@ and `media` containers to your machine's network. If you already have applicatio
 services using those ports the Baserow service which uses that port will crash.
 
 To fix this you can change which ports Baserow will use by setting the corresponding
-environment variable:
+environment variables:
 
-- For `backend` set `BACKEND_PORT` which defaults to `8000`
-- For `web-frontend` set `WEB_FRONTEND_PORT` which defaults to `3000`
-- For `media` set `MEDIA_PORT` which defaults to `4000`
+- For `backend` change both:
+  - `BACKEND_PORT`, defaults to `8000` 
+  - `PUBLIC_BACKEND_URL` with the new port, defaults to `http://localhost:8000`
+- For `web-frontend` change both:
+  - `WEB_FRONTEND_PORT`, defaults to `3000`
+  - `PUBLIC_WEB_FRONTEND_URL` with the new port, defaults to `http://localhost:3000`
+- For `media` change both:
+  - `MEDIA_PORT`, defaults to `4000`
+  - `MEDIA_URL`, defaults to `http://localhost:4000/media/`
 
 This is how to set these variables in bash:
 
 ```bash
-$ BACKEND_PORT=8001 docker-compose up 
+$ BACKEND_PORT=8001 PUBLIC_BACKEND_URL=http://localhost:8001 docker-compose up 
 $ # or using dev.sh
-$ BACKEND_PORT=8001 ./dev.sh
+$ BACKEND_PORT=8001 PUBLIC_BACKEND_URL=http://localhost:8001 ./dev.sh
 ```
 
 ### Make Baserow publicly accessible