From a3051b8c44e22d561270209b1b175e209831dc44 Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 09:20:40 -0300 Subject: [PATCH 1/6] feat: add Dockerfile and compose --- .dockerignore | 34 +++++++++++++++++++++++++ Dockerfile | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ compose.yaml | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..dc157ff --- /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.y*ml +**/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 0000000..226a169 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,70 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 + +ARG BUN_VERSION=1.1.38 + +################################################################################ +# Use bun image for base image for all stages. +FROM oven/bun:${BUN_VERSION} as base + +# Set working directory for all build stages. +WORKDIR /usr/src/app + +################################################################################ +# Create a stage for installing production dependencies. +FROM base as deps + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.bun to speed up subsequent builds. +# Leverage bind mounts to package.json and bun.lockb to avoid having to copy them +# into this layer. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=bun.lockb,target=bun.lockb \ + --mount=type=cache,target=/root/.bun \ + bun install --production + +################################################################################ +# Create a stage for building the application. +FROM deps as build + +# Download additional development dependencies before building, as some projects require +# "devDependencies" to be installed to build. If you don't need this, remove this step. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=bun.lockb,target=bun.lockb \ + --mount=type=cache,target=/root/.bun \ + bun install + +# Copy the rest of the source files into the image. +COPY . . +# Run the build script. +RUN bun run build + +################################################################################ +# Create a new stage to run the application with minimal runtime dependencies +# where the necessary files are copied from the build stage. +FROM base as final + +# Use production bun environment by default. +ENV NODE_ENV production + +# Run the application as a non-root user. +USER bun + +# Copy package.json so that package manager commands can be used. +COPY package.json . + +# Copy the production dependencies from the deps stage and also +# the built application from the build stage into the image. +COPY --from=deps /usr/src/app/node_modules ./node_modules +COPY --from=build /usr/src/app/dist ./dist + +# Expose the port that the application listens on. +EXPOSE 3333 + +# Run the application. +ENTRYPOINT bun start diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..ec3337d --- /dev/null +++ b/compose.yaml @@ -0,0 +1,53 @@ +# 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 + env_file: + - .env + ports: + - 3333:3333 + +# The commented out section below is an example of how to define a PostgreSQL +# database that your application can use. `depends_on` tells Docker Compose to +# start the database before your application. The `db-data` volume persists the +# database data between container restarts. The `db-password` secret is used +# to set the database password. You must create `db/password.txt` and add +# a password of your choosing to it before running `docker-compose up`. +# depends_on: +# db: +# condition: service_healthy +# db: +# image: postgres +# restart: always +# user: postgres +# secrets: +# - db-password +# volumes: +# - db-data:/var/lib/postgresql/data +# environment: +# - POSTGRES_DB=example +# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password +# expose: +# - 5432 +# healthcheck: +# test: [ "CMD", "pg_isready" ] +# interval: 10s +# timeout: 5s +# retries: 5 +# volumes: +# db-data: +# secrets: +# db-password: +# file: db/password.txt + From c7a437484bf9d6678f527c08063feadc442e5be9 Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 09:52:30 -0300 Subject: [PATCH 2/6] fix: use export instead of export default --- src/app.ts | 6 ++++-- src/modules/health/patients/controllers/create.test.ts | 2 +- src/modules/pet/breeds/controllers/create.test.ts | 2 +- src/modules/pet/pets/controllers/create.test.ts | 2 +- src/modules/pet/species/controllers/create.test.ts | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/app.ts b/src/app.ts index 7e7714c..b057a38 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,7 +7,7 @@ import breedsRoutes from "./modules/pet/breeds/routes"; import { axiomTelemetry } from "./modules/shared/utilities/telemetry"; import patientsRoutes from "./modules/health/patients/routes"; -const app = new Elysia() +export const app = new Elysia() .use(axiomTelemetry()) .use(swagger()) .use(petsRoutes) @@ -16,4 +16,6 @@ const app = new Elysia() .use(patientsRoutes) .listen(env.PORT); -export default app; +console.log( + `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`, +); diff --git a/src/modules/health/patients/controllers/create.test.ts b/src/modules/health/patients/controllers/create.test.ts index 98b370f..1cf5201 100644 --- a/src/modules/health/patients/controllers/create.test.ts +++ b/src/modules/health/patients/controllers/create.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "bun:test"; -import app from "@/app"; +import { app } from "@/app"; import { CreatePatient } from "../types"; describe("Create patient e2e", () => { diff --git a/src/modules/pet/breeds/controllers/create.test.ts b/src/modules/pet/breeds/controllers/create.test.ts index 439880c..62fb0be 100644 --- a/src/modules/pet/breeds/controllers/create.test.ts +++ b/src/modules/pet/breeds/controllers/create.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "bun:test"; -import app from "@/app"; +import { app } from "@/app"; import { CreateBreed } from "../types"; import db from "@/db"; import { breedsTable, speciesTable } from "@/db/schema"; diff --git a/src/modules/pet/pets/controllers/create.test.ts b/src/modules/pet/pets/controllers/create.test.ts index d8747ec..e7cfda0 100644 --- a/src/modules/pet/pets/controllers/create.test.ts +++ b/src/modules/pet/pets/controllers/create.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "bun:test"; -import app from "@/app"; +import { app } from "@/app"; import { CreatePet } from "../types"; import db from "@/db"; import { breedsTable, speciesTable } from "@/db/schema"; diff --git a/src/modules/pet/species/controllers/create.test.ts b/src/modules/pet/species/controllers/create.test.ts index fca5b2b..873e7d3 100644 --- a/src/modules/pet/species/controllers/create.test.ts +++ b/src/modules/pet/species/controllers/create.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "bun:test"; -import app from "@/app"; +import { app } from "@/app"; import { CreateSpecie } from "../types"; import db from "@/db"; import { speciesTable } from "../specie"; From 98e68e17f5e55aa6147fa5a4ad9bec83e057b2f7 Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 11:01:54 -0300 Subject: [PATCH 3/6] feat: clean compose --- compose.yaml | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/compose.yaml b/compose.yaml index ec3337d..d2986d7 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,43 +11,8 @@ services: server: build: context: . - environment: - NODE_ENV: production env_file: - .env ports: - 3333:3333 -# The commented out section below is an example of how to define a PostgreSQL -# database that your application can use. `depends_on` tells Docker Compose to -# start the database before your application. The `db-data` volume persists the -# database data between container restarts. The `db-password` secret is used -# to set the database password. You must create `db/password.txt` and add -# a password of your choosing to it before running `docker-compose up`. -# depends_on: -# db: -# condition: service_healthy -# db: -# image: postgres -# restart: always -# user: postgres -# secrets: -# - db-password -# volumes: -# - db-data:/var/lib/postgresql/data -# environment: -# - POSTGRES_DB=example -# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password -# expose: -# - 5432 -# healthcheck: -# test: [ "CMD", "pg_isready" ] -# interval: 10s -# timeout: 5s -# retries: 5 -# volumes: -# db-data: -# secrets: -# db-password: -# file: db/password.txt - From eb3062cdf63df641d665832f7c96ed6a345c50ef Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 11:03:24 -0300 Subject: [PATCH 4/6] style: use the new sintax for Dockerfile --- Dockerfile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 226a169..0a05cca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,14 +10,14 @@ ARG BUN_VERSION=1.1.38 ################################################################################ # Use bun image for base image for all stages. -FROM oven/bun:${BUN_VERSION} as base +FROM oven/bun:${BUN_VERSION} AS base # Set working directory for all build stages. WORKDIR /usr/src/app ################################################################################ # Create a stage for installing production dependencies. -FROM base as deps +FROM base AS deps # Download dependencies as a separate step to take advantage of Docker's caching. # Leverage a cache mount to /root/.bun to speed up subsequent builds. @@ -30,7 +30,7 @@ RUN --mount=type=bind,source=package.json,target=package.json \ ################################################################################ # Create a stage for building the application. -FROM deps as build +FROM deps AS build # Download additional development dependencies before building, as some projects require # "devDependencies" to be installed to build. If you don't need this, remove this step. @@ -47,10 +47,10 @@ RUN bun run build ################################################################################ # Create a new stage to run the application with minimal runtime dependencies # where the necessary files are copied from the build stage. -FROM base as final +FROM base AS final # Use production bun environment by default. -ENV NODE_ENV production +ENV NODE_ENV=production # Run the application as a non-root user. USER bun @@ -67,4 +67,4 @@ COPY --from=build /usr/src/app/dist ./dist EXPOSE 3333 # Run the application. -ENTRYPOINT bun start +ENTRYPOINT ["bun", "start"] From 58242f6c8ce689718003afad7964fdcf3e09b69f Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 11:03:37 -0300 Subject: [PATCH 5/6] doc: update README --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index d56d9f1..97a821c 100644 --- a/README.md +++ b/README.md @@ -142,3 +142,28 @@ To run end-to-end tests, use the following command: ```sh bun run test:e2e ``` + +## Running with Docker + +To run the project using Docker, use the following commands: + +1. Build the Docker image: + ```sh + docker build -t modular-pets . + ``` + +2. Run the Docker container: + ```sh + docker run -p 3333:3333 --env-file .env modular-pets + ``` + +OR + +1. Use Docker Compose to build and run the project: + ```sh + docker-compose up + ``` + +OBS1: Don't forget to fill in the necessary environment variables in the `.env` file. + +OBS2: Use the hostname `host.docker.internal` instead of `localhost` to connect to the host machine from the Docker container. \ No newline at end of file From e66ca0f2da0c4c6cf00c3f98482ae046a8df32d0 Mon Sep 17 00:00:00 2001 From: Vinicius Cestari Date: Tue, 17 Dec 2024 11:04:10 -0300 Subject: [PATCH 6/6] doc: update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 97a821c..504ab3f 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,6 @@ OR docker-compose up ``` -OBS1: Don't forget to fill in the necessary environment variables in the `.env` file. +- OBS1: Don't forget to fill in the necessary environment variables in the `.env` file. -OBS2: Use the hostname `host.docker.internal` instead of `localhost` to connect to the host machine from the Docker container. \ No newline at end of file +- OBS2: Use the hostname `host.docker.internal` instead of `localhost` to connect to the host machine from the Docker container. \ No newline at end of file