When should I use Docker's container name?

2020-05-22 04:07发布

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
3条回答
萌系小妹纸
2楼-- · 2020-05-22 04:42

Not only for visibility, but it also can be used as container_id in the exec or rm command.

When you want to run a command in an existing container (running or exited), you will use the "docker exec" command in which you specify the container name or id.

Examples:

Create a container named qqqq and start a process "sleep" 1 minutes, 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
查看更多
SAY GOODBYE
3楼-- · 2020-05-22 04:54

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

docker start --interactive named-containter
查看更多
Juvenile、少年°
4楼-- · 2020-05-22 04:56

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

查看更多
登录 后发表回答