First Steps With Docker
Docker is a popular platform to build, distribute and deploy portable applications, based on container technology. Application containers are one of the most important components of the cloud platform, allowing to quickly provision and deploy running applications based on images to a variety of targets: compatible cloud infrastructures, on-prem, bare metal.
This tutorial uses Mac OS X 10.10 Yosemite, but you can follow similar steps for Windows. Linux doesn’t avail of a GUI installer, but you can easily install Docker via apt-get or yum. Note that the Docker website is undergoing some major restyling so the screenshots may differ from the actual pages.
1. Install Docker Toolbox
Download and install the Docker Toolbox. This will install the Docker Quickstart Terminal, the Kitematic GUI client, the docker command, and a few other utilities.
2. Run Your First Image
Now that we’re ready to work with Docker, we can launch the Docker Quickstart Terminal and run our first image.
There are two types of images: “official”, and contributed. Official images are made available by trusted vendors on Docker Hub, sort of an App Store for Docker images. You can find images for pretty much anything you may want to run on a server: Nginx, Mongo, RabbitMQ, but also barebone OS images: Ubuntu, CentOS, and so on. Contributed images are published by organizations like RedHat, IBM, Oracle, or ordinary users.
Let’s download the latest Ubuntu image, and execute a command:
$ docker run ubuntu:latest echo “hello world!”
Unable to find image ‘ubuntu:latest’ locally
latest: Pulling from library/ubuntu
d3a1f33e8a5a: Pull complete
c22013c84729: Pull complete
d74508fb6632: Pull complete
91e54dfb1179: Pull complete
library/ubuntu:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for ubuntu:latest
As you can see, Docker is trying to find a local image first. Images are cached locally for performance. In this case, we’ll have to download it from the Hub. Once downloaded, the image is loaded into a container and run, the command echo is executed with the argument “hello world!”. After that, the container is terminated.
Instead of running the echo command, we could’ve run the bash shell:
$ docker run -it ubuntu:latest bash
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
Running the shell keeps the container alive until we terminate it with the exit command. The -it flags wire the container’s terminal and stdin to our terminal, so we can interact with it. Note that the Ubuntu image is very barebone. If you need programs like vim or less, you’ll have to apt-get them.
3. Build Your Own Image
The coolest aspect of Docker is that it lets you compose and build images at will, in order to create complex images that can be provisioned on the fly to power applications for your specific needs. The Dockerfile is the equivalent of a Makefile to build a Docker image. You can reuse existing images and specialize them, by installing applications, running commands, or deploying files.
Let’s specialize the docker/whalesay image that comes with the Docker tutorial, to display the output of the fortune command. The docker/whalesay image is a Ubuntu distro with a custom build of the cowsay program that displays Docker’s whale instead of the usual cow.
Let’s create a Dockerfile with the following contents:
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
This tells Docker to use the latest tag of the docker/whalesay image, install the fortune program via apt-get, and execute cowsay feeding it with the output of fortune. Let’s build the image running this command from the directory containing the Dockerfile (don’t forget the trailing dot):
$ docker build -t docker-whale .
At this point, we have created a docker-whale image that can be run:
$ docker run -it docker-whale
4. Publish Your Image
Now that we built the docker-whale image, we are ready to publish it to Docker Hub. To publish images to the Hub, we’ll have to sign up for an account first, but worry not: it can be created free of charge.
Once we’ve created and verified our account, we can login from the Docker Quickstart Terminal:
$ docker login
Username: <enter your username>
Password: <enter your password>
Email: <enter your email>
WARNING: login credentials saved in /Users/delphine/.docker/config.json
Now we can tag and push the image:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker-whale latest cb6dccf1ca20 25 hours ago 274 MB
docker/whalesay latest fb434121fc77 3 months ago 247 MB
$ docker tag cb6dccf1ca20 claudiopro/docker-whale:latest
$ docker push claudiopro/docker-whale
If everything went well, the docker-whale image will appear in your repository on Docker Hub:
I recommend the brilliant Self-Paced Training by Johnny Tu available on the Docker website. It is divided in three parts: Introduction, Fundamentals, and Operations. Each tutorial lasts around 1h, so they can be easily fit into a lunch break or the evening.