Skip to content

Commit

Permalink
fix(docker): optimize things
Browse files Browse the repository at this point in the history
  • Loading branch information
barthofu committed Mar 2, 2024
1 parent bcd74ec commit 2c8594e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
74 changes: 53 additions & 21 deletions .docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
# ======================
# ===== Build stage ====
# ======================
# This dockerfile has multiple stages in order
# to optimize the build process time and reduce
# the final image size.

FROM node:20.11-alpine as builder
# ============================
# ==== Dependencies stage ====
# ============================

FROM node:20.11-alpine as dependencies

WORKDIR /app

COPY src ./src
COPY tsconfig.json .
COPY package.json .
COPY package-lock.json .

# install deps
RUN apk add --no-cache --virtual .build-deps alpine-sdk python3 && \
npm ci --silent && \
npm run install:plugins && \
apk del .build-deps

# build project
# ======================
# ===== Build stage ====
# ======================

FROM node:20.11-alpine as builder

WORKDIR /app

# copy source files
COPY src ./src
COPY tsconfig.json .

# copy build files from dependencies stage
COPY --from=dependencies /app/package.json .
COPY --from=dependencies /app/package-lock.json .
COPY --from=dependencies /app/node_modules /app/node_modules

# install plugin dependencies
RUN npm run install:plugins

# build the project
RUN npm run build

# ==================
# ========================
# ===== Prepare stage ====
# ========================

FROM node:20.11-alpine as prepare

WORKDIR /app

# copy build files from builder stage
COPY --from=builder /app/build /app/build
COPY --from=builder /app/package.json /app/package.json
COPY --from=builder /app/package-lock.json /app/package-lock.json
COPY --from=builder /app/node_modules /app/node_modules

# purge dev dependencies
RUN npm prune --omit=dev

# =====================
# ===== Run stage =====
# ==================
# =====================

FROM node:20.11-alpine as runner

Expand All @@ -32,17 +70,11 @@ FROM node:20.11-alpine as runner
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

# copy build files from builder
COPY --from=builder /app/build /app/build
COPY --from=builder /app/package.json /app/package.json
COPY --from=builder /app/package-lock.json /app/package-lock.json

# purge dev dependencies
# RUN npm prune --omit=dev && \
RUN apk add --no-cache --virtual .build-deps alpine-sdk python3 && \
npm ci --silent --only=production && \
npm cache clean --force && \
apk del .build-deps
# copy build files from prepare stage
COPY --from=prepare /app/build /app/build
COPY --from=prepare /app/package.json /app/package.json
COPY --from=prepare /app/package-lock.json /app/package-lock.json
COPY --from=prepare /app/node_modules /app/node_modules

# finaly start the bot
CMD ["npm", "run", "start"]
51 changes: 14 additions & 37 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
version: '3'

networks:
dev:

services:

# ==== Application ====
Expand All @@ -20,30 +17,11 @@ services:
- ./database:/app/database
- ./logs/:/app/logs
- ./assets:/app/assets
restart: always
networks:
- dev
restart: unless-stopped
# depends_on:
# - database

# ==== MySQL database ====

# database:
# image: mysql # or `mariadb`
# container_name: database
# restart: always
# volumes:
# - ./data:/var/lib/mysql:rw
# environment:
# - MYSQL_DATABASE=${DATABASE_NAME}
# - MYSQL_USER=${DATABASE_USER}
# - MYSQL_PASSWORD=${DATABASE_PASSWORD}
# - MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD}
# ports:
# - 3306:3306
# networks:
# - dev


# ==== PostgreSQL database ====

# database:
Expand All @@ -58,20 +36,19 @@ services:
# - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
# ports:
# - 5432:5432
# networks:
# - dev

# ==== phpMyAdmin ====
# ==== MySQL database ====

# phpmyadmin:
# image: phpmyadmin
# container_name: phpmyadmin
# database:
# image: mysql # or `mariadb`
# container_name: database
# restart: always
# depends_on:
# - database
# ports:
# - 8080:80
# volumes:
# - ./data:/var/lib/mysql:rw
# environment:
# PMA_HOST: database
# networks:
# - dev
# - MYSQL_DATABASE=${DATABASE_NAME}
# - MYSQL_USER=${DATABASE_USER}
# - MYSQL_PASSWORD=${DATABASE_PASSWORD}
# - MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD}
# ports:
# - 3306:3306

0 comments on commit 2c8594e

Please sign in to comment.