Single Line Program to Access a Docker Container

Description

I have several docker images I have pulled down and running in several containers on my server. As images update I destroy the old containers, remove the old images, and bring up the new images in a new container. Instead of running the following as root:

docker ps
CONTAINER ID        IMAGE                             COMMAND                CREATED             STATUS              PORTS                              NAMES
9ff50d7099b1        zaphinath/apirecisphere:latest    "/nodejs/bin/npm sta   46 hours ago        Up 46 hours         8080/tcp, 0.0.0.0:9800->9800/tcp   gloomy_albattani    
f2104cfaab88        zaphinath/apivietnamdocs:latest   "java -Djava.securit   2 days ago          Up 2 days           0.0.0.0:9700->8080/tcp             romantic_davinci 

You then have to find the container id and execute the following to enter the container if you ever need to debug something.

docker exec -it <docker_id> bash





There is a much easier way to accomplish this. I have a folder called bin in my home directory that contains lots of quick execution scripts and I have one for each of my docker containers. This allows me to access them when I need.

#!/bin/bash
docker exec -it `docker ps | grep recisphere | awk '{print $1}'` bash

Notes

This does not work as an alias because when the .bashrc loads the docker container id is loaded when the bashrc file loads. If the image updates you would have to reload the bashrc source to make an alias work. Having a small bash executables is a good alternative.

Comments are closed.