Skip to content

Instantly share code, notes, and snippets.

@nielsonsantana
Last active July 5, 2022 13:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielsonsantana/282953fdef662ee28c8404ed6c3ecf93 to your computer and use it in GitHub Desktop.
Save nielsonsantana/282953fdef662ee28c8404ed6c3ecf93 to your computer and use it in GitHub Desktop.
Docker Shortcut to singin on docker container by name
#!/bin/bash
DOCKER_PS_LINE=`docker ps | awk '{print $1,$2,$NF}' | grep -m 1 $1`
CONTAINER_NAME=`echo $DOCKER_PS_LINE | awk '{print $2}'`
CONTAINER_ID=`echo $DOCKER_PS_LINE | awk '{print $1}'`
if [ -n "$CONTAINER_ID" ]; then
echo "Logged in: $CONTAINER_NAME"
docker exec -it $CONTAINER_ID bash
else
echo "No container found for query: '$1'"
fi
@nielsonsantana
Copy link
Author

nielsonsantana commented May 8, 2018

dockerenter

Shortcut to enter on docker container by name

Install

curl -L https://gist.githubusercontent.com/nielsonsantana/282953fdef662ee28c8404ed6c3ecf93/raw/a501706c3368a59952f7f0a67464132fdb773b5a/dockerenter.sh > /usr/local/bin/dockerenter
chmod +x /usr/local/bin/dockerenter

Usage

This command search by the container named web and connect using docker exec.

dockerenter web

@beshkenadze
Copy link

Here is my two cents, not every container have bash shell:

DOCKER_PS_LINE=`docker ps | awk '{print $1,$2,$NF}' | grep -m 1 $1`
CONTAINER_NAME=`echo $DOCKER_PS_LINE | awk '{print $2}'`
CONTAINER_ID=`echo $DOCKER_PS_LINE | awk '{print $1}'`

if [ -n "$CONTAINER_ID" ]; then
    echo "Logged in: $CONTAINER_NAME"
    docker exec -it $CONTAINER_ID sh -c "(bash || ash || sh);"
else
    echo "No container found for query: '$1'"
fi

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