PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How to return random generated name instead of ID when starting docker container

 October 18, 2022     docker, shell     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing