Dockerfile
This Dockerfile details instructions for building a Docker image for a Node.js application. Let's analyze each section:
Base Image
# BASE IMAGE
# Using node:20-bullseye-slim as the base image for our Docker build
FROM node:20-bullseye-slim AS base
- Base Image: Uses the
node:20-bullseye-slim
image as a base. This is a lightweight Node.js version 20 image, based on Debian Bullseye Slim.
# Updating package lists and upgrading installed packages
RUN apt-get update -y
RUN apt-get upgrade -y
- Package Update: Updates the list of packages and then updates the installed packages.
# Installing git as it is commonly used for various build processes
RUN apt-get install -y git
- Git Installation: Installs Git, commonly needed for build processes.
# Installing a specific version of npm globally
RUN npm i -g npm@latest
- NPM Installation: Updates npm to the latest version globally.
Build Image
# BUILD IMAGE
# Creating a new build stage starting from the "base" stage
FROM base AS builder
- Build Stage: Starts a new build stage based on the "base" stage.
# Setting the working directory inside the Docker container
WORKDIR /codechat
- Working Directory: Defines
/codechat
as the working directory in the container.
# Copying specific project files needed for installing dependencies
COPY ./package.json ./tsconfig.json ./
- Copies Project Files: Copies
package.json
andtsconfig.json
to the container, necessary to install dependencies.
# Installing npm packages defined in package.json
RUN npm install
- Dependency Installation: Installs the npm dependencies defined in
package.json
.
# Copying source code, public files, documentation, and Prisma configuration
COPY ./src ./src
COPY ./public ./public
COPY ./docs ./docs
COPY ./prisma ./prisma
COPY ./views ./views
COPY ./.env ./.env
- Copy of Project Files: Copies the source code, public files, documentation, Prisma configuration and others to the container.
# Copying scripts to working directory.
COPY ./scripts ./scripts
- Script Copy: This command copies the scripts located in the host's
./scripts
directory to the same directory in the container. These scripts are used to configure and manage the database.
# Execution permission
RUN chmod +x ./scripts/*
- Execute Permission: Modifies the permissions of script files to make them executable. This is necessary so that scripts can be executed inside the container.
# Executing command
RUN ./scripts/run_database_operation_deploy.sh
- Running Command: Runs the
./scripts/run_database_operation_deploy.sh
command defined in npm scripts, which in turn runs the detailed Bash script.
The Bash script performs the following actions:
Environment Configuration:
- Checks if it is running in a Docker environment. If not, it reads environment variables from a
.env
file and exports them.
- Checks if it is running in a Docker environment. If not, it reads environment variables from a
Database URL Verification and Configuration:
- Checks if the
DATABASE_PROVIDER
is one of the supported ones (PostgreSQL, MySQL, MongoDB). - Builds the
DATABASE_URL
based on the provided environment variables, adjusting the schema according to the database provider.
- Checks if the
Execution of Prisma Commands:
- Runs
prisma migrate deploy
to apply database migrations. - Runs
prisma generate
to generate the Prisma client according to the schema.
- Runs
Error Handling:
- If the
DATABASE_PROVIDER
is not one of the expected ones, or if the.env
file is not found, the script displays an error message and ends with an error status.
- If the
This script is a crucial part of the deployment process, ensuring that the application has the correct environment variables and that the database is correctly configured and migrated before starting the application.
# Running the build process defined in package.json
RUN npm run build
- Build Process: Executes the build process defined in
package.json
.
Release Image
# RELEASE IMAGE
# Creating a new stage for the release image starting from the "base" stage
FROM base AS release
- Release Stage: Starts a new stage for the release image, using the "base" stage as a base.
# Setting the working directory
WORKDIR /codechat
# Copying package.json and package-lock.json from the "builder" stage
COPY --from=builder /codechat/package.json ./package.json
COPY --from=builder /codechat/package-lock.json ./package-lock.json
# Installing only production dependencies
RUN npm install --omit=dev
# Copying built code and Prisma configuration from the "builder" stage
COPY --from=builder /codechat/dist ./dist
COPY --from=builder /codechat/prisma ./prisma
COPY --from=builder /codechat/public ./public
COPY --from=builder /codechat/docs ./docs
COPY --from=builder /codechat/views ./views
COPY --from=builder /codechat/.env ./.env
COPY --from=builder /codechat/scripts ./scripts
# Creating a directory for instances (purpose may vary depending on the application)
RUN mkdir instances
# Setting environment variables for the Docker container
ENV DOCKER_ENV=true
ENV NODE_ENV='production'
- Release Stage Configuration: Copies necessary files from the build stage, installs only production dependencies and configures environment variables.
# Command to run when the Docker container starts
ENTRYPOINT ["/bin/bash", "-c", ". ./scripts/run_database_operation_generate.sh && npm run start:prod" ]
- Startup Command: Defines the
npm run start:prod
command to be executed when the container starts.
In summary, this Dockerfile
creates a Docker image for a Node.js application, using multiple stages to optimize the size of the final image. It starts with a base stage for general configurations, follows with a build stage to build the application, and ends with a release stage to prepare the image for production.