Docker was made available in 2013; one of the limitations early on had been that Docker did not run on Windows OS. Docker was designed to run on the Linux kernel initially. Multiple Docker containers running in isolation reused the same Linux kernel. Docker was not platform agnostic.
In 2015-2016, Docker for Windows was introduced, first as Beta, to make it feasible to run a Docker daemon on Windows Server 2016. No virtualization is involved to run the Docker daemon on Windows. The Windows Server Containers reuse the host OS kernel and create a sandboxed environment similar to how Docker containers on Linux do. The first stable release of Docker for Windows was made available in July 2016. The Beta release is called Edge, starting with the CE release model. Docker for Windows (Stable and Edge) are supported on Windows 10 and Windows Server 2016. Docker for Windows has the provision to run both Docker Linux containers and Docker Windows containers. In this tutorial, we shall get started with using Docker for Windows on Windows Server 2016.
This tutorial has the following sections:
- Setting the Environment
- Installing Docker for Windows
- Starting Docker
- Docker for Windows Settings
- Using the PowerShell
- Listing Docker Version and Info
- Using Docker Commands
- Fixing Common Issues
Because Docker for Windows supports only Windows 10 and Windows Server 2016, we need to create an AWS EC2 instance running Windows Server 2016 OS if either of these is not available on a local machine. We have used Windows Server 2016 instance running on AWS EC2. Why Windows Server 2016 and not Windows 10?
- Windows 10 is designed for the desktop and not designed for the Cloud.
- Windows 10 is not available on EC2. Windows 10 is available on Microsoft Azure, but not available in the free tier. EC2 instances based on Microsoft Windows Server 2016 with SQL Server 2016 Express AMI are eligible for the free tier.
- Windows Server 2016 was the first OS to support Docker for Windows
Launch a Windows Server 2016 instance on EC2 with Microsoft Windows Server 2016 with SQL Server 2016 Express AMI, using the procedure discussed in an earlier article, "Setting Up Windows Server 2016 on Amazon EC2."
If you are not familiar with AWS EC2 and are primarily an Azure user, a Windows Server 2016 instance on Azure may be used.
Connect to the Windows Server 2016 instance from a local machine, which is also discussed in the earlier article "Setting Up Windows Server 2016 on Amazon EC2."
In this section, we shall install Docker for Windows. Double-click the IE icon, as shown in Figure 1.
Figure 1: Launching IE
Double-click the Get Docker for Windows (Stable) link to download the binaries for Docker for Windows, as shown in Figure 2.
Figure 2: Downloading Docker for Windows binaries
Double-click the Docker for Windows Installer, as shown in Figure 3.
Figure 3: Docker for Windows Installer
The Docker for Windows files start to get unpacked (see Figure 4).
Figure 4: Unpacking files
Docker for Windows starts to get installed, as indicated by the "Installing…" message in Figure 5.
Figure 5: Installing Docker for Windows
Docker for Windows gets installed, as shown in Figure 6. Click Close and log out. As a message indicates, the user must log out of Windows to complete the installation.
Figure 6: Docker for Windows installed
Click Connect in Chrome RDP again to connect to the Windows Server 2016 instance, as shown in Figure 7.
Figure 7: Re-connecting to Windows Server 2016
The Docker for Windows icon should have been added to the Windows screen (see Figure 8).
Figure 8: Docker for Windows icon
To start the Docker daemon, double-click the Docker for Windows icon. As the message in Figure 9 indicates, "Docker is starting…"
Figure 9: Docker is starting
A dialog prompts with the message that Hyper-V is not enabled. To enable Hyper-V, click OK, as shown in Figure 10.
Figure 10: Enabling Hyper-V
The Windows Server 2016 machine restarts and, when the Docker has started running, the message "Docker running…" gets displayed, as shown in Figure 11.
Figure 11: Docker is running
To display and modify (if required) the Docker for Windows settings, right-click the Docker icon shown in Figure 11 and select Settings, as shown in Figure 12.
Figure 12: Settings
The different settings categories—General, Shared Drives, Advanced, Network, Proxies, Daemon, Diagnose & Feedback, and Reset—are provided, as shown in Figure 13. The General settings include the checkboxes Start Docker when you log in, Automatically check for updates, Send usage statistics; all of these are selected by default. The Proxies settings are the proxies Docker uses to pull Docker images. By default, no proxies are configured. With the Daemon settings, the Docker daemon may be configured with some experimental features, which include Insecure registries and registry mirrors. The "Docker is running" message indicates that Docker is running. No settings need to be modified by default.
Figure 13: Docker for Windows settings
With the Docker daemon running, Docker commands may be used to run Docker Windows containers or the Docker Linux container. By default, the Docker Windows containers may be run, but a feature toggle is provided, as shown in Figure 12, to Switch to Linux Containers. If the Linux containers mode is enabled, the toggle setting becomes Switch to Windows containers, as shown in Figure 14.
Figure 14: Toggle Setting Switch to Windows Containers
Docker commands may be run in a command shell such as cmd.exe or PowerShell. To launch PowerShell, select Windows PowerShell from the Windows Server, as shown in Figure 15.
Figure 15: Launching PowerShell
The PowerShell command line gets launched, as shown in Figure 16.
Figure 16: PowerShell
Run the following command to list the Docker version details:
The command output includes the Docker Server Version 17.06.2-ce, API Version 1.30, and the Built. The OS/Arch is listed as windows/amd64 (see Figure 17).
Figure 17: Output from docker version
Run the following command to list just the Docker server version:
The Docker server version is 17.06.2-ce, as shown in Figure 18.
Figure 18: Docker Server Version
The Docker Compose and Docker Machine versions may also be listed, as shown in Figure 19.
Figure 19: Docker Compose and Machine Versions
To list system-wide information, run the following command:
The system-wide information gets listed, as shown in Figure 20.
Figure 20: The docker info output
The Docker commands may be used to run Docker Windows containers or Docker Linux containers based on the Windows/Linux mode set. The docker run command output in Docker Windows Containers mode is shown in Figure 21.
Figure 21: Output from docker run
The docker ps command does not list any containers running, as shown in Figure 22.
Figure 22: Output from docker ps
Using Docker for Windows could come across some issues; some of these issues are discussed next. The error "Unable to write to the database. Exit code: 1" could get generated, as shown in Figure 23.
Figure 23: An error message
If Hyper-V was not enabled when installing Docker for Windows, uninstall and reinstall with Hyper-V enabled (refer to Figure 10).
Restarting Docker might fix an error. To restart, select Reset in Settings and click Restart Docker, as shown in Figure 24.
Figure 24: Restart Docker
In the Restart Docker dialog, click Restart (see Figure 25).
Figure 25: Restart Docker
The Docker is restarting message should get displayed, as shown in Figure 26.
Figure 26: Docker is restarting
When Docker starts to run, the message "Docker is running" gets displayed, as shown in Figure 27.
Figure 27: Docker is running
If the issue is still not fixed, the Docker for Windows Service listed in Figure 28 might need to be restarted.
Figure 28: Docker for Windows Service
Right-click Docker for Windows Service and select Restart, as shown in Figure 29.
Figure 29: Restarting Docker for Windows Service
If Docker is not running, the docker commands such as docker version and docker info generate an error, as shown in Figure 30.
Figure 30: The docker version command generates an error message
In this tutorial, we discussed getting started with Docker for Windows on Windows Server 2016. Docker for Windows is also supported on Windows 10. Docker for Windows runs both Windows containers and Linux containers.