Skip to content

Commit

Permalink
add docker dev setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mariovyord committed Dec 13, 2023
1 parent a70fbae commit f3bfeaa
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env
.env.dev
dist
node_modules
node_modules
.npm
33 changes: 30 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 . .
Expand All @@ -17,4 +21,27 @@ COPY . .
RUN npm run build

# Command to run the app
CMD ["npm", "start"]
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"]
19 changes: 19 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -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"]
36 changes: 30 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions docker-compose-debug.yml
Original file line number Diff line number Diff line change
@@ -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"
41 changes: 41 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Overlay configuration to run tests
version: "3.9"
services:
app:
command:
- "npm"
- "run"
- "test"
1 change: 1 addition & 0 deletions docker-compose.yaml → docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
build:
context: .
dockerfile: ./Dockerfile
target: production
ports:
- "5000:5000"
networks:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f3bfeaa

Please sign in to comment.