Create a Docker + Node.js service container using docker-compose.yml

Prerequisites

  • Docker is installed on the host
  • A basic understanding of Node.js and package.json configuration

Instructions

Create a project folder

Create a project folder of your choice. All the following files will be created under this folder.

Create the Dockerfile

This configuration assumes that there is a script called build in your package.json and it builds to a folder called build with an entrypoint of index.js. You may want to change these values.

FROM node:14-alpine  # Or your preferred version and flavor of node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:14-alpine  # Or your preferred version and flavor of node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm ci --only=production
COPY --from=0 /usr/src/app/build ./build
RUN npm install -g pm2
CMD ["pm2-runtime", "build/index.js"]

Create the docker-compose.yml

version: "3.1"
 services:
   timelapse01:
     image: yourimagename
     user: "1000:1000"  # Optional, but helps with file permissions. Use your uid and gid from the host

Build your image

In the terminal, run the following command on the project folder

docker build -t yourimagename .

Note that yourimagename needs to be the same name as in your docker-compose.yml

Run your container

To start your container using the image you’ve just built, run the following terminal command in your project folder.

docker-compose up -d

Stop your container

In order to stop the container, run the following terminal command in your project folder.

docker-compose down

Alternatively, run the following command to remove all the volumes associated with the container as well.

docker-compose down -v

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.