Skip to content

Commit 9bb909c

Browse files
committed
add script to build/run in supabase cli context
1 parent 69e4a40 commit 9bb909c

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ static/api.json
88
data/
99
bin/
1010
coverage/
11-
.idea/
11+
.idea/
12+
.docker/.debug

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ curl --location --request GET 'http://localhost:5000/bucket' \
6969

7070
To perform your tests you can run the following command: `npm test`
7171

72+
### Running in context
73+
74+
Sometimes it is useful to test changes in the context of the entire stack (e.g. a project running via the Supabase cli). The `replace-existing-container.sh` script builds an image and replace an existing running container with it. All settings of the existing container are preserved (volumes, network, env, etc).
75+
76+
```bash
77+
# Start supabase project - in root of supabase project "PROJECT-NAME"
78+
supabase start
79+
80+
# In root of storage api repo
81+
./src/scripts/replace-existing-container.sh supabase_storage_PROJECT-NAME
82+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
##############################################################################
4+
#
5+
# Replace Existing Container
6+
#
7+
# This script builds a new docker image from the current code,
8+
# and replaces a running supabase storage container with it
9+
# it can be used to test changes in the context of other services (e.g. supabase cli)
10+
#
11+
##############################################################################
12+
13+
set -euo pipefail
14+
15+
if [ "$#" -ne 1 ]; then
16+
echo "Usage: $0 <replace_container_name>"
17+
exit 1
18+
fi
19+
20+
REPLACE_CONTAINER_NAME="$1"
21+
NEW_IMAGE_TAG="storage-api:debug-replace-existing"
22+
23+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24+
TARGET_DIR="$SCRIPT_DIR/../../.docker/.debug"
25+
DOCKER_COMPOSE_PATH="$TARGET_DIR/$REPLACE_CONTAINER_NAME.yml"
26+
27+
# check if specified container exists / is running
28+
if ! docker ps --format '{{.Names}}' | grep -q "^${REPLACE_CONTAINER_NAME}$"; then
29+
echo "Error: Docker container '$REPLACE_CONTAINER_NAME' is not running."
30+
exit 1
31+
fi
32+
33+
# ensure target docker-compose directory exists
34+
mkdir -p "$TARGET_DIR"
35+
36+
# build storage api
37+
echo " "
38+
echo "[1/5] Building \"$NEW_IMAGE_TAG\" image from current storage code..."
39+
echo " "
40+
docker build -t "$NEW_IMAGE_TAG" .
41+
42+
# create docker compose file based on the running image
43+
echo " "
44+
echo "[2/5] Generating docker-compose file from existing container..."
45+
echo " "
46+
docker pull ghcr.io/red5d/docker-autocompose:latest
47+
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose "$REPLACE_CONTAINER_NAME" > "$DOCKER_COMPOSE_PATH"
48+
49+
# update docker-compose to use newly built image
50+
OLD_IMAGE=$(grep 'image:' "$DOCKER_COMPOSE_PATH" | awk '{print $2}' | head -n 1)
51+
echo " "
52+
echo "[3/5] Replacing image $OLD_IMAGE with \"$NEW_IMAGE_TAG\"..."
53+
echo " "
54+
sed -i.bak "s|image: $OLD_IMAGE|image: $NEW_IMAGE_TAG|" "$DOCKER_COMPOSE_PATH"
55+
56+
# stop and remove storage container
57+
echo " "
58+
echo "[4/5] Stopping existing \"$REPLACE_CONTAINER_NAME\" container..."
59+
echo " "
60+
docker rm -f "$REPLACE_CONTAINER_NAME"
61+
62+
# start new container to replace existing
63+
echo " "
64+
echo "[5/5] Starting new container..."
65+
echo " "
66+
docker-compose -f "$DOCKER_COMPOSE_PATH" up -d
67+
68+
echo " "
69+
echo "✅ New \"$REPLACE_CONTAINER_NAME\" container is running"
70+
echo " "
71+
echo "You can update the container by running this script again"
72+
echo " "
73+
echo "To watch logs run: docker logs -f $REPLACE_CONTAINER_NAME"
74+
echo " "

0 commit comments

Comments
 (0)