When should I use Docker's container name?

Docker

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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 - Docker

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

docker start --interactive named-containter

Solution 3 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTLJView Question on Stackoverflow
Solution 1 - DockerMarcus HughesView Answer on Stackoverflow
Solution 2 - DockermrtsView Answer on Stackoverflow
Solution 3 - Dockerosexp2003View Answer on Stackoverflow