Skip to content

Instantly share code, notes, and snippets.

@arctic-hen7
Last active April 15, 2021 23:41
Show Gist options
  • Save arctic-hen7/b6e2ec11212a6a7a4805deff689f7f3e to your computer and use it in GitHub Desktop.
Save arctic-hen7/b6e2ec11212a6a7a4805deff689f7f3e to your computer and use it in GitHub Desktop.
Script to get the ID of a Docker container by its name
#!/bin/bash
containername=$1
# Feed the list of currently running containers into an Awk script that declares the container name a variable and then gets the outputs of running it as a regex pattern, printing the first word
id=$(docker ps | awk -v containername="$containername" '$0 ~ containername{print $1}');
# Check if there is an image ID or not
if [[ -z $id ]]; then
echo "No images match that pattern. Try broadening your search, you can enter regex patterns to this command to if you'd like, but be aware of Awk syntax.";
else
echo "$id";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment