Last active
April 14, 2019 17:31
-
-
Save eliashaeussler/b9ba9b54c63a22d429bd2f64811177ee to your computer and use it in GitHub Desktop.
Bash into Docker container (with local aliases) using docker-compose
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 | |
set -e | |
CONTAINER="${1-server}" | |
COMMAND="$2" | |
CONTAINER_ID="$(docker-compose ps -q "$CONTAINER")" | |
if [[ $(docker-compose ps --services | grep "$CONTAINER") ]]; then | |
COMPOSE_COMMAND="exec" | |
else | |
COMPOSE_COMMAND="run" | |
fi | |
# Fallback to bash with aliases, if no command is given | |
if [[ -z "$COMMAND" ]]; then | |
echo "No command set, using bash with aliases." | |
LOCAL_RCFILE="$HOME/.bashrc" | |
TEMP_RCFILE="/tmp/.bashrc_temp" | |
COMMAND="bash --rcfile $TEMP_RCFILE" | |
docker cp "$LOCAL_RCFILE" "$CONTAINER_ID":"$TEMP_RCFILE" | |
fi | |
# Shift container name and command name arguments | |
set +e | |
shift | |
shift | |
set -e | |
docker-compose $COMPOSE_COMMAND $CONTAINER $COMMAND $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment