Custom docker file NodeJS - adonisv79/bytecommander.com GitHub Wiki

Dockerfile

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
EXPOSE 3000

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

.dockerignore

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

# Repositories
.git
.gitignore

# IDEs
.vs
.idea

# Non-production configs
*.env

# Code quality checks
.eslintrc
.eslint
.jest.config
*.test.*
coverage

# Source codes and uncompiled src
src
*.ts

# Non-production items in our codes
*.log
docs

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

Build

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