Last active
April 15, 2021 23:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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