Quick Docker Exec

10 February, 2022

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 to write a small bash script to make this process less annoying. You can check it out here.

Features

Script 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'

Example output

# 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