-
-
Save ibolmo/44fdef6bf8f3e775d8d5252caa454aa1 to your computer and use it in GitHub Desktop.
dshell: A little wrapper around the docker-compose run command that intelligently gets you a shell inside a container
This file contains 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 | |
CONTAINER_ID=`docker ps -f status=running -f name=web_run -n 1 | head -n 2 | tail -n 1 | cut -d' ' -f1` | |
if [[ "${CONTAINER_ID:0:9}" = "CONTAINER" ]]; then | |
echo "No container exists, creating it (hit enter after a sec to bring up terminal)..." | |
docker-compose run --service-ports web /bin/bash | |
exit 0 | |
fi | |
STATUS=`docker inspect --format='{{json .State.Status}}' $CONTAINER_ID` | |
echo -n "Found $CONTAINER_ID and it is currently $STATUS. " | |
if [[ "$STATUS" = '"running"' ]]; then | |
echo "Shelling in..." | |
else | |
echo "Starting and shelling in..." | |
docker start $CONTAINER_ID | |
fi | |
if [[ "$#" -ne 0 ]]; then | |
docker exec -i -t $CONTAINER_ID /bin/bash -c "$@" | |
else | |
docker exec -i -t $CONTAINER_ID /bin/bash | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be aware that this script is currently broken and doesn't execute the command you pass after running the container.