- Getting Started with Docker Desktop for Windows
- The easiest way to create containerized applications and leverage the Docker Platform from your desktop
- Overview
- Thank you for installing Docker Desktop. Here is a quick 5 step tutorial on how to use. We will walk you through:
- Running your first container
- Creating and sharing your first Docker image and pushing it to Docker Hub
- Create your first multi-container application
- Learning Orchestration and Scaling with Docker Swarm and Kubernetes
- Step 1: Local Web Server
- Run IIS without setting it up
- Step 2: Customize and Push to Docker Hub
- Step 3: Run a Multi-Service App
- Easily connect multiple services together
- Step 4: Orchestration: Swarm
- Adam the Automator
- How To Create A Docker Windows Image Using Docker Build
- Kevin Sapp
- Understanding Docker Container Images
- Understanding Docker Build and Images
- Prerequisites/Requirements
- Getting Prepared
- Downloading the IIS Windows Docker Image
- Reviewing the Current Docker Base Images
- Downloading the Base Image
- Inspecting the Dockerfile
- Building a New Docker Image
- Running the Docker Container
- Running Code Inside a Docker Container
- Inspecting the IIS Website
- Reviewing Docker History
- Cleaning up the Running Docker Images
Getting Started with Docker Desktop for Windows
The easiest way to create containerized applications and leverage the Docker Platform from your desktop
Overview
Thank you for installing Docker Desktop. Here is a quick 5 step tutorial on how to use. We will walk you through:
Running your first container
Creating and sharing your first Docker image and pushing it to Docker Hub
Create your first multi-container application
Learning Orchestration and Scaling with Docker Swarm and Kubernetes
Step 1: Local Web Server
Run IIS without setting it up
If you haven’t run Docker before, here’s a quick way to see the power of Docker at work. First, make sure you are using Windows Containers. Then from the command line, type
Then open http://localhost:8080 in your browser and see the default IIS web page. You can replace the default page with your own content by copying a file into the container. You need to stop the container first:
Next, in your favorite text editor, create a file called index.html in a new directory. Something as simple as
Copy that file into the container and it will overwrite the default IIS homepage:
Start the container again:
Refresh http://localhost:8080 and see your new content. You have created a web server without installing the web server. Docker took care of the dependencies.
When you’re finished, it’s good practice to stop and remove the running containers
Step 2: Customize and Push to Docker Hub
The last step used a Docker image which Microsoft publishes and maintains. Next step, create your own custom image. You should have a Docker ID, you probably created it to download Docker Desktop.
In your favorite text editor create a file called Dockerfile in the same C:\temp directory. No extension, just Dockerfile. Paste in this code and save the file:
This tells Docker to use the same IIS base image, and create a layer that adds in the HTML you created in the last step. Instead of manually copying a file into the container, you will create an image with your HTML already inside it. To build the image, in your terminal, type:
Two things, first replace YourDockerID > with your Docker ID. Also notice the “.” at the end of the line. That tells Docker to build in the context of this directory. So when it looks to COPY the file to /inetpub/wwwroot it will use the file from this directory.
And go to http://localhost:8081 to see the page — note your original container is still running and you can see it at http://localhost:8080.
Finally push to Docker Hub:
You may be asked to login if you haven’t already. Then you can go to hub.docker.com, login and check your repositories
Finally, stop and remove the running containers:
Step 3: Run a Multi-Service App
Easily connect multiple services together
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Docker Compose installs automatically with Docker Desktop.
A multi-container app is an app that has multiple containers running and communicating with each other. This sample uses a simple .Net Core web app running with a MySQL database. You can check out the app in our dockersamples GitHub repo. We’ve pushed two images to the Docker Hub under the dockersamples repo. Docker Compose handles service discovery directly, allowing the app to reference the service directly and Docker will route traffic to the right container. To try it out, open a text editor and paste the text from this file. Then save it as docker-compose.yml.
There’s a lot of details in there but basically you can see that it specifies the images to be used, the service names, application configuration, the ports available, and networks the different services are on.
To run it, open a command line and navigate to the same directory as the docker-compose.yml file. At the command line, type
You will see a bunch of commands go by as it pulls images from Docker Hub and then starts them up. When it has finished running, navigate to http://localhost. You should see a music album viewer. The .NET Core application saves data in the MySQL database — you don’t need .NET Core or MySQL installed, all the components are running in Docker.
To stop and remove all services and resources created by Docker Compose:
Step 4: Orchestration: Swarm
Before you start this section, make sure you’re using Linux containers. Swarm does support Windows containers, but they are much bigger and will take longer to pull.
While it is easy to run an application in isolation on a single machine, orchestration allows you to coordinate multiple machines to manage an application, with features like replication, encryption, load balancing, service discovery and more. If you’ve read anything about Docker, you have probably heard of Kubernetes and Docker swarm mode. Docker Desktop is the easiest way to get started with either Swarm or Kubernetes.
A swarm is a group of machines that are running Docker and joined into a cluster. After that has happened, you continue to run the Docker commands you’re used to, but now they are executed on a cluster by a swarm manager. The machines in a swarm can be physical or virtual. After joining a swarm, they are referred to as nodes.
Swarm mode uses managers and workers to run your applications. Managers run the swarm cluster, making sure nodes can communicate with each other, allocate applications to different nodes, and handle a variety of other tasks in the cluster.
Swarm uses the Docker command line or the Docker Compose file format with a few additions. Give it a try with a few simple steps.
First, copy the contents of this file into a file called docker-stack-simple.yml.
Then, from the command line in the same directory as that file, type the following commands.
You should see a command you could copy and paste to add another node to the swarm. For our purposes right now don’t add any more nodes to the swarm.
Docker will tell you that it is creating networks and services. And all connected to a stack called “vote”. You can see all stacks running on your Swarm by typing
Probably you have just the one, with 5 services. Next you can see all the services by typing
This will show you the 5 services, all with 1 replica. All with a name vote_ plus something like vote_db. You can run this a few times until all the replicas say 1/1.
So what happened? With the simple compose file format you created a application that has 5 components:
- A voting page in Flask that pushing results to redis.
- A redis instance to store key value pairs.
- A worker that goes into the redis instance, pulls out data and pushes it into the database.
- A postgres database.
- A results page running in Node.js that draws from the database.
Now, click on localhost:5000 to vote. You can vote for cats or dogs, whichever you like better. On localhost:5001 you can see the results of the vote. Open up different browsers to add in additional votes if you want.
The code for all these components is in our Example Voting App repo on GitHub. There’s a lot going on here but here are some points to highlight:
- The services all refer to each other by name. So when the result app calls on the database, it connects to `postgres@db` and the Swarm takes care of directory the service to `db`.
- In a multi-node environment, Swarm will spread out the replicas however you want.
- Swarm will also do basic load balancing. Here’s how you can see this in action:
Load localhost:5000 again. Note the “Processed by container ID “ at the bottom of the page. Now add a replica:
Once it is done verifying, reload the page a few times and see the container ID rotates between three different values. Those are the three different containers. Even if they were on different nodes, they would share the same ingress and port.
To clean up before moving to the next section, type
Adam the Automator
How To Create A Docker Windows Image Using Docker Build
Kevin Sapp
Read more posts by this author.
Are you new to Docker Windows Images? Are you currently working in a Windows shop and curious to learn about Docker builds for container images? You have come to the right place. The best way to learn about new something is by doing.
In this article, you are going to learn how to create your first Windows Docker image from a Dockerfile using the docker build command.
Let’s get started!
Table of Contents
Understanding Docker Container Images
For years, the only way to test or perform development on multiple operating systems (OS) was to have several dedicated physical or virtual machines imaged with the OS version of your choice. This methodology required more hardware and overhead to provision new machines for each software and OS specification.
However, these days the usage of Docker container images has grown partly due to the popularity of micro-service architecture. In response to the rise in Docker’s popularity, Microsoft has started to publicly support Docker images for several flagship products on their Docker Hub page. They have even added native support for images for Windows as a product feature in the Windows 10 and Windows Server 2016!
A Docker image is run on a container by using the Docker Engine. Docker images have many benefits such as portability (applicable to multiple environments and platforms), customizable, and highly scalable. As you can see below, unlike traditional virtual machines, the Docker engine runs on a layer between the host OS kernel and the isolated application services that are being containerized.
Understanding Docker Build and Images
The docker build **command can be leveraged to automate container image creation, adopt a container-as-code DevOps practice, and integrate containerization into the development cycle of your projects. Dockerfiles are simply text files that contain build instructions used by Docker to create a new container image that is based on an existing image.
The user can specify the base image and list of commands to be run when a container image is deployed or startup for the first time. In this article, you will learn how to create a Windows-based docker image from Dockerfile using a Windows container.
This process has several benefits over using a pre-built container image:
- You are able to rebuild a container image for several versions of Windows – which is great for testing code changes on several platforms.
- You will have more control over what is installed in the container. This will allow you to keep your container size to a minimum.
- For security reasons, you might want to check the container for vulnerabilities and apply security hardening to the base image
Prerequisites/Requirements
This article is a walkthrough on learning about learning how to build a Docker image using a Dockerfile. If you’d like to follow along, ensure that you have the following prerequisites in place.
- Docker for Windows installed. I’ll be using the Docker Community Edition (CE) version 2.1.0.4 in my environment.
- Internet access is needed for downloading the Docker images
- Windows 10+ Operating System (version 1709 is being used for this tutorial)
- Nested virtualization enabled
- 5 GB of free diskspace on your local machine
- PowerShell 5.0+
- This tutorial uses the Visual Studio Code IDE. However feel free to use what ever IDE you’d prefer.
Note: Be sure to enable Windows Containers Configuration when installing Docker.
Getting Prepared
You’ll first need a folder to store all of the Docker images and containers you’ll be building from those images. To do so, open a Powershell or cmd terminal (you’ll be using PowerShell throughout this article) and create a new directory called C:\Containers.
Once the folder is created, change to that directory. This puts the console’s current working directory to C:\Containers to default all downloads to this directory.
In this article, you’ll get a headstart. Most of the files to work through this project are already available. Once the folder is created, perform a Git pull to copy over the files needed for this article from the TechSnips Github repository to the C:\Containers folder. Once complete, check to make sure that the C:\Containers folder looks like below.
Downloading the IIS Windows Docker Image
The first task to perform is to download a “template” or base image. You’ll be building your own Docker image later but first, you need an image to get started with. You’ll be downloading the latest IIS and Windows Server Core Images that are required for this tutorial. The updated list of images can be found on the official Microsoft Docker hub image page.
Reviewing the Current Docker Base Images
Before downloading the image from the image repository, let’s first review the current Docker base images that you currently have on your local system. To do so, run a PowerShell console as Administrator and then type docker images . This command returns all images on your local system.
As you can see below, the images available are initially empty.
Downloading the Base Image
Now it’s time to download the base IIS image from Docker Hub. To do so, run docker pull as shown below. This process can take some time to complete depending on your internet speeds.
Now run docker images and you should have the latest Microsoft Windows Core IIS image available for this tutorial.
Inspecting the Dockerfile
In an earlier step, you had downloaded an existing Dockerfile for this tutorial. Let’s now take a look at exactly what that entails.
Open the C:\Containers\Container1\Dockerfile file in your favorite editor. The contents of this Dockerfile are used to define how the container image will be configured at build time.
You can see an explanation of what each piece of this file does in the in-line comments.
Building a New Docker Image
You’ve got the Dockerfile ready to go and a base IIS image downloaded. Now it’s time to build your new Docker image using the Dockerfile.
To build a new image, use the docker build command. This command creates the image. For this article, you can see below you’re also using the -t **option. This option allows you to give your new image a friendly tag name and also reference the Dockerfile by specifying the folder path where it resides.
Below you can see an example of ensuring the console is in the C:\Containers directory and then building a new image from the Dockerfile in the C:\Containers\Container1 directory.
Once started, you can see the progress of the command as it traverses each instruction in the docker file line by line:
Once done, you should now have a new Docker image!
Now run the docker images command to view the images that are available. You can see below an example of the container1 image created.
Note: The docker build —help command is a useful parameter to display detailed information on the docker command being run.
Running the Docker Container
At this point, you should have a new image created. It’s time to spin up a container using that image. To bring up a new container, use the docker run command.
The docker run command will bring up a new Docker container based on the container1 image that you created earlier. You can see an example of this below.
Notice that the -d parameter is used. This tells the docker runtime to start the image in the detached mode and then exit when the root process used to run the container exits.
When docker run completes, it returns the ID of the container created. The example below is capturing this ID into a $containerID variable so we can easily reference it later.
Once the container is brought up, now run the docker ps command. This command allows you to see which containers are currently running using each image. Notice below that the running image is automatically generated a nickname (busy_habit in this case). This nickname is sometimes used instead of the container ID to manage the container.
Running Code Inside a Docker Container
A new container is built from a new image you just created. Let’s now start actually using that container to run code. Running code inside of a Docker container is done using the docker exec command.
In this example, run docker exec to view PowerShell output for the Get-ChildItem command in the container using the command syntax below. This will ensure the instructions in the Dockerfile to remove the default IIS files succeeded.
You can see below that the only file that exists is index.html which means the default files were removed.
Now run the ipconfig command in the container to get the local IP address of the container image so that you can try to connect to the IIS website.
You can see below that ipconfig was run in the container just as if running on your local computer and has return all of the IP information.
Inspecting the IIS Website
Now it’s time to reveal the fruits of your labor! It’s time to see if the IIS server running in the Docker container is properly serving up the index.html page.
Open a browser and paste the IP4 Address found via ipconfig into the address bar. If all is well, you should see a Hello World!! message like below.
Reviewing Docker History
One useful command to use when working with Docker containers i the docker history command. Although not necessarily related to creating an image or container itself, the docker history command is a useful command that allows you to review changes made to the container image.
You can see below, that docker history returns all of the Dockerfile and PowerShell activity performed on the container1 container you’ve been working with.
Cleaning up the Running Docker Images
The steps below are used to cleanup all stopped containers running on your machine. This will free up diskspace and system resources.
Run the docker ps command to view a list of the containers running on your system:
Now stop the running containers using the docker stop command:
Finally you can permanently remove the stopped containers using the docker system prune command.