Skip to content

Commit 1996943

Browse files
author
William Blankenship
committed
Merge pull request #122 from retrohacker/master
Best Practices Document
2 parents 3184f12 + d094c98 commit 1996943

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ The image assumes that your application has a file named
4242
dependencies and defining its [start
4343
script](https://docs.npmjs.com/misc/scripts#default-values).
4444

45+
We have assembled a [Best Practices Guide](./docs/BestPractices.md) for those using these images on a daily basis.
46+
4547
## Run a single Node.js script
4648

4749
For many simple, single file projects, you may find it inconvenient to write a

docs/BestPractices.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Docker and Node.js Best Practices
2+
3+
## Environment Variables
4+
5+
Run with `NODE_ENV` set to `production`. This is the way you would pass inn secrets and other runtime configurations to your application as well.
6+
7+
```
8+
-e "NODE_ENV=production"
9+
```
10+
11+
## Non-root User
12+
13+
By default Docker runs container as root which inside of the container can pose as a security issue. You would want to run the container as an unprivileged user wherever possible. This is however not supported out of the box with the `node` Docker image.
14+
15+
```Dockerfile
16+
FROM node:4.1.2
17+
# Add our user and group first to make sure their IDs get assigned consistently
18+
RUN groupadd -r app && useradd -r -g app app
19+
```
20+
21+
This Docker Image can than be run with the `app` user in the following way:
22+
23+
```
24+
-u "app"
25+
```
26+
27+
#### Memory
28+
29+
By default any Docker Container may consume as much of the hardware such as CPU and RAM. If you are running multiple containers on the same host you should limit how much memory they can consume.
30+
31+
```
32+
-m "300M" --memory-swap "1G"
33+
```
34+
35+
## CMD
36+
37+
When creating an image, you can bypass the `package.json`'s `start` command and bake it directly into the image itself. This reduces the number of processes running inside of your container.
38+
39+
```Dockerfile
40+
CMD ["node","index.js"]
41+
```
42+
43+
## Docker Run
44+
45+
Here is an example of how you would run a default Node.JS Docker Containerized application:
46+
47+
```
48+
$ docker run \
49+
-e "NODE_ENV=production" \
50+
-u "app" \
51+
-m "300M" --memory-swap "1G" \
52+
-w "/usr/src/app" \
53+
--name "my-nodejs-app" \
54+
node [script]
55+
```
56+
57+
## Security
58+
59+
The Docker team has provided a tool to analyze your running containers for potential security issues. You can download and run this tool from here: https://github.com/docker/docker-bench-security

0 commit comments

Comments
 (0)