Deploying your First Container with Docker for Windows

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

The easiest and cheapest way to get started with Docker is on Windows. This tutorial will walk you through the process of deploying a container running Ubuntu, which includes installing Docker for your windows machine.

Docker is a tool that allows you to deploy containers. Docker for Windows includes the “docker hub” which can be used to search and download app packages from the iOS App Store.

Deploying your First Container with Docker for Windows

You’re in for a treat if you’re new to containers and Docker and operate mostly on Windows. In this post, you’ll learn how to use Docker Desktop for Windows, also known as Docker Desktop in this article, to set up your first Docker container on Windows 10.

Docker Desktop is a combination of the Docker Engine and a management interface for Windows 10. Install Docker Desktop, deploy your first container, and communicate data between your host and containers in this post.

Prerequisites/Requirements

This is a walkthrough article that shows you how to use Docker Desktop. Before you begin, make sure you have a few prerequisites in place.

  • An Internet connection capable of downloading data in excess of 800MB
  • Release 1703 or newer of Windows 10 64-bit Pro, Enterprise, or Education editions. This is required for Hyper-V to work on Windows 10.
  • A CPU that supports SLAT (nested paging). Since about 2008, all AMD/Intel CPUs have been SLAT compliant.
  • RAM of at least 4GB
  • Virtualization Technology or VTx is a term used to describe BIOS hardware virtualization. This must be enabled and shown as Enabled on Task Manager’s performance tab, as illustrated below.

Windows Task Manager shows that BIOS virtualization is enabled.Windows Task Manager shows that BIOS virtualization is enabled.

Docker Desktop Download and Installation

You’ll need to download and install Docker Desktop first. Docker Desktop is available in two different versions: stable and testing.

The stable version is produced every quarter and assures that the application has been thoroughly tested. The stable release will be used in this article.

Warning: If you haven’t previously done so, Docker Desktop will ask you to install the Hyper-V hypervisor. The Hyper-V hypervisor precludes user-mode hypervisors like VirtualBox, VMWare, and others from running guest virtual machines. Support for Hyper-V in VirtualBox and VMWare is limited but on the way.

You may alternatively choose a download source by manually downloading Docker Desktop According to Docker.com or Using Chocolatey as a Base, the Windows package manager. Let’s go through each strategy in detail.

According to Docker.com

To download Docker Desktop directly According to Docker.com, you can go to the product page, register for an account and download it from there. This is preferred if you intend to use Docker in production by registering an account.

If you’re simply trying out Docker for the first time, you may also download it directly, which is much more convenient.

Run the program when it has been downloaded and accept all of the settings.

Do not tick the option when asked whether you want to use Windows containers instead of Linux containers, as illustrated below. This post will make use of Linux containers.

Docker Desktop InstallationDocker Desktop Installation

Restart your computer after the installation is finished.

When you choose Windows containers or Linux containers, Docker will connect images to a Windows kernel or a Linux kernel, respectively. After installation, you may modify this option by right-clicking the Docker icon in the System Tray and choosing Switch to Windows Containers, as seen below.

Making the switch to Windows containersMaking the switch to Windows containers

Using Chocolatey as a Base

Chocolatey is another way to have Docker Desktop downloaded and installed. Many download/install procedures are automated by Chocolatey. To do so, log in as an administrator to a command-line terminal (either cmd or PowerShell) and use the command below to download and install the application in one go.

install docker-desktop choco

After that, restart Windows 10.

If you’d like to try out the testing release at some point, you can download and install this by running install docker-desktop choco –pre.

Installing Docker Desktop and Verifying It

Docker Desktop operates as a service after it is installed. When you log in to Windows after a reboot, it appears on the system tray. But how can you know it’s truly working?

Open a command-line prompt and execute the docker command to ensure Docker Desktop is operating properly. A Docker command reference will appear if the installation completed well.

Finally, use the command docker run hello-world to have Docker download and run an example container image named hello-world. If everything goes properly, you should see something like this.

Docker Desktop was used to execute a successful test image.Docker Desktop was used to execute a successful test image.

Using Docker Containers to Execute Commands

You’ve installed Docker Desktop and double-checked everything? What’s next? Running commands is a frequent operation to execute in a Docker container to get started. You may send instructions straight into a running container using the docker run command from the host (your Windows 10 PC).

To use docker run to execute commands in a container, first supply an image name, then the command. To begin, instruct Docker to execute the command hostname within an alpine container, as shown below.

> docker run alpine hostname b74ff46601af

Docker will download the small image from the Docker Hub, set up a container from that image, send the command right into the container, and shut it down all in one swoop since you don’t have the alpine Docker image on your machine yet.

You may also use the -it argument if you want to keep the container running. This argument instructs Docker to maintain the container in “interactive mode” after executing the command, leaving it running in the forefront. You will then be given with a terminal prompt that is ready to use.

> docker run -it alpine sh / #

To return to Windows 10, type exit after you’re through using the terminal.

In Containers, Getting Files from the Docker Host

