Renaming Containers

Difficulty: Beginner

Question

How do you rename an existing Docker container, and what changes or stays the same when you do?

Answer

Use docker rename <current_name_or_id> <new_name>. It works on both running and stopped containers, takes effect instantly without restarting anything, and changes only the human-friendly name; the container ID, configuration, filesystem and running processes stay exactly the same.

Why names matter

Every container has a 64-character ID and a name. If you do not pass --name, Docker generates a random one like eager_hopper. Names are what you type in day-to-day commands (docker logs api), what scripts target, and, on user-defined networks, what other containers use as a DNS hostname to reach this one. Renaming is useful when you started a container without --name, when you want to keep an old version around (api becomes api-old before starting a new api), or to fix a naming convention.

Rules and behaviour

  • Unique names only. If the new name is taken, Docker returns a conflict error. Remove or rename the other container first.
  • Allowed characters. Names must start with a letter or digit and may contain letters, digits, _, . and -.
  • ID never changes. Anything referencing the container by ID keeps working.
  • No restart. The process is not signalled; open connections are unaffected.
  • DNS name follows the container name. On user-defined networks the new name becomes how other containers resolve it, so clients using the old hostname will fail on their next lookup. Aliases set with --network-alias are separate and not changed.
  • Images are different. You cannot rename an image with docker rename; use docker tag to give an image a new name.

A practical blue-green style swap

A small but handy pattern on a single server:

  1. docker rename api api-old
  2. docker run -d --name api --network app my-api:2.0
  3. Check docker logs api and the health endpoint.
  4. If good, docker rm -f api-old; if not, stop the new one and rename api-old back.

Gotchas

  • Compose-managed containers. Compose tracks containers by labels, not names, but renaming them breaks the <project>-<service>-<n> convention and confuses humans. Change the Compose file instead.
  • Scripts with hard-coded names will break silently. Grep your deploy scripts before renaming production containers.

Code examples

Renaming a container

# Started without --name, so Docker picked one
docker run -d nginx:1.27-alpine
docker ps --format '{{.ID}}  {{.Names}}'

# Give it a meaningful name (running container, no restart)
docker rename eager_hopper web-proxy
docker ps --format '{{.ID}}  {{.Names}}'

# Name already in use
docker rename web-proxy db

The ID stays a1b2c3d4e5f6 across the rename; only the name changes, and a duplicate name is rejected.

Key points

  • docker rename OLD NEW works on running and stopped containers without a restart.
  • Only the name changes; ID, config and data are untouched.
  • Names must be unique across the host.
  • On user-defined networks the container is resolved by its new name afterwards.
  • Use docker tag, not docker rename, for images.

Common follow-up questions

  • How do containers find each other by name on a Docker network?
  • What is the difference between a container name and a network alias?
  • How would you do a zero-downtime swap of a container on one host?

Interview tips

  • Answer the command in one line, then show depth by explaining what does not change.
  • Mention the DNS impact on user-defined networks; it shows you think about dependent services.
  • Do not confuse containers with images; mention docker tag if images come up.

Concepts covered

docker rename, Container Names, Container ID, Name Conflicts

More Docker & Containers questions