How can I quickly have a light Linux based environment to do punctual and rapid tests?

Have you ever needed a local Linux based computer for just explore, test or quickly review some piece of software? One example of this could be the need to test the settings of a web server for a site using virtual hosts or reverse proxy directives.

For solving that, one option is to take a physical computer and prepare it with the operating system and all the other required software, but this for sure will take a considerable time and effort, even when disk imaging replication systems are in place. Other option is to use a virtual machine, but it eats a considerable amount of resources from the host machine. And other option is to use a Docker container, which is the quickest and lightest one I've found so far (with other containerization technologies the situation will be similar).

In these situations I use the following Dockerfile as a starting point to build the base image from which containers will be later built:

# Generic image for quick exploration tasks based on Ubuntu Trusty
FROM ubuntu:trusty
MAINTAINER Rodolfo Castellanos
RUN rm -f /etc/apt/apt.conf.d/docker-clean
RUN apt-get update
RUN apt-get install --yes vim tree aptitude bash-completion screen
CMD ["/bin/bash"]
# Some options to create images from this Dockerfile are:
# a) save it with the name Dockerfile and run these commands in a shell:
# cd ~/directory-where-Dockerfile-was-created
# docker build --tag acmeorg/explorationimg .
# b) save it with another name, like QuickExplorationOnUbuntuTrusty, and run these commands
# in a shell:
# cd ~/directory-where-quick-dockerfile-was-created
# docker build --tag acmeorg/explorationimg --file QuickExplorationOnUbuntuTrusty . 

Note that this Dockerfile is not intended to be shared on a site like Docker Hub; it's only for the purpose of helping in the mentioned quick exploration or testing. Some points to highlight are the following:

  • It's fixed to be based on the Ubuntu Trusty distribution to be sure to use the local cached Docker image, once it gets downloaded for the first time.
  • The containers created from the base image of Ubuntu Trusty are tweaked to clear the cache of downloaded APT packages. The instruction RUN rm -f /etc/apt/apt.conf.d/docker-clean removes this tweak, because I want to preserve that cache and reuse it between several containers, even after destroying them, in order to minimize the time required to get those packages from the Internet. Later, when the container is created, we'll see the use of volumes to take advantage of this APT cache. Other people may or may not need the same.
  • With the instruction RUN apt-get install --yes vim tree aptitude bash-completion screen some packages I frequently need are installed: vim for editing files, tree for exploring directories, aptitude for managing the installed packages, bash-completion to reduce the keystrokes needed when typing commands in the Bash shell and, finally, screen allows me bypass the default behavior of Docker containers running only one process and to have several virtual terminals or shells within the container, which is useful to run one or more processes, one per virtual terminal.
  • Each container created from this new Dockerfile, will run a Bash shell when it starts, as instructed with the line CMD ["/bin/bash"]

Being at my host computer, assuming that it has the Docker package installed and ready to be used, and also assuming that the Dockerfile is located in the directory called directorio-dockerfile, the new Docker image can be built in a shell with these commands:

cd ~/dockerfile-directory
docker build -t acmeorg/explorationimg .

The first time it will take some time to download the Ubuntu Trusty base image, but from the second time it will take much less time.

With the image ready, now I can create and run a container within my host computer with a command like this (showed in serveral lines for easy reading):

docker run --interactive --tty \
    -v /home/myusername/docker-things/debs:/var/cache/apt \
    -v /home/myusername/docker-things/apt-lists:/var/lib/apt/lists \
    --name quickcontainer acmeorg/explorationimg

For the container quickcontainer, the volume mounted at /var/cache/apt will allow reusing all the .deb package files existing there and, similarly, the volume mounted at /var/lib/apt/lists will allow reusing all the metadata files about those packages.

This command will be executed almost instantly and when it ends it will offer me an interactive shell to play with. From this point on, I can do whatever I need, for example:

  • Install any another software package (a web server, a database server, a development environment, the runtime of a programming language, etc)
  • Start a screen session to use one virtual terminal per process to execute: one terminal for the web server, another for the database server and several others for interactive execution of commands (editing configuration files, viewing error log files, installing software packages, etc)

When I'm done with my tasks I can exit the container or stop it from the host computer, and in the latter case I would use this command:

docker stop quickcontainer

If later I need to start that same container again, I would use this command:

docker start --attach --interactive quickcontainer

If I decide that I no longer need that container, after it has been stopped, I can remove it whit this command:

docker rm quickcontainer

Note that this command will leave untouched the volume directories (/home/myusername/docker-things/debs and /home/myusername/docker-things/apt-lists) within my host computer.

When I no longer need the image created, it can be removed with this command:

docker rmi acmeorg/explorationimg

Well, that's all about the somehow unconventional way I'm using containerization technologies to exploit them in my tests and frequent tasks!

Añadir nuevo comentario