From 1641912e5546f7f082c0915dbc336225485e9af2 Mon Sep 17 00:00:00 2001 From: wop Date: Thu, 26 Sep 2024 02:47:42 +0200 Subject: [PATCH] init --- Dockerfile | 28 ++++++++++++++++++++++++ README.md | 38 ++++++++++++++++++++++++++++++++ docker-compose.yml | 54 ++++++++++++++++++++++++++++++++++++++++++++++ main.py | 7 ++++++ requirements.txt | 2 ++ 5 files changed, 129 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 main.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..58610e7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# Base Image für den Container - entsprechend der genutzten Python Version anpassen +FROM python:3.10.13-bullseye + +# Verhindert das Kopieren von pvc Dateien - relevant? +ENV PYTHONDONTWRITEBYTECODE 1 + +# Sorft dafür, dass Python-Output zum Terminal geschickt wird +ENV PYTHONUNBUFFERED 1 + +# Updates und Installation der nötigen Software - entsprechend anpassen +RUN apt-get update \ + && apt-get install -y build-essential \ + && apt-get install -y libpq-dev \ + && apt-get install -y gettext \ + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ + && rm -rf /var/lib/apt/lists/* + +# Kopiervorgang Host > Container +COPY ./requirements.txt /requirements.txt + +# Installation der Abhängigkeiten +RUN pip install -r /requirements.txt --no-cache-dir + +# Dir, von der die Befehle abgesetzt werden +WORKDIR /app + +# Uvicorn Befehl, um die App zu starten +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..f045616 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Andi's App - Irgendwas mit K + +# Commands + +`alias pps="podman container ps -a"` +`alias pre="podman-compose down && podman-compose up -d"` + +`podman-compose up` +`podman-compose down` + +# Dockerfile + +[Dockerfile Reference](https://docs.docker.com/reference/dockerfile/) +[Compose File Reference](https://docs.docker.com/reference/compose-file/) +[Podman 'Basic' Networking](https://github.com/containers/podman/blob/main/docs/tutorials/basic_networking.md) +[Postgres Dockerhub](https://hub.docker.com/_/postgres/tags?page=3) + +### Source Dockerfile + App + +https://dev.to/mbuthi/devops-with-fast-api-postgresql-how-to-containerize-fast-api-application-with-docker-1jdb + +### Unterschiede RUN CMD ENTRYPOINT + +https://stackoverflow.com/questions/37461868/difference-between-run-and-cmd-in-a-dockerfile + +### Beschreibung WORKDIR + +https://stackoverflow.com/a/51066379 + +## POSTGRES + +### Restore/Backup + +https://stackoverflow.com/a/29913462 + +# Reference + +[Explainshell](https://github.com/idank/explainshell) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..320686b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,54 @@ +# specify the compose version +version: '3.7' + +# Specify the services for our docker compose setup +services: + + api: + build: + # path to the directory containing the Dockerfile + context: . + + # Specify the image name + image: products_api + + container_name: andi-app + # this volume is used to map the files and folders on the host to the container + # so if we change code on the host, code in the docker container will also be changed + volumes: + - .:/app + + # Map port 8000 on the host to port 8000 on the container + ports: + - 8000:8000 + + # Specify the .env file path + env_file: + - ./.env + + # Define a dependency on the "products_db" service, so it starts first + depends_on: + - products_db + + products_db: + + # specify the image name of our database + # If the image is not found in our local repository + # It will be pulled from docker registry that is Docker Hub + image: docker.io/postgres:latest + container_name: andi-db + + # Mount a volume to persist postgreSQL data + volumes: + - postgres_data:/var/lib/postgresql/data/ + environment: # Use environment variables for db configuration + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DATABASE=${POSTGRES_DATABASE} + +# Define a volume for persisting postgreSQL data +volumes: + postgres_data: + +# Source +# https://dev.to/mbuthi/devops-with-fast-api-postgresql-how-to-containerize-fast-api-application-with-docker-1jdb diff --git a/main.py b/main.py new file mode 100644 index 0000000..f5961e6 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"Hello": "World"} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..97dc7cd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn