Richard Bucker

starting or exec'ing a new container

Posted at — Jun 1, 2015

I’m not going to elaborate on the problem too much other than to describe the challenge and then provide my script that solved it.

I’m using CoreOS(685.0.0) + Docker(1.6.2) in order to host my own development environment. The benefits are already well known… a consistent environment for my code and testing. There are a number of real challenges getting to a shell prompt and not blowing out my disk space as I create more layers and images of the same sandbox.

Here are some of my requirements:

The script below launches(run) my container if it’s not already running and attaches(exec) if the container is already running.

CONTAINER_ID=docker ps -q --filter "name=${boxname}"
RUNNING=docker inspect --format="{{.State.Running}}" ${CONTAINER_ID:-"NOTRUNNING"} 2> /dev/null

if [ “$RUNNING” == “true” ]; then
echo “connecting to exiting ${boxname}”
docker exec -i -t ${boxname} ${shellname}
echo “running a fresh ${boxname} instance”
docker run –rm -it -v /data/data1/devbox/shared/:/var/shared/ –name=${boxname} ${imgname} /bin/${shellname}