To the uninitiated, we are not talking about Docker clothing company, which makes the popular Men’s Khakis. What we are talking about is the Docker that has changed the way Software Applications are bulit, shipped and run. You have probably heard about Docker and how cool it is, but never really understood fully. And you are itching to finally sit down and read about it. You have come to the right place. In this blogpost, I’m going to demystify Docker for you. By reading this post fully, you will understand,
- What the heck is Docker?
- What makes Docker so invaluable and indispensable?
- How to install Docker on your PC or MAC?
- Most commonly used Docker commands
- A full working example
Ready? Let’s begin.
What the heck is Docker?
Docker provides a way for Applications to be built and run in a container with all the required software packaged in it.
But you ask, what in the world is container?
Container is a Docker process that can be run on any Linux or Windows based system. It includes everything it needs to run, including System lirbaries, application code, application dependencies and configuration files. (You throw Docker container in a Car’s bumper and it will still work. Just kiddin.) Containers running on a system share the Operating System Kernel with other processes (including other Docker containers). A Docker container is a mini-machine in its own right.
Tip: You can list all the Docker containers running your system by running the command
To contrast this with how software applications are traditionally run, look at the image below.
At this point, you may be wondering, ‘Wait a minute. I’ve seen this before. Are you not talking about virutalization? Vmware and stuff?‘
Not really. Simply put, vmware virutalizes the underlying hardware resources. But Docker virtualizes the Operating System that runs on top of Vmware virutal machine.
At this point, if you have been doing Application Support for a while, one stricking advantage should be obvious to you: Consistancy of environments. Think about how many times you have been told by the development team: ‘Oh, but it works in my local Dev enviornment. Something must be wrong in production servers. May be a jar file is missing in the classpath in Prod?’ Painful. Docker puts an end to all this environment specific mysteries.
So, to summarize: Docker is a container solution that enables buliding, shipping and running applications with all the required software in a single unit. The benefits include consistency across deployments, fast startup, flexible and developer-friendly build process.
Enough fluff. Let’s get our hands moving by actually running a Docker container.
Before you can actually run your first Docker Conainter, you have to install Docker software.
What do you need to begin?
A Linux or Windows or MAC system (can be a desktop or a server).
There are two flavors of Docker. Community Edition and Enterprise Edition. Community Edition is free and mainly greared towards learning and testing purposes. Enterprise Edition comes with few bells and whistles and also comes with an invoice in the mail. We will be using Community edition to learn Docker. (Note: You cannot install Docker Enterprise Edition in Desktops). For full compatibility matrix, check this link.
Note: In older MAC and Windows systems, Docker was installed using Docker Toolbox, a legacy desktop solution that inlucded docker-machine, docker and docker-compose commands (more on these commands later). If you have newer MAC ( OS X El Capitan10.11 or later) or Winodws (Winodws 10 or later) It is recommeded to install Docker for MAC or Docker for Windows instead of Docker Toolbox.
To install Docker for MAC, download the dmg from Get Docker for Mac (Stable).
Double click Docker.dmg. Drag and drop to the Applications folder as shown below.
Note: You must have Admin privilege to perform the install, as shown by the pop up below that appears when you run the install
Once installed, the docker icon (Whale) should appear on your top status bar.
Download the installer from Get Docker for Windows (Stable).
Simply launch the exe file and follow prompts. It should be a super straight forward install.
You can use apt-get to install from the Docker repository or install a deb package using dpkg.
Follow the instructions here
You can use yum to install from the Docker repository or download rpm from here
For detailed instructions, go here.
Great, now that you’ve installed Docker, let’s start playing with it with some basic commands.
Using Docker for the first time
We will be using Docker for Mac to illustrate. The commands are exactly the same in other flavors.
First thing you want to do is to make sure you have the latest version of Docker (at least not some ancient version). As of this writing, Docker CE 17.12.0 is the stable version. Click on the Docker icon (Whale) at the top status bar and click on About Docker
Open a terminal and type the following command
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:03:51 2017
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:12:29 2017
Notice the GO version. Docker is built using the Go programming language.
If you simply type the command docker and press Enter, you will see nice concise command list.
If you need help with any of the commands, simply type the following command:
docker <command name> –help
~$docker ps --help
Usage: docker ps [OPTIONS]
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
Okay, let’s get down to business. Let’s actually run a docker container.
With docker, there is this concept called images. First, you create an image and then you run the image using a container.
Image ——–> Run ——–> Container.
Running your first container
From the shell prompt, execute the following command.
~$docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
d3938036b19c: Pull complete
a9b30c108bda: Pull complete
67de21feec18: Pull complete
817da545be2b: Pull complete
d967c497ce23: Pull complete
Status: Downloaded newer image for ubuntu:latest
Let’s disect the above command.
docker: This is the docker executable
run: This tells docker to run a container
-i: This tells docker to keep STDIN open (i.e to be interactive)
-t: This tells docker to allocate a tty ( so that you get a nice terminal to type commands in)
ubuntu: This is the all important parameter – this tells docker which image to run. If the image is not available locally, docker will try to obtain the image from docker repository (hub.docker.com)
/bin/bash: This is the command to be run when the container begins running.
Open another terminal window and type the following command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5dfc81d2340 ubuntu "/bin/bash" 3 seconds ago Up 5 seconds practical_kepler
docker ps shows the containers that are currently running. Notice the container id. Also notice how the docker command borrows ps which is unix command to show the runnign processes.
Note: you can use the command docker ps -a show the containers that had run in the past but no longer running.
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c9d990395902 3 days ago 113MB
docker images shows the avaialble images. When I ran the docker run command, it pulled the ubuntu image from hub.docker.com. Note the tag latest. Tags are how you keep various versions of the image. If no tag is specified, the defaut tag would be latest.
So, what is the nature of the container I just started ? Let’s run few commands in the container’s bash shell that was started earlier.
root@a5dfc81d2340:/# cat /etc/os-release
VERSION="16.04.4 LTS (Xenial Xerus)"
PRETTY_NAME="Ubuntu 16.04.4 LTS"
So, what you got was Ubuntu 16.04.4 (from the version field).
Note: What if you want a sepcific version of ubuntu ? You need to go to hub.docker.com and check the available ubuntu versions.
When you search for ‘ubuntu’, you get the following images:
Notice the ‘Supported tags and respective Dockerfile links’. Note how 16.04 is tagged as latest. If you want ubuntu 14.04 for example, your docker run command sholud look like the following:
~$docker run -i -t ubuntu:14.04 /bin/bash
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
c2c80a08aa8c: Pull complete
6ace04d7a4a2: Pull complete
f03114bcfb25: Pull complete
99df43987812: Pull complete
9c646cd4d155: Pull complete
Status: Downloaded newer image for ubuntu:14.04
root@2cbd08aaa731:/# cat /etc/os-release
VERSION="14.04.5 LTS, Trusty Tahr"
PRETTY_NAME="Ubuntu 14.04.5 LTS"
docker ps will show you that you are running 14.04
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cbd08aaa731 ubuntu:14.04 "/bin/bash" 4 minutes ago Up 4 minutes fervent_kepler
Yes, it’s that simple.
Coming back to the ubuntu 16.04 container we started earlier, let’s run couple of commands in the container.
root@ff8b7428adf6:/# df -m
Filesystem 1M-blocks Used Available Use% Mounted on
overlay 64251 1447 59512 3% /
tmpfs 64 0 64 0% /dev
tmpfs 1000 0 1000 0% /sys/fs/cgroup
/dev/sda1 64251 1447 59512 3% /etc/hosts
shm 64 0 64 0% /dev/shm
tmpfs 1000 0 1000 0% /proc/scsi
tmpfs 1000 0 1000 0% /sys/firmware
df -m shows the file systems that have been mounted with their sizes in megabytes.
The beauty of docker is, all these file systems are completely isolated from the host operating system. It’s as if you are sitting in a OS inside an OS. Whatever you do to this container