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:launch fastshared source / data / certs / config (with or without a data container)able to build my go projectidiomatic go structureable to connect with multiple terminals to the same instance without having to run the ssh serverThe script below launches(run) my container if it’s not already running and attaches(exec) if the container is already running.#!/bin/shimgname=devboxboxname=devboxshellname=bashCONTAINER_ID=docker ps -q --filter "name=${boxname}"RUNNING=docker inspect --format="{{.State.Running}}" ${CONTAINER_ID:-"NOTRUNNING"} 2> /dev/nullif [ “$RUNNING” == “true” ]; thenecho “connecting to exiting ${boxname}"docker exec -i -t ${boxname} ${shellname}elseecho “running a fresh ${boxname} instance”docker run –rm -it -v /data/data1/devbox/shared/:/var/shared/ –name=${boxname} ${imgname} /bin/${shellname}fi