From 85b5b1d03d111a57bedf5e18026c88a5e65f536b Mon Sep 17 00:00:00 2001 From: brijsiyag Date: Sat, 9 Jul 2022 23:34:24 +0530 Subject: [PATCH] build(app): dockerize full app for development Fixes part of #116 --- api/{Dockerfile => Dockerfile.dev} | 24 +++++++++++++++--------- api/scripts/populate_db.sh | 1 + docker-compose.yml | 21 +++++++++++++++++++++ ui/Dockerfile.dev | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) rename api/{Dockerfile => Dockerfile.dev} (59%) create mode 100644 api/scripts/populate_db.sh create mode 100644 docker-compose.yml create mode 100644 ui/Dockerfile.dev diff --git a/api/Dockerfile b/api/Dockerfile.dev similarity index 59% rename from api/Dockerfile rename to api/Dockerfile.dev index 87a9f2c..a623e80 100644 --- a/api/Dockerfile +++ b/api/Dockerfile.dev @@ -1,18 +1,18 @@ # base image -FROM python:3.8 +FROM python:3.8 # setup environment variable for work directory -ENV API_HOME=/home/app/webapp +ENV API_HOME=/home/app/api # make work directory -RUN mkdir -p $API_HOME +RUN mkdir -p $API_HOME # set work directory -WORKDIR $API_HOME +WORKDIR $API_HOME # set environment variables ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 +ENV PYTHONUNBUFFERED 1 # copy requirements file COPY requirements.txt $API_HOME @@ -21,13 +21,19 @@ COPY requirements.txt $API_HOME RUN pip install --upgrade pip && pip install -r requirements.txt # copy api directory to docker's work directory. -COPY . $API_HOME +COPY . $API_HOME # make migrations -RUN chmod +x scripts/makemigrations.sh && ./scripts/makemigrations.sh && python manage.py migrate +RUN chmod +x scripts/makemigrations.sh && ./scripts/makemigrations.sh + +# Migrate all migrations +RUN python manage.py migrate + +# Populate Database with dummy data +RUN chmod +x scripts/populate_db.sh && scripts/populate_db.sh # port where the Django app runs -EXPOSE 8000 +EXPOSE 8000 # start server -ENTRYPOINT ["python", "manage.py", "runserver", "0.0.0.0:8000"] +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/api/scripts/populate_db.sh b/api/scripts/populate_db.sh new file mode 100644 index 0000000..70c7111 --- /dev/null +++ b/api/scripts/populate_db.sh @@ -0,0 +1 @@ +python manage.py loaddata user_data diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d7a5466 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: "3.9" +services: + api: + build: + context: ./api + dockerfile: Dockerfile.dev + volumes: + - ./api:/home/app/api + ports: + - 8000:8000 + command: python manage.py runserver 0.0.0.0:8000 + ui: + build: + context: ./ui + dockerfile: Dockerfile.dev + volumes: + - ./ui:/home/app/ui + - /home/app/ui/node_modules + ports: + - 3000:3000 + command: npm run dev -- --host \ No newline at end of file diff --git a/ui/Dockerfile.dev b/ui/Dockerfile.dev new file mode 100644 index 0000000..786873e --- /dev/null +++ b/ui/Dockerfile.dev @@ -0,0 +1,24 @@ +FROM node:alpine3.16 + +# Set working DIE +WORKDIR /home/app/ui + +# Upgrade Alpine Packages +RUN apk -U upgrade + +# Add bash to run tests and other scripts +RUN apk add --no-cache bash + +# Copy package.json and package-lock.json +COPY package*.json . + +# Install node packages +RUN npm ci + +# Copy rest of code +COPY . . + +# PORT where UI will run in browser +EXPOSE 3000 + +CMD ["npm","run","dev","--","--host"] \ No newline at end of file