How to Rename a Docker Container?
The docker container name is a unique name identity given by the docker engine.
The docker provides the facility to rename the docker container name using the rename option.
Lets take a look at the following syntax:
# docker rename <CONTAINER-NAME | CONTAINER-NAME ID > <NEW-CONTAINER-NAME>
vamshi@node03:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e3de85aaf61c nginx "nginx -g 'daemon ..." 1 month ago Up 52 seconds 80/tcp jovial_hertz
e3de85aaf61c -> The dynamically generated container id
nginx -> The Image used to invoke the container.
jovial_hertz -> A name given randomly by docker to container when it was started.
The rename command in action:
[vamshi@node01 ~]$ sudo docker rename jovial_hertz nginx_linuxcent
We now list the output from the docker ps command
[vamshi@node01 ~]$ vamshi@node03:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e3de85aaf61c nginx "nginx -g 'daemon ..." 1 month ago Up 52 seconds 80/tcp nginx_linuxcent
You can also choose to rename a docker container while it is in running or stopped or exited state ay other states.
To Rename a docker container by identifying the container id
[vamshi@node01 ~]$ docker ps | grep “sleep”
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
177b12c2a37c busybox “sh sleep 100″ 2 weeks ago Exited (2) 2 weeks ago eager_wiles
[vamshi@node01 ~]$ docker rename 177b12c2a37c sleeping_geek
[vamshi@node01 ~]$ docker ps -af status=”exited” | grep sleepi -i
177b12c2a37c busybox “sh sleep 100” 2 weeks ago Exited (2) 2 weeks ago sleeping_geek
Some common errors:
vamshi@node03:~$ sudo docker rename stoic_knuth nginx_linuxcent
Error response from daemon: Error when allocating new name: Conflict. The container name “/nginx-test” is already in use by container “e3de85aaf61cb0641280b91772baa3477c896fc32d604d91ad855becf56c507f”. You have to remove (or rename) that container to be able to reuse that name.
Error: failed to rename container named stoic_knuth
You cannot rename more than once container with the same name. Container name is unique bit of identity and has to be unique: