Book Image

Mastering Docker - Third Edition

By : Russ McKendrick, Scott Gallagher
Book Image

Mastering Docker - Third Edition

By: Russ McKendrick, Scott Gallagher

Overview of this book

Docker has been a game-changer when it comes to how modern applications are deployed and created. It has now grown into a key driver of innovation beyond system administration, with an impact on the world of web development. But how can you make sure you're keeping up with the innovations it's driving, or be sure you're using it to its full potential? Mastering Docker shows you how; this book not only demonstrates how to use Docker more effectively, but also helps you rethink and reimagine what's possible with it. You will cover concepts such as building, managing, and storing images, along with best practices to make you confident, before delving more into Docker security. You'll find everything related to extending and integrating Docker in new and innovative ways. Docker Compose, Docker Swarm, and Kubernetes will help you take control of your containers in an efficient manner. By the end of the book, you will have a broad, yet detailed, sense of what's possible with Docker, and how seamlessly it fits in with a range of other platforms and tools.
Table of Contents (17 chapters)

Docker installation

Installers are one of the first pieces you need to get up and running with Docker on both your local machine and your server environments. Let's first take a look at which environments you can install Docker in:

  • Linux (various Linux flavors)
  • macOS
  • Windows 10 Professional

In addition, you can run them on public clouds, such as Amazon Web Services, Microsoft Azure, and DigitalOcean, to name a few. With each of the various types of installers listed previously, Docker actually operates in different ways on the operating system. For example, Docker runs natively on Linux, so if you are using Linux, then how Docker runs on your system is pretty straightforward. However, if you are using macOS or Windows 10, then it operates a little differently, since it relies on using Linux.

Let's look at quickly installing Docker on a Linux desktop running Ubuntu 18.04, and then on macOS and Windows 10.

Installing Docker on Linux (Ubuntu 18.04)

As already mentioned, this is the most straightforward installation out of the three systems we will be looking at. To install Docker, simply run the following command from a Terminal session:

$ curl -sSL https://get.docker.com/ | sh
$ sudo systemctl start docker

You will also be asked to add your current user to the Docker group. To do this, run the following command, making sure you replace the username with your own:

$ sudo usermod -aG docker username

These commands will download, install, and configure the latest version of Docker from Docker themselves. At the time of writing, the Linux operating system version installed by the official install script is 18.06.

Running the following command should confirm that Docker is installed and running:

$ docker version

You should see something similar to the following output:

There are two supporting tools that we are going to use in future chapters, which are installed as part of the Docker for macOS or Windows 10 installers.

To ensure that we are ready to use these tools in later chapters, we should install them now. The first tool is Docker Machine. To install this, we first need to get the latest version number. You can find this by visiting the releases section of the project's GitHub page at https://github.com/docker/machine/releases/. At the time of writing, the version was 0.15.0—update the version number in the commands in the following code block with whatever the latest version is when you install it:

$ MACHINEVERSION=0.15.0
$ curl -L https://github.com/docker/machine/releases/download/v$MACHINEVERSION/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine
$ chmod +x /tmp/docker-machine
$ sudo mv /tmp/docker-machine /usr/local/bin/docker-machine

To download and install the next and final tool, Docker Compose, run the following commands, again checking that you are running the latest version by visiting the releases page at https://github.com/docker/compose/releases/:

$ COMPOSEVERSION=1.22.0
$ curl -L https://github.com/docker/compose/releases/download/$COMPOSEVERSION/docker-compose-`uname -s`-`uname -m` >/tmp/docker-compose
$ chmod +x /tmp/docker-compose
$ sudo mv /tmp/docker-compose /usr/local/bin/docker-compose

Once it's installed, you should be able to run the following two commands confirm the versions of the software is correctly:

$ docker-machine version
$ docker-compose version

Installing Docker on macOS

Unlike the command-line Linux installation, Docker for Mac has a graphical installer.

Before downloading, you should make sure that you are running Apple macOS Yosemite 10.10.3 or above. If you are running an older version, all is not lost; you can still run Docker. Refer to the other older operating systems section of this chapter.

