Skip to content

Instantly share code, notes, and snippets.

@hdhruna
Created August 23, 2019 12:58
Show Gist options
  • Save hdhruna/53a913d72eba2be67b569fe5da39860d to your computer and use it in GitHub Desktop.
Save hdhruna/53a913d72eba2be67b569fe5da39860d to your computer and use it in GitHub Desktop.
AngularUI multistage Dockerfile with pure NGINX layer
FROM node:11-alpine as base
WORKDIR /app
COPY . .
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force && npm config set unsafe-perm true && npm i npm@latest -g && npm install --quiet && npm audit fix && npm install -g @angular/cli@latest
FROM node:11-alpine as dev-base
WORKDIR /app
COPY --from=base /app/ ./
ARG APPLICATION_ENV="development"
ARG FRONTEND_URL="https://dev.frontend.com"
ENV APPLICATION_ENV=${APPLICATION_ENV} FRONTEND_URL=${FRONTEND_URL}
RUN npm run build
FROM node:11-alpine as prod-base
WORKDIR /app
COPY --from=base /app/ ./
ARG APPLICATION_ENV="production"
ARG FRONTEND_URL="https://frontend.com"
ENV APPLICATION_ENV=${APPLICATION_ENV} FRONTEND_URL=${FRONTEND_URL}
RUN npm run build
## Development image
FROM nginx:alpine as dev
WORKDIR /usr/share/nginx/html
COPY --from=dev-base /app/dist/frontend/ .
COPY --from=dev-base /app/coverage/ coverage
COPY --from=dev-base /app/documentation/ documentation
COPY nginx/default.conf /etc/nginx/nginx.conf
RUN apk -U add ca-certificates tzdata --no-cache
EXPOSE 8080/tcp
LABEL maintainer="Hitesh Narendra Dhruna" \
com.frontend.description="Frontend Website" \
com.frontend.env="development"
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf", "-g", "daemon off;"]
## Production image
FROM nginx:alpine as prod
WORKDIR /usr/share/nginx/html
COPY --from=prod-base /app/dist/frontend/ .
COPY nginx/default.conf /etc/nginx/nginx.conf
RUN apk -U add ca-certificates tzdata --no-cache
EXPOSE 8080/tcp
LABEL maintainer="Hitesh Narendra Dhruna" \
com.frontend.description="Frontend Website" \
com.frontend.env="production"
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf", "-g", "daemon off;"]
# Pure NGINX Image
FROM scratch as final
WORKDIR /usr/share/nginx/html
COPY --from=prod /etc/passwd /etc/group /etc/
COPY --from=prod /usr/share/nginx/html/ /usr/share/nginx/html/
COPY --from=prod /usr/sbin/nginx /usr/sbin/nginx
COPY --from=prod /etc/nginx/ /etc/nginx/
COPY --from=prod /var/run/ /var/run/
COPY --from=prod /usr/lib/libpcre.* /usr/lib/
COPY --from=prod /var/log/nginx/ /var/log/nginx/
COPY --from=prod /var/cache/nginx/ /var/cache/nginx/
COPY --from=prod /lib/*.so.* /lib/
EXPOSE 8080/tcp
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf", "-g", "daemon off;"]
#/etc/nginx/nginx.conf
user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
multi_accept on;
worker_connections 65535;
}
http {
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
types_hash_max_size 2048;
client_max_body_size 32M;
# MIME
include mime.types;
# load configs
include /etc/nginx/conf.d/*.conf;
default_type application/octet-stream;
# logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
# limits
limit_req_log_level warn;
limit_req_zone $binary_remote_addr zone=login:10m rate=10r/m;
server {
listen 8080;
# . files
location ~ /\.(?!well-known) {
deny all;
}
# favicon.ico
location = /favicon.ico {
log_not_found off;
access_log off;
}
# robots.txt
location = /robots.txt {
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.html =404;
}
# gzip
gzip on;
gzip_vary on;
gzip_http_version 1.1;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 256;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
@hdhruna
Copy link
Author

hdhruna commented Aug 23, 2019

Ensure 18.09 or later version of Docker is installed

To build for development:
DOCKER_BUILDKIT=1 docker build -t frontend-dev:latest --target dev .

To build for production:
DOCKER_BUILDKIT=1 docker build -t frontend-prod:latest --target prod .

To build a secured Production-ready Image with a minimum attack force:
DOCKER_BUILDKIT=1 docker build -t frontend:latest --target final .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment