Skip to content

Instantly share code, notes, and snippets.

@ibolmo
Forked from bcooksey/dshell
Last active September 12, 2017 18:14
Show Gist options
  • Save ibolmo/44fdef6bf8f3e775d8d5252caa454aa1 to your computer and use it in GitHub Desktop.
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
#!/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
@FokkeZB
Copy link

FokkeZB commented Sep 6, 2017

Be aware that this script is currently broken and doesn't execute the command you pass after running the container.

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