Skip to content

Commit

Permalink
feat: migration use of docker compose and env by file
Browse files Browse the repository at this point in the history
  • Loading branch information
rv2931 authored and RV committed Mar 2, 2024
1 parent 0aa29f0 commit 4f8516d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 23 deletions.
17 changes: 13 additions & 4 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# these values are used in the local docker env. You can use "localhost" hostname if you run the application without docker
POSTGRES_HOSTNAME=postgres_bloom
POSTGRES_USER=bloom_user
POSTGRES_PASSWORD=bloom
POSTGRES_DB=bloom_db
# APP_ENV is here to set the runtime environment
# values: dev|test|prod
APP_ENV=test

# VERSION
# is used as bloom:${VERSION}
VERSION=1.0.0


POSTGRES_HOST=postgres_bloom-test
POSTGRES_USER=bloom_user-test
POSTGRES_PASSWORD=bloom-test
POSTGRES_DB=bloom_test
POSTGRES_PORT=5432
SPIRE_TOKEN=
SLACK_URL=
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ celerybeat.pid

# Environments
.env
.env.test
.env.*
.venv
env/
venv/
Expand Down
52 changes: 34 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,45 @@ BLOOM_DEV_DOCKER = @docker run --name bloom-test --mount type=bind,source="$(sh
BLOOM_PRODUCTION_DOCKER = @docker run --mount type=bind,source="$(shell pwd)",target=/source_code --env-file ./.env --log-driver json-file --log-opt max-size=10M --log-opt max-file=3 --entrypoint /entrypoint.sh

build:
@docker build -t d4g/bloom:${VERSION} --platform linux/amd64 -f docker-env/Dockerfile .
@docker tag d4g/bloom:${VERSION} d4g/bloom:latest
@docker compose -f docker-env\docker-compose.yaml build

launch-dev-db:
@docker compose -f docker-env/docker-compose-db.yaml up -d
@sleep 20
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} alembic upgrade head
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} /venv/bin/python3 alembic/init_script/load_vessels_data.py
start-db-dev:
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml up db-dev -d
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml up dbadmin-dev -d

start-app-dev:
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml up app-dev -d

stop-db-dev:
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml down db-dev
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml down dbadmin-dev
stop-app-dev:
@docker compose --env-file .env.dev -f docker-env/docker-compose.yaml down app-dev

connect-app-dev:
@if [ -f $$(pwd)/.env.dev ]; then ln -sf $$(pwd)/.env.dev $$(pwd)/.env; else ( echo "ERROR: No file $$(pwd)/.env.dev"; exit 1) ; fi
@docker compose -f docker-env/docker-compose.yaml exec app-dev bash

load-amp-data:
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} /venv/bin/python3 alembic/init_script/load_amp_data.py
@docker compose -f docker-env/docker-compose.yaml exec app-dev /venv/bin/python3 alembic/init_script/load_amp_data.py

load-test-positions-data:
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} /venv/bin/python3 alembic/init_script/load_positions_data.py
load-positions-data:
@docker compose -f docker-env/docker-compose.yaml exec app-dev /venv/bin/python3 alembic/init_script/load_positions_data.py

launch-dev-container:
$(BLOOM_DEV_DOCKER) -dti d4g/bloom:${VERSION} /bin/bash
launch-app-dev:
@docker compose -f docker-env/docker-compose.yaml exec app-dev /venv/bin/python3 app.py

launch-dev-app:
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} /venv/bin/python3 app.py
start-db-test:
@if [ -f $$(pwd)/.env.test ]; then ln -sf $$(pwd)/.env.test $$(pwd)/.env; else ( echo "ERROR: No file $$(pwd)/.env.test"; exit 1) ; fi
@docker compose -f docker-env/docker-compose.yaml up db -d
@docker compose -f docker-env/docker-compose.yaml up dbadmin -d

launch-test:
$(BLOOM_DEV_DOCKER) --rm d4g/bloom:${VERSION} tox -vv
start-app-test:
@if [ -f $$(pwd)/.env.test ]; then ln -sf $$(pwd)/.env.test $$(pwd)/.env; else ( echo "ERROR: No file $$(pwd)/.env.test"; exit 1) ; fi
@docker compose -f docker-env/docker-compose.yaml up app-test -d
stop-app-test:
@if [ -f $$(pwd)/.env.test ]; then ln -sf $$(pwd)/.env.test $$(pwd)/.env; else ( echo "ERROR: No file $$(pwd)/.env.test"; exit 1) ; fi
@docker compose -f docker-env/docker-compose.yaml down app-test

