Skip to content

Commit f84b16c

Browse files
committed
Added Dockerfile template and initial README.
1 parent b6b62b6 commit f84b16c

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

Dockerfile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Step 1: Initial build using the `yarn build` command
2+
####################################################
3+
FROM node:latest as build
4+
5+
# Prepare the build directory
6+
RUN mkdir -p /opt/build;
7+
8+
WORKDIR /opt/build
9+
10+
# If your build step requires environment variables too, add them here
11+
12+
# Copy required files
13+
# Note: I specify each file directly here to avoid copying over
14+
# existing /dist folder or other dev files like .env
15+
COPY ./src ./src
16+
COPY [ "package.json", "yarn.lock", "tsconfig.json", "./" ]
17+
18+
RUN yarn install --no-progress && yarn build
19+
20+
21+
# Step 2: Fetch production-only dependencies
22+
####################################################
23+
FROM node:latest as dependencies
24+
25+
# Set environment to production
26+
ENV NODE_ENV='production'
27+
28+
RUN mkdir -p /opt/build;
29+
30+
WORKDIR /opt/build
31+
32+
COPY --from=build [ "/opt/build/package.json", "/opt/build/yarn.lock", "./" ]
33+
34+
RUN yarn install --production=true --no-progress
35+
36+
37+
# Step 3: Build done, create the deployable/runnable image step
38+
####################################################
39+
FROM node:slim as release
40+
41+
# Set environment to production
42+
ENV NODE_ENV='production'
43+
44+
# Prepare the app directory
45+
RUN mkdir -p /opt/app;
46+
47+
WORKDIR /opt/app
48+
49+
# Add any and all environment variables your application needs here:
50+
ENV JWT_SECRET= \
51+
PORT=80
52+
53+
# Copy dependencies and compiled application from previous steps
54+
COPY --from=dependencies /opt/build/node_modules /opt/app/node_modules
55+
COPY --from=build /opt/build/dist /opt/app/dist
56+
57+
WORKDIR /opt/app
58+
59+
# Run the application using node
60+
ENTRYPOINT [ "node", "dist/index.js" ]
61+
62+
# Alternatively: Run your application using Forever (ensure this is installed as a dependency first)
63+
# See: https://github.com/foreversd/forever
64+
# ENTRYPOINT [ "./node_modules/.bin/forever", "./dist/index.js" ]

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# docker-multistep-nodejs-build
2-
A simple multi-step nodejs application docker build template that you can use to create self-contained, relatively small-sized and easily deployable images of your application.
1+
# Multi-step build container template for NodeJS applications
2+
3+
This is a simple multi-step NodeJS application docker build template that you can use to create self-contained, relatively small-sized and easily deployable images of your application.
4+
5+
## Why multi-step
6+
7+
There are several benefits to this, but one of the bigger ones I would say is that your actual eventual container will not have any of the build tools installed, which can be safer or at the very least cleaner. Your application does not require yarn to run correctly, after all, only your build (and test) steps need this. So by splitting it out into multiple, stand-alone steps, your final image can be safer, and smaller too. You could even go one step further and flatten the built image to reduce the size even further, but that is outside the scope of this readme.
8+
9+
## Node versions
10+
11+
- The template uses `node:latest`, but normally I set a specific node version to ensure compatibility with whatever application I am working with/on. You might want to set a specific version in all three phases, or just stick with `latest` if you like.
12+
- The final stage (which actually runs your application) uses the `node:slim` image, a lighter version that only contains whatever is required to actually run Node. If your application or specific packages used is having issues running in this environment, consider changing the final phase to use `node:alpine` instead. Using the full image here would mean it still will come with tools like NPM and Yarn pre-installed, which is not ideal. Please refer to the [official node image documentation](https://hub.docker.com/_/node/) for more information.
13+
14+
## Prerequisites
15+
16+
- Assumes you are using Yarn (you need to make some changes if you are using NPM)
17+
- Assumes your build step can be triggered with `yarn build`
18+
- Assumes you are using typescript and copies over `tsconfig.json`. Remove that if you are not using TypeScript or this specific file.
19+
20+
There are very few other prerequisites as this is a self-contained image template, but there are a few assumptions that you should be aware of. There are two specific places in the Dockerfile where a specific list of files is copied into the image when building. If your application uses any additional files or folders relevant either when building or when running the application, you will have to update these.
21+
22+
The reason I opted not to copy everything into the image upon building is to avoid possible scenarios where sensitive or (local) development specific files are accidentally copied in. It is safer to rely on a whitelist of sorts, as you'll know for sure what is being copied over.
23+
24+
### What files and folders are copied
25+
26+
- ./src
27+
- package.json
28+
- yarn.lock
29+
- tsconfig.json
30+
31+
## How to use this template
32+
33+
Clone or download the Dockerfile and add it to your application's repository. Then, open it up and customize it to fit your needs. It will mostly work out of the box, but you might have to add your own environment variables or the like in. Look for the line comment in the Dockerfile to see where you can best do that.
34+
35+
## Contribution
36+
37+
Do you have any recommendations, suggestions or feedback? Please feel free to submit pull requests or reach out ot me over at [Twitter](https://twitter.com/hellodeibu).
38+
39+
I hope this is of some use to you.
40+
41+
Thank you kindly!

0 commit comments

Comments
 (0)