Skip to content

Commit

Permalink
Merge pull request #16 from wednesday-solutions/feature/containerization
Browse files Browse the repository at this point in the history
Fix: Added Containerization and test coverage
  • Loading branch information
anasnadeemws authored Mar 11, 2024
2 parents 65cc5aa + 247ab09 commit 7505606
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 14 deletions.
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ venv

# Config file
.env
env.docker
env.local

*__pycache__*

# Temp files
tmp/
temp/
temp/
47 changes: 35 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
# Use a base image with Python 3 already installed
FROM python:3.9.13

# Set the working directory
WORKDIR /usr/src/app
FROM python:3.12.2-slim as base

# Copy only the requirements file first
COPY requirements.txt ./
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Install dependencies
RUN pip3 install --upgrade pip && pip3 install --no-cache-dir -r requirements.txt
# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

# Copy the rest of the application code
WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
nonroot

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER nonroot

# Copy the source code into the container.
COPY . .

# Expose the port
# Expose the port that the application listens on.
EXPOSE 8000

# Run your application
CMD ["sh", "-c", "alembic upgrade head && uvicorn app:app --host 0.0.0.0 --port 8000"]
# Run the application.
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.app:app --host 0.0.0.0 --port 8000"]
22 changes: 22 additions & 0 deletions README.Docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Building and running your application

When you're ready, start your application by running:
`docker compose up --build`.

Your application will be available at http://localhost:8000.

### Deploying your application to the cloud

First, build your image, e.g.: `docker build -t myapp .`.
If your cloud uses a different CPU architecture than your development
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
you'll want to build the image for that platform, e.g.:
`docker build --platform=linux/amd64 -t myapp .`.

Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.

Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
docs for more detail on building and pushing.

### References
* [Docker's Python guide](https://docs.docker.com/language/python/)
57 changes: 57 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.8'

services:
db:
image: mysql:latest
restart: always
env_file:
- .env.docker
environment:
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "echo 'SELECT 1' | mysql -hlocalhost -uroot -p${DB_ROOT_PASSWORD}"]
interval: 10s
timeout: 5s
retries: 3
depends_on:
- redis
networks:
- fast-api-network

redis:
image: redis:6-alpine
restart: always
networks:
- fast-api-network

wait-for-db:
image: atkrad/wait4x
depends_on:
db:
condition: service_healthy
command: tcp db:${DB_PORT} -t 30s -i 250ms
networks:
- fast-api-network

app:
build: .
restart: always
container_name: fast-api-app
env_file:
- ./.env.${ENVIRONMENT_NAME}
depends_on:
wait-for-db:
condition: service_completed_successfully
ports:
- "${SERVER_PORT}:${SERVER_PORT}"
environment:
ENVIRONMENT_NAME: "${ENVIRONMENT_NAME}"
networks:
- fast-api-network

networks:
fast-api-network:
driver: bridge
3 changes: 2 additions & 1 deletion scripts/run_tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
python -m pytest tests
coverage run -m pytest
coverage report

0 comments on commit 7505606

Please sign in to comment.