Monday, June 1, 2015

starting or exec'ing a new container

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 fast
  • shared source / data / certs / config (with or without a data container)
  • able to build my go project
  • idiomatic go structure
  • able to connect with multiple terminals to the same instance without having to run the ssh server
The script below launches(run) my container if it's not already running and attaches(exec) if the container is already running.




#!/bin/sh
imgname=devbox
boxname=devbox
shellname=bash
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}
else
echo "running a fresh ${boxname} instance"
docker run --rm -it -v /data/data1/devbox/shared/:/var/shared/ --name=${boxname} ${imgname} /bin/${shellname}
fi

No comments:

Post a Comment

Fire OS and Android

The relationship between Amazon's FireOS and Google's Android OS is probably very complicated. And while there is probably some very...