AndyJarrett

How to poke around your Docker container with Bash

TL:DR docker exec -t -i $(docker ps -lq) /bin/bash

For the full explanation; I needed to run tail -f on a log file within my docker container recently. I could of added SSH to the DOCKERFILE but I just didn't want to add unnecessary functionality for some rare instances when I need to inspect the guts.

Thats when I came across docker exec and docker exec [OPTIONS] CONTAINER COMMAND [ARG...], by running this I can run a new command in a running container.

docker exec -t -i container_name_or_id /bin/bash  

This will create a new Bash session in the container container_name_or_id which you can get by running docker ps -l for the latest running containers.

If you want something more dynamic you can take this further combinging it with $(docker ps -lq) which will return just the ID for you.

docker exec -t -i $(docker ps -lq) /bin/bash