Custom docker file NodeJS - adonisv79/ GitHub Wiki


Create a file named 'Dockerfile' in your node project root and enter the following

FROM node:10

ENV MY_ENV_VAR="hello world"
ENV NODE_ENV=production

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied where available (npm@5+)
COPY package*.json ./

# If you are building your code for development and want the dev dependencies use 'RUN npm install' only
RUN npm ci --only=production

# Run our production build script from package.json
run npm run build

# Copy the distributable files
COPY dist/* dist/

# Default port for our node project

# Execute node on index.js
CMD [ "node", "index.js" ]


Docker can ignore some files thru the '.dockerignore' file. create one and enter the following

# Repositories

# IDEs

# Non-production configs

# Code quality checks

# Source codes and uncompiled src

# Non-production items in our codes

# We need to add node modules as this ensures dev ependency files are not added


run the following command from the folder where the Dockerfile and .dockerignore file resides.

// format: "docker build -t {your username}/{project-name} ."
docker build -t adonisv79/my-proj .

Running the image

docker run -p 8080:3000 -e NODE_ENV="development" -d adonisv79/my-proj

Login before you publish

docker login

Publish on dockerhub

docker push adonisv79/my-proj