When should I use Docker's container name?

When I ran docker ps -a, I got

CONTAINER ID        IMAGE                         COMMAND                CREATED             STATUS                      PORTS               NAMES
e3be2faeb751        centos:latest   touch /var/log/test   2 minutes ago       Exited (1) 2 minutes ago                        insane_kirch6

What is the name, insane_kirch6, for?


Solution 1:

You can name your own containers with --name when you use docker run. If you do not provide a name, Docker will generate a random one like the one you have.

Check their documentation for naming at Legacy container links, The importance of naming

Solution 2:

And even more importantly, you can run named containers again later with start:

docker start --interactive named-containter

Solution 3:

Not only for visibility, but it also can be used as container_id,
in docker commands like start, stop, exec, rm, ...

When you want to run a command in an existing container (running or exited), you will identify the container either by name or container_id.

Examples:

Create a container named qqqq and start a process "sleep" 1 minute, and then exit.

$ docker run --name qqqq ubuntu sleep 60

Run another command in the container qqqq:

$ docker exec qqqq ps -aef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:21 ?        00:00:00 sleep 60
root        11     0  3 04:21 ?        00:00:00 ps -aef

Delete the container qqqq:

$ docker rm qqqq
qqqq