简体   繁体   中英

Command line shortcut to connect to a docker container

Is there any shortcut command to connect to a docker container without running docker exec -it 'container_id' bash every time?

Here is a shorter command line shortcut to:

  1. Check if a container is running
  2. If running, connect to a running container using docker exec -it <container> bash command:

Script docker-enter :

#!/bin/bash

name="${1?needs one argument}"

containerId=$(docker ps | awk -v app="$name:" '$2 ~ app{print $1}')

if [[ -n "$containerId" ]]; then
    docker exec -it $containerId bash
else
    echo "No docker container with name: $name is running"
fi

Then run it as:

docker-enter webapp

I'm using the following alias on OS X:

alias dex='function _dex(){ docker exec -i -t "$(basename $(pwd) | tr -d "[\-_]")_$1_1" /bin/bash -c "export TERM=xterm; exec bash" };_dex'

In the same directory as my docker-files, I run "dex php" to enter the PHP container.

If random id is complicated. Start container with name docker run --name test image and connect with its name docker exec -it test bash .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM