Image for post
Image for post
Docker and Node.js : a match made in heaven ?

Node.js Docker workflow

Guillaume Jacquart
May 29, 2018 · 7 min read

1. Optimize production artifact with Docker

RUN npm install --only=production

2. Normalize environments with Docker Compose

MYSQL_ROOT_PASSWORD=123456
MYSQL_DATABASE=database
APP_HOST=app.test.localhost.tv
FROM node:9-alpineWORKDIR /home/node/app# Install deps
COPY ./package* ./
RUN npm install && \
npm cache clean --force
COPY . .# Expose ports (for orchestrators and dynamic reverse proxies)
EXPOSE 3000
# Start the app
CMD npm start
version: '3'services:
reverse-proxy:
image: traefik # The official Traefik docker image
command: --api --docker.exposedbydefault=false # Enables the web UI and tells Træfik to listen to docker, without exposing by default
ports:
- "80:80" # The HTTP port
- "8080:8080" # The Web UI (enabled by --api)
volumes:
- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
db:
image: mysql:5
restart: always
environment:
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE
redis:
image: redis:alpine
app:
build: .
environment:
- DB_HOST=db
- DB_NAME=${MYSQL_DATABASE}
- DB_USER=root
- DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
- REDIS_HOST=redis
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=Host:${APP_HOST}"
depends_on:
- db
- redis
version: '3'services:
app:
build:
dockerfile: Dockerfile-dev
context: .
command: npm run dev
volumes:
- "./src:/home/node/app/src"
docker-compose -f docker-compose.yml -f docker-compose.dev.yml build
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

3. Smoothen delivery and integration with CI/CD

# Use the builder image as base image
FROM builder
# Copy the test files
COPY tests tests
# Override the NODE_ENV environment variable to 'dev', in order to get required test packages
ENV NODE_ENV dev
# 1. Get test packages; AND
# 2. Install our test framework - mocha
RUN npm update && \
npm install -g mocha
# Override the command, to run the test instead of the application
CMD ["mocha", "tests/test.js", "--reporter", "spec"]

Conclusion

Datree

datree brings back control to Git-centric and autonomous…

Guillaume Jacquart

Written by

Digital enthusiast, web engineer

Datree

Datree

datree brings back control to Git-centric and autonomous DevOps organizations, enabling continuous delivery and reliable flow of changes.

Guillaume Jacquart

Written by

Digital enthusiast, web engineer

Datree

Datree

datree brings back control to Git-centric and autonomous DevOps organizations, enabling continuous delivery and reliable flow of changes.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store