Skip to content

Commit

Permalink
feature: Enabling containerized development
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarath P S committed Sep 24, 2021
1 parent 031b494 commit 962817b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 7 deletions.
5 changes: 0 additions & 5 deletions Containerfile

This file was deleted.

36 changes: 36 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# Base Image for builder
FROM python:3.9 as builder

# Install Requirements
COPY requirements.txt /
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txt


# Build the app image
FROM python:3.9

# Create directory for the app user
RUN mkdir -p /home/app

# Create the app user
RUN groupadd app && useradd -g app app

# Create the home directory
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# Install Requirements
COPY --from=builder /wheels /wheels
COPY --from=builder requirements.txt .
RUN pip install --no-cache /wheels/*

COPY . $APP_HOME

RUN chown -R app:app $APP_HOME

USER app

CMD ["uvicorn", "project_name.app:app", "--host=0.0.0.0","--port=8000","--reload"]
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ shell: ## Open a shell in the project.
@if [ "$(USING_POETRY)" ]; then poetry shell; exit; fi
@./.venv/bin/ipython -c "from project_name import *"

.PHONY: docker-build
docker-build: ## Builder docker images
@docker-compose -f docker-compose-dev.yaml -p project_name build

.PHONY: docker-run
docker-run: ## Run docker development images
@docker-compose -f docker-compose-dev.yaml -p project_name up -d

.PHONY: docker-stop
docker-stop: ## Bring down docker dev environment
@docker-compose -f docker-compose-dev.yaml -p project_name down

.PHONY: docker-ps
docker-ps: ## Bring down docker dev environment
@docker-compose -f docker-compose-dev.yaml -p project_name ps

.PHONY: docker-log
docker-logs: ## Bring down docker dev environment
@docker-compose -f docker-compose-dev.yaml -p project_name logs -f app

# This project has been generated from rochacbruno/fastapi-project-template
# __author__ = 'rochacbruno'
# __repo__ = https://github.com/rochacbruno/fastapi-project-template
Expand Down
26 changes: 26 additions & 0 deletions docker-compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.9'

services:
app:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/project_name
volumes:
- .:/home/app/web
depends_on:
- db
db:
build: postgres
image: project_name_postgres-13-alpine-multi-user
volumes:
- $HOME/.postgres/project_name_db/data/postgresql:/var/lib/postgresql/data
ports:
- 5435:5432
environment:
- POSTGRES_DBS=project_name, project_name_test
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
2 changes: 2 additions & 0 deletions postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM postgres:alpine3.14
COPY create-databases.sh /docker-entrypoint-initdb.d/
23 changes: 23 additions & 0 deletions postgres/create-databases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -e
set -u

function create_user_and_database() {
local database=$1
echo "Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database PASSWORD '$database';
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_DBS" ]; then
echo "Creating DB(s): $POSTGRES_DBS"
for db in $(echo $POSTGRES_DBS | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi
4 changes: 2 additions & 2 deletions project_name/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def read(*paths, **kwargs):
CORSMiddleware,
allow_origins=settings.server.cors_origins,
allow_credentials=settings.get("server.cors_allow_credentials", True),
allow_methods=settings.get("server.cors_allow_methods", ["*"]),
allow_headers=settings.get("server.cors_allow_headers", ["*"]),
allow_methods=settings.get("server.cors_allow_methods", ["*"]),
allow_headers=settings.get("server.cors_allow_headers", ["*"]),
)

app.include_router(main_router)
Expand Down

0 comments on commit 962817b

Please sign in to comment.