Quick development envs with Docker
Very often, we end up setting up an entirely different machine for testing our stuff. Of late, I’ve been using docker on Ubuntu 18.04 for testing individual environments for various projects I work on.
That way, when things finally make into production, I am sure that I’ve not forgotten to handle dependencies, configuration parameters required etc.
An added advantage is when you decide upon release version names. For each release, you’ve an environment with version name as environment name. Later, when you want to make a hotfix on a branch of a version, just spin up that container to bring back your test bed.
So, what are the two key requirements to get this done?
- Always use named containers _(despite docker’s random names for unnamed containers being quite entertaining)__.
- Just check for presence of named container and start it if it exists.
docker containers exit once the process you started with the container exits. Essentially, the trick is to have a name first instead of having to always look at container ids.
Making a container
This will setup a container with the name given and drop you into bash shell.
Let us try installing sqlite3
; just to prove the point of reusing and sharing the files across host and container.
Now, I can see test.db in $HOME/code/helloworld folder in my host. I use whatever editor (VIM, if you ask) on my host and do the version control stuff (git or fossil) also from there. But for executing and testing my work, I use the container.
Reusing the container
&&
Collecting all to a script
#!/bin/bash
# Quick script to manage latest ubuntu docker containers
# Useful for spinning up machines for development
# args:
# name-of-machine
# make|start|stop
# make > sharedfolderhere:target
CONTAINER=
OP=
if [; then
# do an ifconfig to see which is your etherner interface and
# change it from eno1 below
fi
if [; then
#check if it is already there
FOUND=""
FOUND=
if [; then
else
fi
fi
if [; then
fi
And just use it like below:
If you really don’t want this container, you can always manually remove the container like:
To transport to other machines, docker export
and docker import
should work; I’ve not yet tried that yet.
PS: This article was posted in medium.com as well.