First Steps With Docker

Claudio Procida
5 min readSep 17, 2015

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.

The Docker Toolbox Installer for Mac OS X

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.

The Docker Quickstart Terminal greets us

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.
Digest: sha256:73fbe2308f5f5cb6e343425831b8ab44f10bbd77070ecdfbe4081daa4dbe3ed1
Status: Downloaded newer image for ubuntu:latest
hello world!

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
root@0a76731e26fd:/# ls
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:

FROM docker/whalesay:latest
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
The output of the docker-whale container

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
Login Succeeded

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:

Here’s our first image published on Docker Hub

Further Reading

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.

References

  1. Docker Docs — https://docs.docker.com/
  2. Docker Hub — https://hub.docker.com/
  3. Docker Self-Paced Training — https://training.docker.com/self-paced-training

--

--

Claudio Procida

Full stack web developer with a keen interest for the open web platform, privacy, security, and IoT. Former Amazonian, former IBMer. All views my own.