You can download the installer from the Docker store, at https://store.docker.com/editions/community/docker-ce-desktop-mac. Just click on the Get Docker link. Once it's downloaded, you should have a DMG file. Double-clicking on it will mount the image, and opening the image mounted on your desktop should present you with something like this:

Once you have dragged the Docker icon to your Applications folder, double-click on it and you will be asked whether you want to open the application you have downloaded. Clicking Yes will open the Docker installer, showing the following:

Click on Next and follow the onscreen instructions. Once it is installed and started, you should see a Docker icon in the top-left icon bar on your screen. Clicking on the icon and selecting About Docker should show you something similar to the following:

You can also open a Terminal window. Run the following command, just as we did in the Linux installation:

$ docker version

You should see something similar to the following Terminal output:

You can also run the following commands to check the versions of Docker Compose and Docker Machine that were installed alongside Docker Engine:

$ docker-compose version
$ docker-machine version

Installing Docker on Windows 10 Professional

Like Docker for Mac, Docker for Windows uses a graphical installer.

Before downloading, you should make sure that you are running Microsoft Windows 10 Professional or Enterprise 64-bit. If you are running an older version or an unsupported edition of Windows 10, you can still run Docker; refer to the other older operating systems section of this chapter for more information.

Docker for Windows has this requirement due to its reliance on Hyper-V. Hyper-V is Windows' native hypervisor and allows you to run x86-64 guests on your Windows machine, be it Windows 10 Professional or Windows Server. It even forms part of the Xbox One operating system.

You can download the Docker for Windows installer from the Docker store at https://store.docker.com/editions/community/docker-ce-desktop-windows/. Just click on the Get Docker button to download the installer. Once it's downloaded, run the MSI package and you will be greeted with the following:

Click on Yes, and then follow the onscreen prompts, which will go through not only installing Docker, but also enabling Hyper-V, if you do not already have it enabled.

Once it's installed, you should see a Docker icon in the icon tray in the bottom right of your screen. Clicking on it and selecting About Docker from the menu will show the following:

Open a PowerShell window and type the following command:

$ docker version

This should also show you similar output to the Mac and Linux versions:

Again, you can also run the following commands to check the versions of Docker Compose and Docker Machine that were installed alongside Docker Engine:

$ docker-compose version
$ docker-machine version

Again, you should see a similar output to the macOS and Linux versions. As you may have started to gather, once the packages are installed, their usage is going to be pretty similar. This will be covered in greater detail later in this chapter.

Older operating systems

If you are not running a sufficiently new operating system on Mac or Windows, then you will need to use Docker Toolbox. Consider the output printed from running the following command:

$ docker version

On all three of the installations we have performed so far, it shows two different versions, a client and server. Predictably, the Linux version shows that the architecture for the client and server are both Linux; however, you may notice that the Mac version shows the client is running on Darwin, which is Apple's Unix-like kernel, and the Windows version shows Windows. Yet both of the servers show the architecture as being Linux, so what gives?

That is because both the Mac and Windows versions of Docker download and run a virtual machine in the background, and this virtual machine runs running a small, lightweight operating system based on Alpine Linux. The virtual machine runs using Docker's own libraries, which connect to the built-in hypervisor for your chosen environment.

For macOS, this is the built-in Hypervisor.framework, and for Windows, Hyper-V.

To ensure that no one misses out on the Docker experience, a version of Docker that does not use these built-in hypervisors is available for older versions of macOS and unsupported Windows versions. These versions utilize VirtualBox as the hypervisor to run the Linux server for your local client to connect to.

VirtualBox is an open source x86 and AMD64/Intel64 virtualization product developed by Oracle. It runs on Windows, Linux, Macintosh, and Solaris hosts, with support for many Linux, Unix, and Windows guest operating systems. For more information on VirtualBox, see https://www.virtualbox.org/.

For more information on Docker Toolbox, see the project's website at https://www.docker.com/products/docker-toolbox/, where you can also download the macOS and Windows installers.

This book assumes that you have installed the latest Docker version on Linux, or have used Docker for Mac or Docker for Windows. While Docker installations using Docker Toolbox should be able to support the commands in this book, you may run into issues around file permissions and ownership when mounting data from your local machine to your containers.