Estimated reading time: 4 minutes
Last updated on November 8th, 2024 at 04:49 pm
A clear Docker container naming convention is important for effective management, organization, and easy identification of containers in distributed and complex environments.
We will discuss the 10 best practices for naming Docker containers with detailed examples. We will also check the good and bad examples for the naming convention.
By following their best practices, you can ensure that Docker containers are easy to identify and manage.
Table of Contents
DevOps Efficiency Hacks in Your Inbox! 📩
Stop wasting time searching. Get weekly tips & tutorials to streamline your DevOps workflow.
1. Be Descriptive:
Use a name that clearly indicates the purpose of the container. Having a clear functional container name helps anyone in a team understand the role of the Docker container.
Good 👍
wordpress-db
Clear and function name indicates that this container is for WordPress Database
Bad 👎
db1
No information about the container and its purpose.
2. Use Lowercase Letters:
Docker container names are case-sensitive with limitations on only using uppercase, lowercase letters, digits, hyphens, underscores, and dots.
The name must follow the regex here:
https://github.com/moby/moby/blob/master/daemon/names/names.go#L6
[a-zA-Z0-9][a-zA-Z0-9_.-]
- numbers
0-9
- lowercase letters
a-z
- uppercase letters
A-Z
- underscore
_
- point
.
- hyphen
-
I recommended sticking to the lowercase letter for consistency in the Docker container naming convention.
Good 👍
nginx-proxy
Bad 👎
NginxProxy
Not a consistent name and it looks weird 🤮
3. Avoid Special Characters:
Docker container naming convention allows certain special characters such as hyphens ( – ), underscores ( _ ) and dot ( . )
For better compatibility and readability avoid other special characters and stick to the default uppercase, lowercase letters, digits, hyphens, and underscores.
Good 👍
mongo-db
Bad 👎
mongo!db
4. Use Short and Meaningful Names:
To communicate with another container, the name must be a maximum of 63 characters else DNS will fail to resolve. Keeping the name concise and meaningful is the goal for this Docker container naming convention
- https://github.com/moby/moby/issues/46928
- You can check the source code for additional information.
Using long and complex names can create trouble and readability issues.
Good 👍
nginx-proxy
Bad 👎
a-long-time-ago-in-a-galaxy-someone-use-long-name
Fast-Track Your DevOps Career 🚀
Stay ahead of the curve with the latest industry insights. Get weekly tips & propel your skills to the next level.
5. Use Hyphens or Underscores for Separation:
Docker doesn’t allow special characters other than hyphens ( – ) or underscores ( _ ). But if you use this properly this can increase the readability of your Docker container naming convention.
Good 👍
python-backend
Bad 👎
pythonbackend
Well, why use long names without separation? It looks ugly and hard to read.
I will not include a bad example here as this is optional.
6. Include Environment Information:
If you run multiple containers in the same or different environment such as development, staging, or production, consider including the environment-related information in Docker container naming conventions.
Good 👍
webserver-production
Bad 👎
webserver
7. Avoid Ambiguity:
Docker will not allow to have a duplicate container name but ensure that each Docker container is unique within the environment.
This will help to avoid any conflict and confusion.
Good 👍
app-backend
Bad 👎
app
Here app
can be any app frontend or backend.
8. Avoid Using Generic Names:
I’m sure most users use generic terms while naming the Docker container, it’s easy to remember right?
The best practice for the Docker container naming convention is to avoid generic names like “web“, “database“, “app” or “server“
Avoid this unless they reflect the purpose and no duplication or ambiguity.
Good 👍
wordpress
Bad 👎
web
9. Consistent Naming Convention:
Define the consistent naming convention policy across the entire Docker environment. Having the policy in place makes sure that it is easy to manage and maintain over time even with cross-teams.
Good 👍
# Staging Env
nginx-proxy-staging
# Production Env
nginx-proxy-production
10. Include Version Information (Optional):
Well, it’s best practice to include version information as a Docker tag rather than a name but for some reason, if you’re only using the latest
as the default Docker tag then this might be helpful.
Good 👍
mysql-5.7
Level Up Your DevOps Skills! 📈
Get Weekly Tips, Tutorials & Master the Latest Trends – Subscribe Now!
Conclusion:
By following the above Docker container naming convention tips, you can ensure that you have a well-named Docker container that is easy to understand and manage. A consistent naming scheme across the Docker environment enables you to operate and maintain long-term.
If you’re looking for how to make your Docker container secure check out the Docker Container Security Cheatsheet and don’t get hacked🔐