Basic Dockerfile for TypeScript [Node/JavaScript]


In this article, I would like to share basic Dockerfile for Node.js and TypeScript.

🤔 Dockerfile

Basic Express.js App

This Dockerfile is for basic TypeScript Application with Node.js process(e.g. Express.js):

FROM node

# Create app directory
RUN mkdir -p /app
WORKDIR /app

# Install app dependencies
COPY package.json /app
COPY yarn.lock /app
RUN yarn

# Bundle app source
COPY . /app
RUN yarn build

EXPOSE 80
CMD [ "yarn", "start" ]

create-react-app Project

This Dockerfile is for basic React.js Application with create-react-app:

## Build environment ##
FROM node as builder

# Create app directory
RUN mkdir -p /app
WORKDIR /app

# Install app dependencies
COPY package.json /app
COPY yarn.lock /app
RUN yarn

# Bundle app source
COPY . /app
RUN yarn build

## production environment ##
FROM nginx:1.15.0-alpine

# Copy Nginx conf
RUN rm -rf /etc/nginx/conf.d
COPY conf /etc/nginx

# Copy react code
COPY --from=builder /usr/src/app/build /usr/share/nginx/html

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Nuxt.js with SSR

It is Dockerfile for running Nuxt.js with SSR. It supports to minimize Docker image by “Docker Multi-stage build” and “node-prune”

FROM node:10.11.0-alpine as builder

WORKDIR /app

RUN apk add --no-cache curl git && cd /tmp && \
curl -#L https://github.com/tj/node-prune/releases/download/v1.0.1/node-prune_1.0.1_linux_amd64.tar.gz | tar -xvzf- && \
mv -v node-prune /usr/local/bin && rm -rvf * && \
echo "yarn cache clean && node-prune" > /usr/local/bin/node-clean && chmod +x /usr/local/bin/node-clean

ADD package.json ./
RUN yarn --frozen-lockfile --non-interactive
ADD . ./
RUN yarn build

ENV NODE_ENV=production
RUN yarn --frozen-lockfile --non-interactive --production && node-clean

FROM node:10.11.0-alpine

WORKDIR /app
ENV HOST=0.0.0.0

ADD package.json ./
ADD nuxt.config.js ./

COPY --from=builder ./app/node_modules ./node_modules/
COPY --from=builder ./app/.nuxt ./.nuxt/
COPY --from=builder ./app/src/static ./src/static/

EXPOSE 8080
CMD ["yarn", "start", "-p", "8080"]

🚕 docker-compose

Basic docker-compose.yml are as follows:

version: '3.5'

services:
app:
build: .
volumes:
- '.:/app'
ports:
- '3000:80'
environment:
- NODE_ENV=development

Build the image and fire up the container:

docker-compose up -d --build

🐯 .gitignore

To generate .gitignore, please run the command:

gibo Ruby MacOS JetBrains Node >> .gitignore

😸 .dockerignore

.dockerignore contains:

node_modules
npm-debug.log
.next

🐮 [Appendix] Install node & yarn

If you want to install Node.js or yarn in Dockerfile, please write as follows:

# Install Node.js 8
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs

# Install Node.js 10
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs

# Install yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt-get update && apt-get install yarn

🐹 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!