DEV Community

Nipu Chakraborty
Nipu Chakraborty

Posted on • Updated on

Docker Best Practices for Production

1. Always use specific version

❌ Don't do this

FROM node
Enter fullscreen mode Exit fullscreen mode

✅ Do this because it will ensure your container image version

FROM node:20.0.0
Enter fullscreen mode Exit fullscreen mode

2. Try to use alpine version

❌ Don't do this because It can be use large

FROM node:20.0.0
Enter fullscreen mode Exit fullscreen mode

✅ Do this for small official image

FROM node:20.0.0-alpine
Enter fullscreen mode Exit fullscreen mode

3. Dont install all dependencies

❌ Don't do this

RUN npm install
Enter fullscreen mode Exit fullscreen mode

✅ Do this because you don't need dev dependencies in your production server

RUN npm install --production
Enter fullscreen mode Exit fullscreen mode

4. Mantain versioning

❌ Don't do this

docker build -t myapp .
Enter fullscreen mode Exit fullscreen mode

✅ Do this it will help you to track version of your container images.

docker build -t myapp:1.0 .
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
code42cate profile image
Jonas Scholz

Good list! I've always used this one as reference when I started to learn Docker: snyk.io/blog/10-best-practices-to-...

Collapse
 
tyler36 profile image
tyler36

Thanks for the article.

It's a nice list which could be improved by saying why these things are bad.
Without this context, some people may think you copied AI/IDE suggestions without understanding why they are an improvement.

Collapse
 
nipu profile image
Nipu Chakraborty • Edited

Thanks for the feedback updated

Collapse
 
wesllycode profile image
wesllycode

Very nice !