Hello World with Docker
Docker is a nice and easy way to isolate and precisely define and execution environment, both for production and testing. The following includes basic setup and start-up steps for few basic Docker Container instances. For details on all the commands, see the official Docker documentation.
Installing
Initial installation of the Docker service is documented here. On a newer Debian (8 and up) system, it boils down to:
apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
apt-get update
apt-get install docker-ce
Hello World
Once installed, predefined images can be downloaded and executed easily. As a normal user, either use sudo in front of every docker command, or add yourself to the docker group. Be aware that the latter has security implications in a shared environment.
sudo groupadd docker
Now download and execute the official hello-world image.
docker run hello-world
It will search for the image locally first, and download it if necessary. The image itself merely prints a welcome message and exits.
A clean image
Docker Hub contains thousands of predefined images, including very small installations of the most common Linux distributions. The following downloads, starts the latest Ubuntu image (16.04 at the time of writing), and executes bash. The -i and -t arguments leave an interactive shell into the running Docker Container.
docker run -it ubuntu bash
cat /etc/issue
exit
For detailed documentation about the various docker commands, see here.
A custom image
It is easy to built on top-of existing images; it is after all what Docker is designed for. Each line in a Dockerfile defines another layer upon the previous. It means, that if the previous layer is already downloaded or built, Docker will simply pick up from the step it needs to alter first. For this reason, it can be useful to think through the combination of alterations which will be applied. Even though it is possible to do a lot on a single line, it might be useful to break unstable commands up. Conversely, it might make sense to combine multiple install commands if they are dependent or belong together.
The following minimal image definition takes the latests ubuntu image and installs some useful utilities. See here for the detailed Dockerfile reference.
FROM ubuntu RUN apt-get update && \ apt-get install -y htop tree
To build this Docker image, enter into the directory where the Dockerfile exists, and execute the following, where the -t option gives a name to the new image.
docker build -t my_ubuntu .