Skip to content

Commit 0e0c74a

Browse files
author
William Blankenship
committed
Create BestPractices.md
Best Practices
1 parent 3184f12 commit 0e0c74a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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)