Skip to content

Commit

Permalink
refactor(dev): update var name and adopt compose v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwang44 committed Apr 18, 2024
1 parent 921ce51 commit 8482a01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 2 additions & 3 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
services:
db:
container_name: db_dev
image: postgres:11-alpine
environment:
- POSTGRES_DB=pycontw2016
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secretpostgres
ports:
- ${COMPOSE_DB_PORT:-5432}:5432
pycontw:
container_name: pycontw_dev

app-dev:
build:
context: .
dockerfile: dev.Dockerfile
Expand Down
20 changes: 11 additions & 9 deletions enter_dev_env.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#!/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
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"

0 comments on commit 8482a01

Please sign in to comment.