Blog About
I use Docker a lot, and have always found it annoying that in order to run exec commands, i first need to use docker ps
to look up the ID for the contrainer i’m trying to interact with.
I decided expedite this process by writing a handly little bash script, which you can check out here.
/bin/bash
and /bin/sh
shells in that orderScript parses all of the arguments given to it into a single exec
command - no quotes necessary!
quickdocker.sh /bin/bash -c 'ps aux'
# will turn into -->
docker exec -it <POD ID> /bin/bash -c 'ps aux'
# no arguments provided, tries acquiring shells in order
$ bash quickdocker.sh
CONTAINER ID IMAGE COMMAND STATUS PORTS
1) dab88fc067db ubuntu:latest "bash" Up 32 minutes
2) 61be819285e7 beanmaster "/entrypoint.sh" Up 16 minutes
3) e408febcc43d 835490db8cd7 "/docker-entrypoint.…" Up 54 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp
Container to exec: 2
Running /bin/bash
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
/bin/bash not available in container
Running /bin/sh
~ #
# arguments provided, attempts running them as exec command
$ bash quickdocker.sh /bin/bash -c "hostname -I"
CONTAINER ID IMAGE COMMAND STATUS PORTS
1) dab88fc067db ubuntu:latest "bash" Up 35 minutes
2) 61be819285e7 beanmaster "/entrypoint.sh" Up 19 minutes
3) e408febcc43d 835490db8cd7 "/docker-entrypoint.…" Up 54 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp
Container to exec: 1
Running '/bin/bash -c hostname -I'
172.17.0.5
This admittedly simple bash script has really improved my Docker workflow, and i hope you find it useful as well.
OUTPUT
array’s indexes instead of relying on wc -l
to get that information