From f3bfeaaa05c2f1706e58a60f20c9fd8dd3c11a51 Mon Sep 17 00:00:00 2001 From: NecroBread Date: Wed, 13 Dec 2023 12:57:07 +0200 Subject: [PATCH] add docker dev setup --- .gitignore | 4 ++- Dockerfile | 33 ++++++++++++++++-- Dockerfile.dev | 19 +++++++++++ Makefile | 36 ++++++++++++++++---- docker-compose-debug.yml | 12 +++++++ docker-compose-dev.yml | 41 +++++++++++++++++++++++ docker-compose-test.yml | 8 +++++ docker-compose.yaml => docker-compose.yml | 1 + package.json | 2 ++ 9 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 Dockerfile.dev create mode 100644 docker-compose-debug.yml create mode 100644 docker-compose-dev.yml create mode 100644 docker-compose-test.yml rename docker-compose.yaml => docker-compose.yml (88%) diff --git a/.gitignore b/.gitignore index 7c0f536..7b80dcf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .env +.env.dev dist -node_modules \ No newline at end of file +node_modules +.npm \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0ef5e7f..0edcd05 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,15 @@ FROM node:20.10.0-bullseye-slim # Set the working directory in the container WORKDIR /usr/src/app -# Copy package.json and package-lock.json to the working directory +# Copy package.json and package-lock.json to the working directory (better layer caching) COPY package*.json ./ +FROM base as dev + # Install dependencies -RUN npm install +RUN --mount=type=cache,target=/usr/src/app/.npm \ + npm set cache /usr/src/app/.npm && \ + npm install # Copy all local files to the working directory COPY . . @@ -17,4 +21,27 @@ COPY . . RUN npm run build # Command to run the app -CMD ["npm", "start"] \ No newline at end of file +CMD ["npm", "run", "dev"] + +FROM base as production + +# Set NODE_ENV +ENV NODE_ENV production + +# Install only production dependencies +# Use cache mount to speed up install of existing dependencies +RUN --mount=type=cache,target=/usr/src/app/.npm \ + npm set cache /usr/src/app/.npm && \ + npm ci --only=production + +# Use non-root user +# Use --chown on COPY commands to set file permissions +USER node + +# Copy remaining source code AFTER installing dependencies. +COPY --chown=node:node . . + +# Indicate expected port +EXPOSE 5000 + +CMD ["npm", "start"] diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..306ad47 --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,19 @@ +# Use the official Node.js LTS image +FROM node:20.10.0-bullseye-slim + +# Set the working directory in the container +WORKDIR /usr/src/app + +# Copy package.json and package-lock.json to the working directory (better layer caching) +COPY package*.json ./ + +# Install dependencies +RUN --mount=type=cache,target=/usr/src/app/.npm \ + npm set cache /usr/src/app/.npm && \ + npm install + +# Copy all local files to the working directory +COPY . . + +# Command to run the app +CMD ["npm", "run", "dev"] diff --git a/Makefile b/Makefile index 53af966..5294435 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,32 @@ -up: - @echo "Stopping docker images (if running...)" - docker-compose down - @echo "Building (when required) and starting docker images..." - docker-compose up --build -d - @echo "Docker images built and started!" +PROD_COMPOSE_FILE=docker-compose.yml +DEV_COMPOSE_FILE=docker-compose-dev.yml +DEBUG_COMPOSE_FILE=docker-compose-debug.yml +TEST_COMPOSE_FILE=docker-compose-test.yml +### DOCKER COMPOSE COMMANDS +.PHONY: compose-build +compose-build: + docker compose -f $(DEV_COMPOSE_FILE) build + +.PHONY: compose-up +compose-up: + docker compose -f $(DEV_COMPOSE_FILE) up + +.PHONY: compose-up-build +compose-up-build: + docker compose -f $(DEV_COMPOSE_FILE) up --build + +.PHONY: compose-up-debug-build +compose-up-debug-build: + docker compose -f $(DEV_COMPOSE_FILE) -f $(DEBUG_COMPOSE_FILE) up --build + +.PHONY: compose-down +compose-down: + docker compose -f $(DEV_COMPOSE_FILE) down + +### + +.PHONY: run-tests +run-tests: + docker compose -f $(DEV_COMPOSE_FILE) -f $(TEST_COMPOSE_FILE) run --build api-node \ No newline at end of file diff --git a/docker-compose-debug.yml b/docker-compose-debug.yml new file mode 100644 index 0000000..a034165 --- /dev/null +++ b/docker-compose-debug.yml @@ -0,0 +1,12 @@ +# Overlay configuration to enable debuggers +version: "3.9" +services: + app: + command: + - "npm" + - "run" + - "debug-docker" + ports: + - "5000:5000" + # inspect debug port + - "9229:9229" diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 0000000..edcc01a --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,41 @@ +version: "3" + +services: + app: + build: + context: . + dockerfile: ./Dockerfile.dev + ports: + - "5000:5000" + networks: + - backend + depends_on: + - mongo + env_file: + - .env.dev + volumes: + - type: bind + source: . + target: /usr/src/app/ + - type: volume + target: /usr/src/app/node_modules + deploy: + mode: replicated + replicas: 1 + + mongo: + image: mongo:6.0.4 + ports: + - "27018:27017" + networks: + - backend + volumes: + - mongodata:/data/db + +volumes: + mongodata: + driver: local + +networks: + backend: + name: backend diff --git a/docker-compose-test.yml b/docker-compose-test.yml new file mode 100644 index 0000000..958e7aa --- /dev/null +++ b/docker-compose-test.yml @@ -0,0 +1,8 @@ +# Overlay configuration to run tests +version: "3.9" +services: + app: + command: + - "npm" + - "run" + - "test" diff --git a/docker-compose.yaml b/docker-compose.yml similarity index 88% rename from docker-compose.yaml rename to docker-compose.yml index daf79e6..214db79 100644 --- a/docker-compose.yaml +++ b/docker-compose.yml @@ -5,6 +5,7 @@ services: build: context: . dockerfile: ./Dockerfile + target: production ports: - "5000:5000" networks: diff --git a/package.json b/package.json index 706e245..e06b307 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "main": "src/index.ts", "scripts": { "dev": "nodemon src/index.ts", + "debug": "nodemon --inspect src/index.ts", + "debug-docker": "nodemon --inspect=0.0.0.0:9229 src/index.ts", "build": "tsc", "start": "node dist/index.js", "lint": "eslint . --ext .ts",