Accessing host files from containers is another popular operation. Docker enables you to connect a folder path from your desktop to share that folder with your container in order to access host files in containers. Binding is the term for this procedure.

Make a folder on your local disk for the binding. I’ll use E: and call it input in this example. It will then generate a new text document in the folder called file.txt. You are able to use any path and file you choose.

Docker must mount the folder you want to share between the host and container using the —mount argument once you have it. A mount type, a source host directory path, and a destination directory path are all required parameters for the —mount option. Within the container, the destination path will be a symbolic link.

An example of mounting the full E: drive on the Windows 10 host to appear as the /home/TEST directory within the Linux container is shown below.

> docker run –mount type=bind,source=”E:/”,target=/home/TEST -it alpine

When you try to mount a host folder, Docker Desktop will ask you whether you want to share it with the Docker containers, as seen below.

Docker drive sharingDocker drive sharing

Run cat /home/TEST/input/file.txt if the file.txt file was generated in the Windows 10 folder as explained above. You’ll see that the contents are visible.

Now remove the input folder you just created and run the cat command once again. Notice how the shell now says that the file no longer exists.

Using Docker containers to work with filesUsing Docker containers to work with files

Network Port Mapping

Another key topic to grasp is Docker’s networking capabilities. Let’s look at what it takes to access a web service operating in a container from a local host as an example.

Create a sample image that will run an example website first. Download and run the dockersamples/static-site Docker image. To do so, you’ll utilize docker container run.

The following command does four tasks simultaneously:

  • The static-site Docker image is downloaded from Docker Hub and placed in the docker-samples “directory.”
  • The static-site image is used to start a container instance.
  • Detaches the container from the terminal foreground immediately (—detach).
  • Makes the network ports of the operating container available to the Windows 10 host (—publish-all).

—detach —publish-all docker container run dockersamples/static-site ## A different/shorter syntax for the same thing: ## docker container run -d -P static-site/dockersamples ## docker run -d -P static-site/dockersamples

Docker will provide the container ID that was brought up once it has been executed, as illustrated below.

Running a container returns a Docker container ID.Running a container returns a Docker container ID.

Network Ports for Publication

Local host ports are now mapped to the container’s network stack after using the —publish-all argument. You may use the docker ps subcommand to see a list of all operating containers, as well as the ports that have been allocated to them. One container is operating in the example below, mapping host port 32777 to container port 80 and host port 32776 to container port 443.

Docker container port mapping is shown.Docker container port mapping is shown.

Unless specifically defined, Docker allocates random ports to containers when using the —publish-all command.

Now go to http://localhost:32777 or the port that Docker allocated to map to port 80 as produced by docker ps in a web browser. If all works properly, the following website should appear.

The resulting website in a Docker containerThe resulting website in a Docker container

Changing the Publicly Available Ports

In Docker Desktop for Windows, you now have a Docker container delivering a basic web page. Congratulations! However, instead of depending on the random port selection provided by —publish-all, you must now provide a particular port binding. No worries. Use the -p option.

To begin, supply a unique string of the operating container’s container ID. This container ID may be found by executing docker. ps. Stop the container and start a new one with Docker assigning a particular port to publish after you have the container ID.

The syntax for specifying a port is <external port>:<container port>. For each port that you want to publish, use the –publish or -p switch with the external and container port numbers as shown below.

> docker stop f766 > docker run –detach -p 1337:80 dockersamples/static-site

You simply need to enter enough of the ID to make it unique when supplying a container ID. If you only have one container operating and its ID is f766f4ac8d66bf7, you may identify it by any combination of characters, even simply f. The rule is that everything you type must be able to uniquely identify a single container.

Now visit to localhost:1337 in your web browser. Remember that you are altering the port translation rule in the Docker configuration that allows you to connect to the container, not the image, which always listens on port 80.

On port 1337, a webpage is being served.On port 1337, a webpage is being served.

All containers are being stopped.

You can stop a container using docker stop, but how do you stop several containers at once? Providing numerous, space-delimited container IDs is one approach to do it. An example of how to stop three containers with the IDs fd50b0a446e7, 36ee57c3b7da, and 7c45664906ff is shown below.

> docker stop fd50 36ee 7c45

If you’re using PowerShell to manage Docker containers, you can also use a shortcut to stop all of them. Using PowerShell’s command extension docker stop, provide a list of container IDs to the stop argument (docker ps -q).

When you enter docker ps, you should see no containers listed, indicating that all containers have been stopped.

Organizing

You’ve downloaded a few container images and started and halted some containers. Despite the fact that they’re halted, their assigned storage remains on the local host disk. To free up that space and prevent cluttering your workstation, you must remove the containers.

Use the container remove rm argument to delete a single container, as seen below.

> docker container rm <container ID>

Alternatively, use the prune argument to destroy all halted containers, as seen below.

Additional Reading

Docker for Windows is a new tool that allows users to deploy their first container. This process can be done with the “docker build” command.

Related Tags

  • docker windows container on linux
  • docker run
  • docker cli windows
  • docker run windows container
  • how to start docker

Table of Content