Skip to main content
AllDevToolsHub
Back to all patterns

Docker Container Name Validation

Validation

Validates Docker container names: alphanumeric, underscores, dots, and hyphens.

/^[a-zA-Z0-9][a-zA-Z0-9_.-]+$/

How it works

Docker container names must start with an alphanumeric character and can contain letters, digits, underscores, dots, and hyphens. Minimum 2 characters. Unlike image names, container names allow uppercase letters.

Test Cases

Should Match

  • my-container
  • web_server_1
  • nginx.proxy

Should NOT Match

  • -leading-hyphen
  • a
  • has space

Quick Summary

Validates Docker container names: must start with alphanumeric, can contain letters, digits, underscores, dots, and hyphens. Minimum 2 characters. Container names allow uppercase unlike image names.

Key Takeaways

Key Takeaways

  • Must start with a letter or digit
  • Allows uppercase letters (unlike image names)
  • Allows underscores, dots, and hyphens
  • Minimum 2 characters
Use Cases

When to use it

  • Validating container name inputs in Docker management UIs
  • Checking container names in docker-compose files
  • Enforcing naming conventions in container orchestration scripts
Watch out

Common Mistakes

  • Confusing container name rules with image name rules, images require lowercase, containers allow uppercase
  • Using spaces in container names, use hyphens or underscores instead
FAQ

Docker Container Name Validation, Frequently Asked

Are Docker container names unique?

Yes, within a Docker host. Two containers cannot have the same name on the same host.

How do I rename a Docker container?

Use: docker rename old-name new-name