rm-dev-db:
@docker-compose -f docker-env/docker-compose-db.yaml stop
Expand All @@ -47,7 +63,7 @@ launch-production-app:
$(BLOOM_PRODUCTION_DOCKER) --name bloom-production-app --rm d4g/bloom:${VERSION} /venv/bin/python3 app.py

dump-dev-db:
$(BLOOM_DEV_DOCKER) --rm postgres:latest sh -c 'export PGPASSWORD=$$POSTGRES_PASSWORD && pg_dump -Fc $$POSTGRES_DB -h $$POSTGRES_HOSTNAME -p $$POSTGRES_PORT -U $$POSTGRES_USER> /source_code/bloom_$(shell date +%Y%m%d_%H%M).dump'
$(BLOOM_DEV_DOCKER) --rm db:latest sh -c 'export PGPASSWORD=$$POSTGRES_PASSWORD && pg_dump -Fc $$POSTGRES_DB -h $$POSTGRES_HOSTNAME -p $$POSTGRES_PORT -U $$POSTGRES_USER> /source_code/bloom_$(shell date +%Y%m%d_%H%M).dump'

dump-db:
@docker run --mount type=bind,source="$(shell pwd)",target=/source_code --env-file ./.env.test --network=bloom_net --rm postgres:latest sh -c 'export PGPASSWORD=$$POSTGRES_PASSWORD && pg_dump -Fc $$POSTGRES_DB -h $$POSTGRES_HOSTNAME -p $$POSTGRES_PORT -U $$POSTGRES_USER> /source_code/bloom_$(shell date +%Y%m%d_%H%M).dump'
@docker run --mount type=bind,source="$(shell pwd)",target=/source_code --env-file ./.env.test --network=bloom_net --rm db:latest sh -c 'export PGPASSWORD=$$POSTGRES_PASSWORD && pg_dump -Fc $$POSTGRES_DB -h $$POSTGRES_HOSTNAME -p $$POSTGRES_PORT -U $$POSTGRES_USER> /source_code/bloom_$(shell date +%Y%m%d_%H%M).dump'
81 changes: 81 additions & 0 deletions docker-env/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
version: "3.7"

x-common-infos:
# Env variables stored in a .env file
environment: &common-env
POSTGRES_HOST: ${POSTGRES_HOST:-postgres_bloom}
POSTGRES_PORT: ${POSTGRES_PORT:-5432}
POSTGRES_DB: ${POSTGRES_DB:-bloom_db}
POSTGRES_USER: ${POSTGRES_USER:-boom_user}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-bloom}
services:
db-dev:
container_name: postgres_bloom-${APP_ENV}
image: ${POSTGIS_IMAGE:-postgis/postgis:14-3.3-alpine}
ports:
- ${POSTGRES_PORT:-5432}:5432
env_file: ../.env.dev
environment:
<<: *common-env
healthcheck:
test: ['CMD-SHELL', 'pg_isready --quiet --dbname=${POSTGRES_DB} --username=${POSTGRES_USER}']
interval: 100ms
timeout: 14s
retries: 140
start_period: 0s
networks:
- bloom_net
restart: unless-stopped

app-dev:
container_name: app_bloom-${APP_ENV:-dev}
hostname: app_bloom-${APP_ENV:-dev}
build:
context: ..
dockerfile: docker-env/Dockerfile
args:
CHROME_VERSION: "112.0.5615.165-1"
APP_DIR: /source_code
FROM_IMAGE: ${FROM_IMAGE:-python:3.10-slim-bullseye}
APP_ENV: ${APP_ENV:-dev}
image: dataforgood/bloom:${VERSION:-latest}
env_file:
- ../.env.dev

command: /bin/bash
tty: true
stdin_open: true
environment:
<<: *common-env
SPIRE_TOKEN_FILE: /run/secrets/spire_token
volumes:
- ../data:/source_code/data
- ../alembic:/source_code/alembic
networks:
- bloom_net
restart: unless-stopped
configs:
- source: .env.dev
target: /source_code/.env

dbadmin-dev:
container_name: pgadmin_bloom-${APP_ENV}
image: dpage/pgadmin4
environment:
<<: *common-env
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:[email protected]}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-pgadmin}
env_file:
- ../.env.dev

configs:
.env.dev:
file: ../.env.dev

secrets:
spire_token_dev:
file: ../secrets/dev/spire_token

networks:
bloom_net:
name: bloom_net

0 comments on commit 4f8516d

Please sign in to comment.