Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Created December 11, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetutlage/a32731df7ea5dbc9f93b17bb3d203c03 to your computer and use it in GitHub Desktop.
Save thetutlage/a32731df7ea5dbc9f93b17bb3d203c03 to your computer and use it in GitHub Desktop.
Making `docker-compose exec` faster by proxying it via `docker exec`. Courtesy (https://github.com/docker/compose/issues/4748#issuecomment-561438269)
#!/usr/bin/env bash
docker-compose-exec() {
local ARG=''
local ARGS=''
local COUNT=0
local DIR=$(basename $(pwd))
for ARG in "$@"; do
case $ARG in
-*)
ARGS="$ARGS $ARG"
;;
*)
# first positional arg is container name, replace with container id
if [ $COUNT == 0 ]; then
ARG=$(docker ps -q --filter "name=${DIR}_${ARG}")
fi
ARGS="$ARGS $ARG"
COUNT=$((COUNT+1))
;;
esac
done
docker exec -it $ARGS
}
docker-compose-exec "$1" "$2"
@thetutlage
Copy link
Author

Use it as

./docker-exec.sh app "npm install some-package"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment