Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Container ID of docker process to attach

Tags:

docker

On my remote server, some developers run the same docker images named "my_account/analysis". So, once detached from the docker process, it is struggling to know which is my own process.

The result of docker ps is like this:

CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
6968e76b3746        my_account/analysis             "bash"                   44 hours ago        Up 44 hours         6023/tcp, 6073/tcp, 6800/tcp, 8118/tcp, 8888/tcp, 9050/tcp, 0.0.0.0:8887->8887/tcp           modest_jones
42d970206a29        my_account/analysis             "bash"                   7 days ago          Up 7 days           6023/tcp, 6073/tcp, 6800/tcp, 8118/tcp, 8888/tcp, 9050/tcp, 0.0.0.0:32771->8885/tcp          gallant_chandrasekhar
ac9f804b7fe0        my_account/analysis             "bash"                   11 days ago         Up 11 days          6023/tcp, 6073/tcp, 6800/tcp, 8118/tcp, 8888/tcp, 9050/tcp, 0.0.0.0:8798->8798/tcp           suspicious_mayer
e8e260aab4fb        my_account/analysis             "bash"                   12 days ago         Up 12 days          6023/tcp, 6073/tcp, 6800/tcp, 8118/tcp, 8888/tcp, 9050/tcp, 0.0.0.0:32770->8885/tcp          nostalgic_euler

In this case, because I remember that I ran docker around 2 days ago, I attach my container by docker attach 6968e. However, usually we forgot this.

What is the best practice to detect the container ID of mine under the situation that there are a lot of containers with the same Image name?

like image 329
Light Yagmi Avatar asked Nov 15 '25 09:11

Light Yagmi


1 Answers

The simple way is to name the containers

docker run --name my-special-container my_account/analysis
docker attach my-special-container

You can store the container ID in a file when it launches

docker run --cidfile ~/my-special-container my_account/analysis
docker attach $(cat ~/my-special-container)

You can add more detailed metadata with object labels, but they are not as easily accessible as names

docker run --label com.rkjt50r983.tag=special my_account/analysis
docker ps --filter 'label=com.rkjt50r983.tag=special' 
like image 65
Matt Avatar answered Nov 18 '25 16:11

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!