Skip to content

Instantly share code, notes, and snippets.

@studiotomi
Created June 12, 2019 14:59
Show Gist options
  • Save studiotomi/4dd8e690fe9079af409f7ece9ee02852 to your computer and use it in GitHub Desktop.
Save studiotomi/4dd8e690fe9079af409f7ece9ee02852 to your computer and use it in GitHub Desktop.
Given a string, find id of matching docker container, and return id and name or execute arbitrary command.
#!/bin/bash
function containerCommand() {
# Given a string, search for the matching running docker containers.
# If more than one argument is passed, attempt to run remaning arguments
# on that docker container.
container=`docker ps --format '{{.ID}} {{.Names}}'1`;
shift;
if [[ ! -z "$@" ]]
then
container=`echo -n "${container}" | cut -f1 -d\ `
docker exec -it $container $@;
else
echo "${container}";
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment