First Steps With Docker

Claudio Procida
Sep 17, 2015 · 5 min read

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.

Image for post
Image for post

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

Image for post
Image for post
The Docker Toolbox Installer for Mac OS X

2. Run Your First Image

Image for post
Image for post
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

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
Image for post
Image for post
The output of the docker-whale container

4. Publish Your Image

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:

Image for post
Image for post
Here’s our first image published on Docker Hub

Further Reading

References

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store