Get docker container id from container name

后端 未结 9 1663
無奈伤痛
無奈伤痛 2020-12-12 11:48

What is the command to get the docker container id from the container name?

相关标签:
9条回答
  • 2020-12-12 12:00

    The simplest way I can think of is to parse the output of docker ps

    Let's run the latest ubuntu image interactively and connect to it

    docker run -it ubuntu /bin/bash
    

    If you run docker ps in another terminal you can see something like

    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    8fddbcbb101c        ubuntu:latest       "/bin/bash"         10 minutes ago      Up 10 minutes                           gloomy_pasteur
    

    Unfortunately, parsing this format isn't easy since they uses spaces to manually align stuff

    $ sudo docker ps | sed -e 's/ /@/g'
    CONTAINER@ID@@@@@@@@IMAGE@@@@@@@@@@@@@@@COMMAND@@@@@@@@@@@@@CREATED@@@@@@@@@@@@@STATUS@@@@@@@@@@@@@@PORTS@@@@@@@@@@@@@@@NAMES
    8fddbcbb101c@@@@@@@@ubuntu:latest@@@@@@@"/bin/bash"@@@@@@@@@13@minutes@ago@@@@@@Up@13@minutes@@@@@@@@@@@@@@@@@@@@@@@@@@@gloomy_pasteur@@@@@@
    

    Here is a script that converts the output to JSON.

    https://gist.github.com/mminer/a08566f13ef687c17b39

    Actually, the output is a bit more convenient to work with than that. Every field is 20 characters wide. [['CONTAINER ID',0],['IMAGE',20],['COMMAND',40],['CREATED',60],['STATUS',80],['PORTS',100],['NAMES',120]]

    0 讨论(0)
  • 2020-12-12 12:07

    In Linux:

    sudo docker ps -aqf "name=containername"
    

    Or in OS X, Windows:

    docker ps -aqf "name=containername"
    

    where containername is your container name.

    To avoid getting false positives, as @llia Sidorenko notes, you can use regex anchors like so:

    docker ps -aqf "name=^containername$"
    

    explanation:

    • -q for quiet. output only the ID
    • -a for all. works even if your container is not running
    • -f for filter.
    • ^ container name must start with this string
    • $ container name must end with this string
    0 讨论(0)
  • 2020-12-12 12:08
    1. Get container Ids of running containers ::

      $docker ps -qf "name=IMAGE_NAME"
      
          -f: Filter output based on conditions provided
          -q: Only display numeric container IDs
      
    2. Get container Ids of all containers ::

      $docker ps -aqf "name=IMAGE_NAME"
      
          -a: all containers
      
    0 讨论(0)
  • 2020-12-12 12:18

    If you want to get complete ContainerId based on Container name then use following command

     docker ps --no-trunc -aqf name=containername
    
    0 讨论(0)
  • 2020-12-12 12:19

    I tried sudo docker container stats, and it will give out Container ID along with details of memory usage and Name, etc. If you want to stop viewing the process, do Ctrl+C. I hope you find it useful.

    0 讨论(0)
  • 2020-12-12 12:22

    In my case I was running Tensorflow Docker container in Ubuntu 20.04 :Run your docker container in One terminal , I ran it with

    docker run -it od
    

    And then started another terminal and ran below docker ps with sudo:

    sudo docker ps
    

    I successfully got container id:

    CONTAINER ID        IMAGE               COMMAND             CREATED             
    STATUS              PORTS               NAMES
    e4ca1ad20b84        od                  "/bin/bash"         18 minutes ago      
    Up 18 minutes                           unruffled_stonebraker
    
    0 讨论(0)
提交回复
热议问题