Docker Rename Container

In this quick guide, we will see how to rename a Docker container with ease.

Docker rename command

The docker rename command allows you to rename a container. The basic syntax for this command is as follows:

docker rename [OPTIONS] CONTAINER NEW_NAME

CONTAINER: This is the name or ID of the existing container that you wish to rename.

NEW_NAME: This is the new name you want to assign to the container.

Example

Default Container Names

When you run a Docker container without specifying a name using the --name option, Docker generates a default name for you. This default name isn't just a random string; it's a combination of an adjective and a famous scientist or engineer's name.

For example:
Run the Redis container without specifying a name:
docker run -d redis:latest
The above command pulls the latest Redis image (if not already present) and runs it in detached mode. Because we didn't specify the --name option, Docker will assign a default name to this container. List the running containers to identify the default name:
docker container ls
The output:
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS       NAMES
1234567890ab   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds    6379/tcp    ecstatic_einstein
In this example, Docker has named our Redis container ecstatic_einstein.

Listing Containers 

Before renaming, it's a good idea to take stock of your existing containers. To list all your containers (running and stopped):
docker container ls -a

This command will present you with details like container ID, image name, status, and, importantly, the container's current name.

Renaming the Container 

Docker provides a direct command to rename containers. Let's say you have a container named old_name and you want to rename it to new_name. Here's how you go about it:

docker rename old_name new_name

It's a good practice to ensure that the new name you're assigning isn't already in use by another container, to avoid naming conflicts.

One more example: To rename the existing container named springboot_old_container to springboot_new_container:
docker rename springboot_old_container springboot_new_container

Verify the Rename 

After renaming, it's always a good practice to verify the change:

docker container ls -a

Points to Remember 

Container Status: A container can be renamed irrespective of its status – whether it's running, paused, or stopped. 

Active Connections: If you're connected to a container through an interactive session and you rename it, the session will remain unaffected. 

Consistency: If you have scripts or automated workflows that reference the container by its name, remember to update them after renaming to ensure continuity

Conclusion 

In this guide, we saw how to use the docker rename command to rename the existing container.

Related Container Management Guides

Comments