From 0723eee2a1f6e3641111af482188530b8ccbeef6 Mon Sep 17 00:00:00 2001 From: Prakash Date: Mon, 8 Jul 2024 19:34:30 +0530 Subject: [PATCH] feat: containerize the app --- .dockerignore | 34 +++++++++++++++++++++++++++++++ Dockerfile | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ README.Docker.md | 22 ++++++++++++++++++++ compose.yaml | 21 +++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README.Docker.md create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..fd00eaa96 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +LICENSE +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..520cdf7e8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +# syntax=docker/dockerfile:1 + +ARG NODE_VERSION=20.11.1 + +FROM node:${NODE_VERSION} AS build + +# Run the application as a non-root user. +USER node + +WORKDIR /usr/src/app + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.yarn to speed up subsequent builds. +# Leverage a bind mounts to package.json and yarn.lock to avoid having to copy them into +# into this layer. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=yarn.lock,target=yarn.lock \ + --mount=type=cache,target=/root/.yarn \ + yarn install --frozen-lockfile --ignore-scripts + +# Copy the rest of the source files into the image. +COPY . . + +# Build the application. +RUN yarn tsc + +################################################################################ + +#Production stage +FROM node:${NODE_VERSION}-alpine AS production + +# Run the application as a non-root user. +USER node + +# Use production node environment by default. +ENV NODE_ENV production + +WORKDIR /usr/src/app + +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=yarn.lock,target=yarn.lock \ + --mount=type=cache,target=/root/.yarn \ + yarn install --production --frozen-lockfile --ignore-scripts + +# Copy the build 'dist/' into the image. +COPY --from=build /usr/src/app/dist . + +# Expose the port that the application listens on. +EXPOSE 3000 + +# Run the application. +CMD ["node", "server.js"] + diff --git a/README.Docker.md b/README.Docker.md new file mode 100644 index 000000000..fa3048f63 --- /dev/null +++ b/README.Docker.md @@ -0,0 +1,22 @@ +### Building and running your application + +When you're ready, start your application by running: +`docker compose up --build`. + +Your application will be available at http://localhost:3000. + +### Deploying your application to the cloud + +First, build your image, e.g.: `docker build -t myapp .`. +If your cloud uses a different CPU architecture than your development +machine (e.g., you are on a Mac M1 and your cloud provider is amd64), +you'll want to build the image for that platform, e.g.: +`docker build --platform=linux/amd64 -t myapp .`. + +Then, push it to your registry, e.g. `docker push myregistry.com/myapp`. + +Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/) +docs for more detail on building and pushing. + +### References +* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/) \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 000000000..60ad62c5c --- /dev/null +++ b/compose.yaml @@ -0,0 +1,21 @@ +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Docker compose reference guide at +# https://docs.docker.com/go/compose-spec-reference/ + +# Here the instructions define your application as a service called "server". +# This service is built from the Dockerfile in the current directory. +# You can add other services your application may depend on here, such as a +# database or a cache. For examples, see the Awesome Compose repository: +# https://github.com/docker/awesome-compose +services: + server: + build: + context: . + environment: + NODE_ENV: production + DOCKER_BUILDKIT: 1 + ENABLE_FILE_LOGS: true + ENABLE_CONSOLE_LOGS: false + ports: + - 3000:3000 +