Skip to content

Commit

Permalink
[infra] - upgrading dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
davi-lucciola committed Aug 20, 2024
1 parent df57f67 commit d2e814f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 78 deletions.
29 changes: 20 additions & 9 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@ jobs:
runs-on: self-hosted

# steps:
# # Running API Container
# - name: Pull Docker Image (API)
# - name: Stoping Containers
# - run: |
# sudo docker stop knockbank-web || true
# sudo docker rm -f knockbank-web || true
# sudo docker image rm -f davilucciola/knockbank-web || true
# sudo docker stop knockbank-api || true
# sudo docker rm -f knockbank-api || true
# sudo docker image rm -f davilucciola/knockbank-api || true

# - name: Pull Docker Images
# run: |
# sudo docker image rm -f davilucciola/knockbank-api
# sudo docker pull davilucciola/knockbank-api:latest

# - name: Delete Old Container (API)
# run: sudo docker rm -f knockbank-api || true

# - name: Run Docker Container (API)
# sudo docker pull davilucciola/knockbank-web:latest

# - name: Running API Container
# env:
# TOKEN_SECRET: ${{ secrets.TOKEN_SECRET }}
# SQLALCHEMY_DATABASE_URI: ${{ secrets.SQLALCHEMY_DATABASE_URI }}
# run: |
# sudo docker run --name knockbank-api -p 5000:5000 --restart=always \
# -e TOKEN_SECRET=$TOKEN_SECRET \
# -e SQLALCHEMY_DATABASE_URI=$SQLALCHEMY_DATABASE_URI \
# -d davilucciola/knockbank-api
# -d davilucciola/knockbank-api

# - name: Running Web Container
# env:
# run: |
# sudo docker run --name knockbank-web -p 3000:3000 --restart=always \
# -d davilucciola/knockbank-web
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ jobs:
# run: docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD

# # API - Docker Image
# - name: Build Docker Image (API)
# - name: Build API Docker Image
# run: |
# cd server
# docker build -t davilucciola/knockbank-api .
# docker build . -t davilucciola/knockbank-api

# - name: Push Docker Image (API)
# - name: Push API Docker Image
# run: docker push davilucciola/knockbank-api:latest

# Web - Docker Image
# - name: Build Web Image
# - name: Build Web Docker Image
# run: |
# cd client
# docker build ./client -t knockbank-web
# docker build . -t davilucciola/knockbank-web

# - name: Push Web Docker Image
# run: docker push davilucciola/knockbank-web:latest
3 changes: 3 additions & 0 deletions client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env.local
.env
node_modules
29 changes: 22 additions & 7 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
FROM node:20-alpine
# Build Stage
FROM node:20-alpine AS builder

WORKDIR /knock-bank-web
WORKDIR /knockbank_web

COPY package.json package-lock.json /knock-bank-web/
COPY package.json package-lock.json ./

RUN apk update && \
npm install
RUN npm install

COPY . /knock-bank-web/
COPY src ./src
COPY public ./public
COPY next.config.mjs tailwind.config.ts postcss.config.mjs tsconfig.json ./

ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL

RUN npm run build

# Runtime Stage
FROM node:20-alpine as runtime

WORKDIR /knockbank_web

COPY --from=builder /knockbank_web/.next ./.next
COPY --from=builder /knockbank_web/public ./public
COPY --from=builder /knockbank_web/node_modules ./node_modules
COPY --from=builder /knockbank_web/package.json ./

EXPOSE 3000

CMD ["npm", "run", "start"]
CMD ["npm", "run", "start"]
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"start": "next start",
"lint": "next lint",
"clean": "rm -rf node_modules .next",
"reinstall": "npm run clean && npm install"
"reinstall": "npm run clean && npm install",
"image-push": "docker build -t davilucciola/knockbank-web . && docker push davilucciola/knockbank-web:latest"
},
"dependencies": {
"@hookform/resolvers": "^3.6.0",
Expand Down
4 changes: 0 additions & 4 deletions deploy/Dockerfile

This file was deleted.

29 changes: 0 additions & 29 deletions deploy/nginx.conf

This file was deleted.

22 changes: 10 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
version: '3.9'


services:
db:
image: mysql:5.7
image: mysql:8.3.0
container_name: knockbank-db
environment:
MYSQL_DATABASE: 'knock_bank_db'
Expand All @@ -15,27 +14,26 @@ services:

api:
container_name: knockbank-api
ports:
- "5000:5000"
build:
context: ./server
environment:
TOKEN_SECRET: 'supersecretkey'
SQLALCHEMY_DATABASE_URI: 'mysql+mysqlconnector://admin:admin@db:3306/knock_bank_db'
depends_on:
- db

web:
container_name: knockbank-web
ports:
- "3000:3000"
build:
context: ./client
args:
NEXT_PUBLIC_API_URL: 'http://localhost:5000/api'
depends_on:
- api

proxy:
container_name: nginx-proxy
restart: always
build:
context: ./deploy
depends_on:
- web
ports:
- "80:80"

volumes:
mysql-db:
3 changes: 2 additions & 1 deletion server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.venv
__pycache__
Dockerfile
users.txt
.env
40 changes: 30 additions & 10 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
FROM python:3.11-alpine
# Build Stage
FROM python:3.11-alpine as builder

WORKDIR /knock-bank-api
RUN apk update && \
pip install --upgrade pip && \
pip install --no-cache-dir poetry

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /knockbank_api

COPY pyproject.toml poetry.lock /knockbank_api/

COPY pyproject.toml poetry.lock /knock-bank-api/
RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --without dev --no-root

# Runtime Stage
FROM python:3.11-alpine as runtime

ENV VIRTUAL_ENV=/knockbank_api/.venv \
PATH="/knockbank_api/.venv/bin:$PATH"

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

RUN apk update && \
pip install --upgrade pip && \
pip install --no-cache-dir poetry && \
poetry config virtualenvs.create false && \
poetry install && \
poetry add gunicorn
pip install gunicorn

WORKDIR /knockbank_api

COPY . /knock-bank-api/
COPY pyproject.toml /knockbank_api
COPY knockbankapi /knockbank_api/knockbankapi/
COPY migrations /knockbank_api/migrations/

EXPOSE 5000

CMD ["gunicorn", "-b", "0.0.0.0:5000", "knockbankapi:create_app()"]
CMD flask --app knockbankapi db upgrade ; gunicorn -b 0.0.0.0:5000 'knockbankapi:create_app()'
4 changes: 4 additions & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ generate:

migrate:
poetry run flask --app knockbankapi db upgrade

image-push:
docker build -t davilucciola/knockbank-api .
docker push davilucciola/knockbank-api:latest

0 comments on commit d2e814f

Please sign in to comment.