Skip to content
Snippets Groups Projects

Docker Container IP

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Hermann Mayer
    Edited
    docker-id 432 B
    #!/bin/bash
    #
    # Find a container/pod id by fuzzy name/id.
    
    if [ -z "${1}" ]; then
      echo 'Container name is missing.'
      exit 1
    fi
    
    # $1-n - the regex (pcre) pattern
    function filter()
    {
      docker ps --format '{{.ID}}|{{.Names}}' \
        | grep -P "${@}"
    }
    
    if [ $(filter "${@}" | wc -l) -gt 1 ]; then
      filter "${@}" --color=always >&2
      echo >&2
      echo "Error: Ambiguous match for: ${@}" >&2
      exit 1
    fi
    
    filter "${@}" | cut -d '|' -f1
    docker-ip 333 B
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment