Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker #10

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 18 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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: .
env_file:
- .env
ports:
- 3333:3333

6 changes: 4 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}`,
);
2 changes: 1 addition & 1 deletion src/modules/health/patients/controllers/create.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/pet/breeds/controllers/create.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/modules/pet/pets/controllers/create.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/modules/pet/species/controllers/create.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Loading