From 7341b7d130f5b3c0f0cbfae2ebeefd6714c2972c Mon Sep 17 00:00:00 2001 From: "Wei-Hsiang (Matt) Wang" Date: Thu, 18 Apr 2024 14:52:15 +0000 Subject: [PATCH] refactor(dev): update var name and adopt compose v2 --- docker-compose-dev.yml | 5 ++--- enter_dev_env.sh | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index bfe757bee..7f8b2bc18 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -1,6 +1,5 @@ services: db: - container_name: db_dev image: postgres:11-alpine environment: - POSTGRES_DB=pycontw2016 @@ -8,8 +7,8 @@ services: - POSTGRES_PASSWORD=secretpostgres ports: - ${COMPOSE_DB_PORT:-5432}:5432 - pycontw: - container_name: pycontw_dev + + app-dev: build: context: . dockerfile: dev.Dockerfile diff --git a/enter_dev_env.sh b/enter_dev_env.sh index c13f6c6f5..63bac989e 100755 --- a/enter_dev_env.sh +++ b/enter_dev_env.sh @@ -1,12 +1,14 @@ #!/bin/bash -xe -CONTAINER="pycontw" +CONTAINER="app-dev" +PROJECT_NAME="pycontw_backend_dev" COMPOSE_FILE="./docker-compose-dev.yml" +COMPOSE_CMD="docker compose -f $COMPOSE_FILE -p ${PROJECT_NAME}" # test if the container is running -HASH=`docker ps -q -f name=$CONTAINER` +HASH=`docker ps -q -f name="${PROJECT_NAME}-${CONTAINER}-1"` # test if the container is stopped -HASH_STOPPED=`docker ps -qa -f name=$CONTAINER` +HASH_STOPPED=`docker ps -qa -f name="${PROJECT_NAME}-${CONTAINER}-1"` if [[ $(uname -m) == 'arm64' ]]; then export DOCKER_DEFAULT_PLATFORM=linux/amd64 @@ -14,14 +16,14 @@ fi if [ -n "$HASH" ];then echo "found existing running container $CONTAINER, proceeding to exec another shell" - docker-compose -f $COMPOSE_FILE exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" + $COMPOSE_CMD exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" elif [ -n "$HASH_STOPPED" ];then echo "found existing stopped container $CONTAINER, starting" - (docker-compose -f $COMPOSE_FILE restart && docker start $HASH_STOPPED) >/dev/null 2>&1 - docker-compose -f $COMPOSE_FILE exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" + ($COMPOSE_CMD restart && docker start $HASH_STOPPED) >/dev/null 2>&1 + $COMPOSE_CMD exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" else echo "existing container not found, creating a new one, named $CONTAINER" - docker-compose -f $COMPOSE_FILE up --build --remove-orphans -d - docker-compose -f $COMPOSE_FILE exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" + $COMPOSE_CMD up --build --remove-orphans -d + $COMPOSE_CMD exec -i $CONTAINER bash -c "SHELL=bash source /app/.venv/bin/activate && bash" fi -echo "see you, use 'docker rm $CONTAINER' to kill the dev container or 'docker-compose -f $COMPOSE_FILE down' to kill both the postgres and the dev container if you want a fresh env next time" +echo "see you, use '$COMPOSE_CMD down' to kill both the postgres and the dev container if you want a fresh env next time"