How to rename docker container? [SOLVED]


Written By - Ashwini Kanitkar
Advertisement

How to identify docker container?

The docker is one of the famous and most used containerization tools by organizations in order to build, and deploy their applications in various environments.

When you run the docker containers, the system automatically gives a UUID number to each container in order to avoid naming conflicts. In this article, let us understand how to name and rename the docker containers.

The docker container is identified by the following:

Long UUID for example -

5973788c3dcab6b64e9a981b6cfe0d37c60337119af379daf4533ea67c9bc486

Short UUID for example -

5973788c3dca

A name -

competent_germain

 

NOTE:

If there is no name specified while running the docker container, the Docker daemon assigns a random name to it along with a UUID.

You can rename the docker container in two ways.

Advertisement
  1. Rename a container by the name
  2. Rename a container by ID

 

Assign a name to the docker container

In this section let us understand how to name a Docker Container using the --name option with the docker run command.

$ docker run -d --name testubuntu -t ubuntu:test

Output

4b3a8142789cb2621b153a825cf8f64a5fb84ed1c0e58e2549b71d922b7bcb5a

Now let us see the list of docker containers and you can see that the docker container with the name “testubuntu” has been created successfully.

CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES
4b3a8142789c   ubuntu:test   "tail -f /dev/null"      38 seconds ago      Up 35 seconds                testubuntu

Having a name specified to your docker container, it helps the developer use the name directly and perform the docker tasks instead of using the docker container ID.

Example

You can see that the docker operations can be performed using the docker container name

$ docker restart  testubuntu
$ docker start  testubuntu
$ docker stop testubuntu

 

Method-1: Using the docker rename command

In this section let us understand how to rename Docker Container. The Docker provides a command to rename the docker container. You can use this command while the container is in any of the states. However, the running containers may receive the updated name only after it is restarted.

Syntax

Advertisement
docker rename CONTAINER NEW_NAME

Let us check this with an example. Let us rename our ubuntu container using the docker rename command. Firstly, let us check the status of the docker containers

CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES
5973788c3dca   ubuntu        "sleep infinity"         19 minutes ago      Up 19 minutes                competent_germain

Now, let us rename the docker container competent_germain with ubuntu-test using the command mentioned below

$ docker rename competent_germain ubuntu-test

Let us now see the current status of the docker containers using the docker ps command

CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES
5973788c3dca   ubuntu        "sleep infinity"         21 minutes ago      Up 21 minutes                ubuntu-test

 

Method-2: Using the docker container rename command

You can as well use the other command to rename the docker container. Let us take a look at that in this section.

docker container rename  ubuntu-test ubuntu-test-2

Let us now see the current status of the docker containers.

docker ps

Advertisement
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES
5973788c3dca   ubuntu        "sleep infinity"         28 minutes ago      Up 28 minutes                ubuntu-test-2

NOTE:

The docker container names must be unique otherwise there will be a conflict resulting in an error.

 

Method-3: Rename the docker container using container ID

Let us take a look at renaming the docker container using container ID with an example.

Pull and run the hello-world container using the command mentioned below

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest

 

Run the docker using the docker run command

$ docker run hello-world

Output:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

 

Now, let us see the status of the docker container

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS      NAMES
859a6afc2e7b   hello-world    "/hello"                 12 seconds ago   Exited (0) 10 seconds ago              unruffled_kalam

Now let us rename the container using the container ID of the hello-world image.

$ docker rename 859a6afc2e7b testhello

Let us check the status of the docker container. You can see that the container has been renamed to "testhello"

CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                          PORTS      NAMES
859a6afc2e7b   hello-world    "/hello"                 About a minute ago   Exited (0) About a minute ago              testhello

 

Conclusion

It is essential to name your docker containers so that it's easier to use the name instead of the container ID for troubleshooting purposes. I hope this article helped you understand how to rename the docker containers. For more information please visit the man page of the docker -https://manpages.ubuntu.com/manpages/bionic/man1/docker-container-rename.1.html

In case of any queries please feel free to reach out to us and we will respond at the earliest.

Advertisement

References

https://docs.docker.com/engine/reference/commandline/rename/
https://manpages.ubuntu.com/manpages/bionic/man1/docker-container-rename.1.html

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment