Can I do Better: Connecting to Docker Containers
I like to improve my work flow by asking: “Can I do it better?” and see where that leads.
This time, I will walk you through a situation I encounter in a daily basis: connecting to a running Docker container and the show solution I use to turn a four step manual process into a single line.
If you are connecting to Docker containers frequently, this solution can help you be more efficient and focus on more important parts of the problem.
This article will take you less than three minutes to read.
I connect to running containers to do work or debug. The command I use connect to a container is:
Before executing this command, I need to look up the
SHA. I don’t know what the container SHA is, it could be the same
as the last time I connected to it, or it could be different.
To look up the container’s SHA, I run
docker ps to list all the
running containers and their SHA. Find the container running the image
I want, say
foo, take that SHA value and execute the above command.
Step by step of the above with details to connect to
Get a list of running containers running command:
sudo docker ps
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8996065500e ubuntu "/bin/bash" 4 seconds ago Up 2 seconds clever_goldstine 5bb3e80c076d nginx:latest "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp objective_hermann.1.6agqbzmx8qiazhwwncdegknwa f8ead854815d registry:latest "/entrypoint.sh /etc…" 44 seconds ago Up 42 seconds 5000/tcp registry.1.4efrmjz58hqjdtmhdqhpg4q0z v 2df0bba15bc6 foo "nginx -g 'daemon ..." 47 seconds ago Up 46 seconds 80/tcp sad_boyd
From that list, pick out line running image named ‘foo’
2df0bba15bc6 foo "nginx -g 'daemon ..." 47 seconds ago Up 46 seconds 80/tcp sad_boyd
From that line, pick out the container’s SHA
Remember container SHA, connect to container using original command.
% sudo docker exec -it 2df0bba15bc6 bash
Isn’t this simple?
Honestly, it was, and then it got tedious after the
n-th time, where
n is greater than ten. Especially at the wee-hours and I need to get
something done and go to bed.
It’s easy to fat finger the container SHA in the last command, causing more grief. Copy/paste is nice, but I still need to target the right SHA to copy.
In this process, there are four extra steps between logging in to host and goal of doing work in container (and sometimes: going to bed!)
Can I do better?
n+1 time, I figured out a better way, much better
way. This is my solution to connect to container named ‘foo’ and run
Explanation of key parts:
||executes another command within a command - using that result in place|
||passes output from left command as input into right command|
||grabs line that only match ‘foo’|
||picks out the first ($1) column and uses that|
Whenever I need to connect to a docker container named ‘foo’, I just run the above command after logging in.
- fat fingering
- more than one step needed
This command is easier to run in the wee-hours than the previous method. I can get in and out of a docker container easily.
Added benefit: it’s easy to find this command in the bash history (or make an alias!).
Instead of continuing to accept a four-step process in my work flow, asking “Can I do better?” dramatically reduced the process into a single line. Letting me work on more important items while connected to a Docker container.
Is there a multi-step process in your life that you ask: “Can I do better?” If there is and you can’t find a way to make it better, please contact me. I would love to see if we can solve it together.