DEV Community

Cover image for Why Should You Specify a Language Version in Your Dockerfile?
Tiago Rosa da costa
Tiago Rosa da costa

Posted on

Why Should You Specify a Language Version in Your Dockerfile?

Let me share a lesson learned the hard way. Once, while working for a company, our application was running flawlessly. However, after deploying a new feature update, the application started displaying strange characters in place of properly accented words.

The development team spent countless hours investigating potential causes of the problem. After much effort, I noticed something odd in the Dockerfile: we were using Node.js without specifying a version. By default, Docker pulls the latest version if no specific version is defined.

In our case, Docker had downloaded Node.js version 22.7.0, which happened to have a critical bug affecting UTF-8 encoding. This caused words with accents to render as garbled characters. (For reference, see Node.js issue #54543.)

The Fix

The solution was simple yet crucial: we specified a stable Node.js version (in this case, v20) in the Dockerfile and redeployed the application. Immediately, everything returned to normal.

Best Practices for Your Dockerfile

Always specify the exact version of your base image to avoid such issues. Here's an example:

Image description

In the first line, we explicitly set the base image to Node.js v20, ensuring stability and avoiding unexpected behavior caused by breaking changes in future versions.

Bonus Tip: Optimize Your Docker Image

When creating a Docker image, consider using the Alpine version of your base image (e.g., node:20-alpine). Why?

Lightweight: Alpine images are significantly smaller, saving storage space. This is especially important for private repositories like AWS ECR, where costs are tied to storage usage.
Faster Builds: Smaller base images download and build faster, reducing deployment time.

By specifying the base image version and using optimized images, you can avoid unexpected bugs and improve your build process efficiency. Don’t let a simple oversight like omitting the language version derail your application’s stability!

Top comments (0)