How to use docker container rename command to rename a container

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker containers by renaming them. You will start by creating a Docker container and assigning it a specific name for easy identification.

Following the creation, you will verify the container's name using Docker commands. The core of this lab involves using the docker container rename command to change the container's name, and finally, you will verify that the renaming was successful by checking the container's new name.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555118{{"How to use docker container rename command to rename a container"}} docker/ps -.-> lab-555118{{"How to use docker container rename command to rename a container"}} docker/pull -.-> lab-555118{{"How to use docker container rename command to rename a container"}} end

Create a container with a specific name

In this step, you will learn how to create a Docker container and assign a specific name to it. Naming containers makes them easier to identify and manage.

First, let's pull the hello-world image from Docker Hub. This is a very small image that is useful for testing.

docker pull hello-world

You should see output indicating that the image is being pulled and downloaded.

Now, we will run a container from the hello-world image and give it the name my-hello-container. We use the docker run command with the --name flag to specify the container name.

docker run --name my-hello-container hello-world

When you run this command, Docker will create and start a new container named my-hello-container based on the hello-world image. The hello-world container simply prints a message and then exits.

After the container runs, you can verify that it was created by listing all containers (including exited ones) using the docker ps -a command.

docker ps -a

In the output, you should see a container with the name my-hello-container.

Verify the container name

In the previous step, you created a Docker container and named it my-hello-container. In this step, you will explicitly verify that the container exists and has the correct name using Docker commands.

The docker ps command is used to list containers. By default, it only shows running containers. Since the hello-world container exits after running, we need to use the -a flag to show all containers, including those that have exited.

Run the following command to list all containers:

docker ps -a

Look at the output of the command. You should see a table with information about your containers. Find the row corresponding to the container you just created. The NAMES column in this table shows the name of the container. Verify that the name is indeed my-hello-container.

To specifically check for the existence of a container with a particular name, you can combine docker ps -a with grep.

docker ps -a | grep my-hello-container

If the container named my-hello-container exists, this command will output the line from docker ps -a that contains the container's information. If it doesn't exist, grep will not find a match, and there will be no output. This is a common way to programmatically check for the existence of a container by name.

Rename the container

In the previous steps, you created and verified the name of a Docker container. Sometimes, you might need to change the name of an existing container. Docker provides the docker rename command for this purpose.

The docker rename command takes two arguments: the current name of the container and the new name you want to assign.

We will now rename the container currently named my-hello-container to renamed-hello-container.

docker rename my-hello-container renamed-hello-container

If the command is successful, it will complete without printing any output. This indicates that the container has been successfully renamed.

To confirm the renaming, you can again list all containers using docker ps -a and check the NAMES column.

docker ps -a

You should now see renamed-hello-container in the NAMES column for the container you just renamed, and my-hello-container should no longer appear.

Verify the new container name

In the previous step, you renamed the Docker container from my-hello-container to renamed-hello-container. In this final step, you will perform a definitive check to ensure that the container now has the new name and that the old name is no longer associated with it.

We will use the docker ps -a command again to list all containers and their details.

docker ps -a

Examine the output carefully. You should see the container listed with the name renamed-hello-container. It is also important to confirm that there is no container listed with the old name, my-hello-container.

To specifically verify the presence of the new name and the absence of the old name using command-line tools, you can use grep.

First, check for the new name:

docker ps -a | grep renamed-hello-container

This command should return output if the container was successfully renamed.

Next, check for the old name. This command should not return any output if the renaming was successful.

docker ps -a | grep my-hello-container

By performing both checks, you can be confident that the container has been correctly renamed.

Summary

In this lab, you learned how to create a Docker container and assign it a specific name using the docker run --name command. You then verified the container's existence and name using the docker ps -a command, which lists all containers, including those that have exited.

Following the creation and verification, you practiced renaming the container using the docker container rename command, changing its name from my-hello-container to a new name. Finally, you confirmed the successful renaming by again using docker ps -a to check the updated container name in the output.