Skip to content

Instantly share code, notes, and snippets.

@shkm
Created August 27, 2021 10:12
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 shkm/650c6313b1b6476fc7ff05cb537e067e to your computer and use it in GitHub Desktop.
Save shkm/650c6313b1b6476fc7ff05cb537e067e to your computer and use it in GitHub Desktop.
dcsh: Shell into docker container
#!/usr/bin/env bash
# Tries to open a shell in the main docker container (e.g. it ends in 'app' or 'api')
# Given a param, uses that as a pattern to look for at the end of the container name.
#
# Usage:
#
# dcsh <container_pattern>
set -o errexit # Exit on most errors (see the manual)
set -o errtrace
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline
PATTERN="${1:-ap[pi]}"
container=$(docker-compose ps | tail -n +3 | cut -d' ' -f1 | grep "${PATTERN}_[0-9]\+" | sed 's/.*_\(.*\)_.*/\1/')
echo "Container: $container"
docker-compose exec $container sh -c "command -v bash >/dev/null 2>&1 && bash || sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment