If you are a developer or system administrator that wants to learn about working with containers, Docker for Windows provides a great way to get up and running quickly. Windows 10 contains built in support for Windows Containers and Hyper-V Containers. Additionally, you can run Linux containers using Hyper-V, a minimal Linux kernel and userland in the Windows 10 Fall Creators Update and in Windows Server 1709 or later.
In this article, I will show you how to create a Linux container in Windows 10 using Docker for Windows. If you want to perform the same task on Windows Server, you’ll need Docker Enterprise Edition (Docker EE) instead. To follow the instructions below, you must be running Windows 10 version 1709 or later, on a device that supports Hyper-V with at least 4096MB of RAM. Docker for Windows supports 64-bit Pro and Enterprise editions of Windows 10 only.
The first step is to download and install Docker for Windows. You can find the downloader on Docker’s website here. Run the installer and follow the instructions. You might be required to reboot your computer once or twice during the installation process. The installer will automatically enable Hyper-V and support for Windows Containers if they are not already turned on.
Before you can download base images from Docker’s repository, you’ll need to sign in to Docker on the welcome screen. If you don’t already have a Docker user ID, you can create one here. When you sign in to Docker for Windows, it is important to use your Docker user ID and NOT your email address. Both your Docker user ID and email address will be accepted by Docker for Windows but using your email address will not allow you to access Docker’s repository.
Before you can create a Linux container, you need to pull a base image from Docker’s repository. Open a PowerShell or command prompt and use the following command to pull the latest Ubuntu base image from the repository:
docker pull ubuntu
Using the above command will pull the latest available version of Ubuntu from the repository. If you want to pull a specific version of Ubuntu, use a tag as shown here:
docker pull ubuntu:18.04
If you want to search the repository for Ubuntu images, use search as shown below:
docker search ubuntu
To list the available images on the local computer, including information about image size, image ID, and tags:
To create a new Linux container, we need the ID of the base image and the docker run command. In the command below, I’ve used the image ID for the latest version of Ubuntu in my local repository, and the bash terminal will launch once the container has started:
docker run -i -t cd6d8154f1e1 /bin/bash
The -i and -t parameters allow the bash process to start in the container, attaches the console to the process’s standard input, output, and standard error, and allocates a pseudo-tty text-only console. Once the container has been created, you’ll be presented with a bash prompt. Type hostname and press ENTER to see the container’s Linux hostname. You can stop the container at any time by typing exit and pressing ENTER. Exiting a container stops it from running.
TIP: docker create is similar to docker run but the docker create command creates a writeable container layer over the image and prepares it for running the command you specify. The container is not started. The docker create command is useful in scenarios where you want to set up a container in advance and have it ready to go using docker start.
You can use the docker ps command to list the containers on the local device. The command below lists all containers, regardless of whether they are running. If you omit the -all parameter, docker ps shows only containers that are running. You can see in the output that container and image IDs are listed.
docker ps -all
If you exit a container and want to restart it, use docker start along with the container’s ID.
docker start df75aa301d62
To connect to the container’s terminal, use docker attach:
docker attach df75aa301d62
Let’s make a minor change to the running container, and then use it to create a new local image from which we can create new containers. In this example, I’ll install the vim text editor in the container and then stop the container. To do that, run the following three commands in the container:
apt-get update apt-get install vim exit
Finally, use docker commit to create a new image from the container. You need to specify the container’s ID and an image name. You can overwrite an existing image or create a new image. In this example, I’m creating a new image called Ubuntu with a tag (18.04vim) to help identify it.
docker commit df75aa301d62 ubuntu:18.04vim
Run docker images again and you’ll see the new image in the list. You can now use this image to create a Linux container that includes the vim text editor.
In this article, I showed you how to install Docker for Windows, download a base Ubuntu image, create a new Linux container from a base image, and create a new Linux image from a container.