From 990020d2960b024d695cf4311347fb8f507f14b8 Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Wed, 4 Dec 2024 14:28:43 -0600 Subject: [PATCH] updates --- README.md | 2 + docker-compose.prod.yml | 38 +++++++++++++++++++ services/client/Dockerfile.prod | 48 ++++++++++++++++++++++++ services/client/conf/conf.d/default.conf | 12 ++++++ services/users/Dockerfile.prod | 25 ++++++++++++ services/users/src/db/create.sql | 1 + 6 files changed, 126 insertions(+) create mode 100644 docker-compose.prod.yml create mode 100644 services/client/Dockerfile.prod create mode 100644 services/client/conf/conf.d/default.conf create mode 100644 services/users/Dockerfile.prod diff --git a/README.md b/README.md index 989e29b..c358d37 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # Deploying a Flask and React Microservice to AWS ECS + +https://testdriven.io/courses/aws-flask-react/ diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..3549e19 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,38 @@ +services: + + api: + build: + context: ./services/users + dockerfile: Dockerfile.prod + ports: + - 5004:5000 + environment: + - FLASK_ENV=production + - APP_SETTINGS=src.config.ProductionConfig + - DATABASE_URL=postgres://postgres:postgres@api-db:5432/api_prod + - DATABASE_TEST_URL=postgres://postgres:postgres@api-db:5432/api_test + - SECRET_KEY=my_precious + depends_on: + - api-db + + api-db: + build: + context: ./services/users/src/db + dockerfile: Dockerfile + expose: + - 5432 + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + + client: + build: + context: ./services/client + dockerfile: Dockerfile.prod + args: + - NODE_ENV=production + - VITE_API_SERVICE_URL=${VITE_API_SERVICE_URL} + ports: + - 3007:80 + depends_on: + - api diff --git a/services/client/Dockerfile.prod b/services/client/Dockerfile.prod new file mode 100644 index 0000000..56f5f1e --- /dev/null +++ b/services/client/Dockerfile.prod @@ -0,0 +1,48 @@ +########### +# BUILDER # +########### + +# pull official base image +FROM node:20.16.0 AS builder + +# set working directory +WORKDIR /usr/src/app + +# add `/usr/src/app/node_modules/.bin` to $PATH +ENV PATH /usr/src/app/node_modules/.bin:$PATH + +# install and cache app dependencies +COPY package.json . +COPY package-lock.json . +RUN npm ci + +# set environment variables +ARG VITE_API_SERVICE_URL +ENV VITE_API_SERVICE_URL=$VITE_API_SERVICE_URL +ARG NODE_ENV +ENV NODE_ENV=$NODE_ENV + +# create build +COPY . . +RUN vite build + + +######### +# FINAL # +######### + +# base image +FROM nginx:stable-alpine + +# update nginx conf +RUN rm -rf /etc/nginx/conf.d +COPY conf /etc/nginx + +# copy static files +COPY --from=builder /usr/src/app/dist /usr/share/nginx/html + +# expose port +EXPOSE 80 + +# run nginx +CMD ["nginx", "-g", "daemon off;"] diff --git a/services/client/conf/conf.d/default.conf b/services/client/conf/conf.d/default.conf new file mode 100644 index 0000000..93cba3d --- /dev/null +++ b/services/client/conf/conf.d/default.conf @@ -0,0 +1,12 @@ +server { + listen 80; + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} diff --git a/services/users/Dockerfile.prod b/services/users/Dockerfile.prod new file mode 100644 index 0000000..174d303 --- /dev/null +++ b/services/users/Dockerfile.prod @@ -0,0 +1,25 @@ +# pull official base image +FROM python:3.12.0-slim-bookworm + +# set working directory +WORKDIR /usr/src/app + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# install system dependencies +RUN apt-get update \ + && apt-get -y install netcat-traditional \ + && apt-get clean + +# install dependencies +RUN pip install --upgrade pip +COPY ./requirements.txt . +RUN pip install -r requirements.txt + +# add app +COPY . . + +# run server +CMD gunicorn -b 0.0.0.0:5000 manage:app diff --git a/services/users/src/db/create.sql b/services/users/src/db/create.sql index f9fda23..ee4351d 100644 --- a/services/users/src/db/create.sql +++ b/services/users/src/db/create.sql @@ -1,2 +1,3 @@ +CREATE DATABASE api_prod; CREATE DATABASE api_dev; CREATE DATABASE api_test;