Issue
Running docker containers in detached mode (docker run -d foo) returns the container id (long version). But can it return the (random generated) container name? Since the command reference does not indicates such a functionality, I tried using xargs to pipe the returned id into a docker ps command:
docker run -d -p 8080:8080 container-name | xargs -I % docker ps --format '{{.Names}}' --filter id=%
This does not work because either the id is not really returned but only printed by docker run or because I do not use xargs correctly.
Solution
docker run -d -p 8080:8080 container-name | \
xargs docker container inspect -f '{{ slice .Name 1 }}'
This uses docker container inspect instead of docker ps because you already know exactly which container you want to query. The name of the container is under the .Name field, but it has a leading / that I strip off with slice .Name 1 which is the same as Name[1:] in Go.
Answered By - BMitch Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.