Skip to content

Instantly share code, notes, and snippets.

@raunak-r
Created May 13, 2022 16:19
Show Gist options
  • Save raunak-r/2177e52049a288df8a4d26eedab9ffeb to your computer and use it in GitHub Desktop.
Save raunak-r/2177e52049a288df8a4d26eedab9ffeb to your computer and use it in GitHub Desktop.
Dockerfiles

Dockerfiles

A collection of dockerfiles for Java, Python and Angular
#!/bin/bash
echo "Starting Redis"
/usr/bin/redis-server &
echo "Starting Celery Worker"
# TODO --uid and --gid options are required but throwing permission errors
# else this warning will show up RuntimeWarning:You're running the worker with superuser privileges:this is absolutely not recommended
# celery --app=INSERTAPPNAME worker --loglevel=info --uid=nobody --gid=nogroup &
#celery --app=INSERTAPPNAME worker --loglevel=info &
celery --app=INSERTAPPNAME worker --beat --scheduler django --loglevel=info &
echo "Collecting Static Files"
python3 manage.py collectstatic --noinput
echo "Applying database migrations"
python3 manage.py migrate
echo "Loading Policies"
python3 manage.py load-policies
echo "Calling createadmin to create admin user if not present."
python3 manage.py createadmin
echo "Starting server"
exec gunicorn INSERTAPPNAME.wsgi:application --bind 0.0.0.0:9600 --log-level=debug --workers 3 --timeout 90
# sudo docker build -t INSERTIMAGENAME .
# LOCAL - sudo docker run --network=host --log-opt max-size=10m INSERTIMAGENAME
### Stage 1: Build the application
FROM node:12-slim as build
RUN npm install -g @angular/cli@12.1.2
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY . /app/
RUN ng build
# Stage 2: Serve app with nginx server
FROM nginx:latest
# Copy the build output to replace the default nginx contents.
COPY --from=build /app/dist/ /usr/share/nginx/html
EXPOSE 80
# sudo docker build -t INSERTIMAGENAME .
# LOCAL - sudo docker run --network=host --log-opt max-size=10m INSERTIMAGENAME
### Build stage
FROM maven:3.6.0-jdk-11-slim AS build
WORKDIR /app
COPY pom.xml .
# resolve maven dependencies
RUN mvn clean package -Dmaven.test.skip -Dmaven.main.skip -Dspring-boot.repackage.skip && rm -r target/
COPY src ./src
# build the app (no dependency download here)
RUN mvn clean package -Dmaven.test.skip
# # https://stackoverflow.com/questions/53929866/spring-boot-to-exclude-connecting-to-database-during-maven-build
# # Else, it will try connecting to a db, and the build will fail, since db conn are supplied at runtime.
# RUN mvn -f /app/pom.xml clean install -DskipTests
### Package stage
FROM openjdk:11-jre-slim
COPY --from=build /app/target/INSERTAPPJARNAME.jar /usr/local/lib/INSERTAPPJARNAME.jar
EXPOSE INSERTPORTNUM
ENTRYPOINT ["java","-jar","/usr/local/lib/INSERTAPPJARNAME.jar"]
# INFO - WHY NOT TO USE ALPINE
## https://pythonspeed.com/articles/alpine-docker-python/
####
FROM ubuntu:20.04
# author/ maintainer
MAINTAINER raunak-r
# ubuntu upgrades
RUN apt-get update && \
apt-get -y upgrade && \
apt-get -y install curl
# RUN apt-get clean && apt-get -y autoremove && apt-get -y autoclean
# Install frontend dependencies
# RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
# RUN apt-get install -y nodejs
# RUN apt-get install -y gcc g++ make
# RUN node --version && \
# npm --version
# RUN npm uninstall -g @angular/cli@6.2.9
# RUN npm install -g @angular/cli@6.2.9
# Install backend dependencies
RUN apt-get install -y build-essential libsasl2-dev python3-dev libldap2-dev libssl-dev libffi-dev libpq-dev python3-pip
RUN apt-get install -y libmysqlclient-dev default-libmysqlclient-dev
RUN pip3 install --upgrade virtualenv && \
pip3 install --upgrade setuptools && \
pip3 install --upgrade Cython
RUN apt-get install -y redis-server
COPY ./pybackend/requirements.txt /app/pybackend/
RUN pip3 install prompt-toolkit==1.0.15
RUN pip3 install -r /app/pybackend/requirements.txt
## Everything above is constant and code-agnostic, which helps reduce the build time.
# Copy the main code folder inside the container
COPY . /app
# Grant folder permissions
# RUN chmod +x /app/frontend/
RUN mkdir /app/staticfiles/
RUN chmod +x /app/staticfiles/
# RUN chmod -R 777 /app/frontend/
# RUN mkdir /.npm
# RUN chmod -R 777 /.npm
# Remaining Frontend and Backend Installations
# WORKDIR /app/frontend/
# RUN npm install
# RUN ng build --prod
# RUN npm init
# RUN npm run-script build
# RUN ng postinstall
########## ENVIRONMENTS
EXPOSE INSERTPORTNUM
##########
# Run the django app without using a script
# WORKDIR /app/pybackend/
# CMD python3 manage.py migrate
# CMD python3 manage.py createadmin
# CMD python3 manage.py runsslserver 0.0.0.0:9600
# CMD exec gunicorn INSERTAPPNAME.wsgi:application --bind 0.0.0.0:9600 --log-level=debug --workers 3 --timeout 90
# Run django using a script so that migrate command can make use of correct env values
WORKDIR /app/pybackend/
RUN chmod +x django-entrypoint.sh
ENTRYPOINT ["/app/pybackend/django-entrypoint.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment