Docker on Windows
eBook - ePub

Docker on Windows

Elton Stoneman

Share book
  1. 309 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Docker on Windows

Elton Stoneman

Book details
Book preview
Table of contents
Citations

About This Book

Learn how to run new and old Windows applications in Docker containers.About This Book• Package traditional.NET Frameworks apps and new.NET Core apps as Docker images, and run them in containers for increased efficiency, portability, and security• Design and implement distributed applications that run across connected containers, using enterprise-grade open source software from public Docker images• Build a full Continuous Deployment pipeline for a.NET Framework application, and deploy it to a highly-available Docker swarm running in the cloudWho This Book Is ForIf you want to modernize an old monolithic application without rewriting it, smooth the deployment to production, or move to DevOps or the cloud, then Docker is the enabler for you. This book gives you a solid grounding in Docker so you can confidently approach all of these scenarios.What You Will Learn• Comprehend key Docker concepts: images, containers, registries, and swarms• Run Docker on Windows 10, Windows Server 2016, and in the cloud• Deploy and monitor distributed solutions across multiple Docker containers• Run containers with high availability and fail-over with Docker Swarm• Master security in-depth with the Docker platform, making your apps more secure• Build a Continuous Deployment pipeline by running Jenkins in Docker• Debug applications running in Docker containers using Visual Studio• Plan the adoption of Docker in your own organizationIn DetailDocker is a platform for running server applications in lightweight units called containers. You can run Docker on Windows Server 2016 and Windows 10, and run your existing apps in containers to get significant improvements in efficiency, security, and portability.This book teaches you all you need to know about Docker on Windows, from 101 to deploying highly-available workloads in production. This book takes you on a Docker journey, starting with the key concepts and simple examples of how to run.NET Framework and.NET Core apps in Windows Docker containers. Then it moves on to more complex examples—using Docker to modernize the architecture and development of traditional ASP.NET and SQL Server apps.The examples show you how to break up monoliths into distributed apps and deploy them to a clustered environment in the cloud, using the exact same artifacts you use to run them locally. To help you move confidently to production, it then explains Docker security, and the management and support options.The book finishes with guidance on getting started with Docker in your own projects, together with some real-world case studies for Docker implementations, from small-scale on-premises apps to very large-scale apps running on Azure.Style and approachUsing a step-by-step approach, this book shows you how to use Docker on Windows. It includes practical examples and real-world technical and business scenarios that will help you effectively implement Docker in your environment.There are over 50 examples of Dockerized applications, using C#.NET projects as the source and packaging them into Docker images.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Docker on Windows an online PDF/ePUB?
Yes, you can access Docker on Windows by Elton Stoneman in PDF and/or ePUB format, as well as other popular books in Computer Science & System Administration. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781785288425
Edition
1

Packaging and Running Applications as Docker Containers

Docker reduces the logical view of your infrastructure to three core components: hosts, containers, and images. Hosts run containers, which are isolated instances of an application. Containers are created from images, which are packaged applications. The Docker container image is conceptually very simple - it's a single unit that contains a complete, self-contained application. The image format is very efficient, and the integration between the image and the runtime is very smart, so mastering images is your first step to using Docker effectively.
You've already seen some images in Chapter 1, Getting Started with Docker on Windows, by running some basic containers to check your Docker installation was working correctly - but I didn't look very closely at the image or how Docker used it. In this chapter, you'll get a thorough understanding of Docker images: learning how they're structured, understanding how Docker uses them, and looking at how to package your own applications as Docker images.
The first thing to understand is the difference between an image and a container, which you can see very clearly by running different types of container from the same image.
In this chapter, you'll get a lot of experience of the Docker basics:
  • Running containers from images
  • Building images from Dockerfiles
  • Packaging your own applications as Docker images
  • Working with data in images and containers
  • Packaging legacy ASP.NET web apps as Docker images

Running a container from an image

The docker container run command creates a container from an image and starts the application inside the container. It's actually equivalent to running two separate commands, docker container create and docker container start, which shows that containers can have different states. You can create a container without starting it, and you can pause, stop, and restart running containers. Containers can be in different states, and you can use them in different ways.

Doing one thing with a task container

The dockeronwindows/ch02-powershell-env image is an example of a packaged application that is meant to run in a container and perform a single task. The image is based on Microsoft Nano Server and is set up to run a simple PowerShell script when it starts, printing details about the current environment. Let's see what happens when I run a container directly from the image:
 > docker container run dockeronwindows/ch02-powershell-env
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\ContainerAdministrator\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME 361CB712CB4B
...
Without any options, the container runs a PowerShell script that is built into the image, and the script prints out some basic information about the operating system environment. I call that a task container because the container performs one task and then exits. If you run docker container ls, which lists all the active containers, you won't see this container. But if you run docker container ls --all, which shows containers in all states, you'll see it in the Exited status:
 > docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
361cb712cb4b dockeronwindows/ch02-powershell-env "powershell.exe c:..." 30 seconds ago Exited
Task containers are very useful in automating repetitive tasks - like running scripts to set up an environment, backing up data, or collecting log files. Your container image packages the script to run, along with the exact version of the engine that the script needs, so anyone with Docker installed can run that script without having to install the engine.
This is especially useful for PowerShell, where scripts can be dependent on several PowerShell modules. The modules may be publicly available, but your script is dependent on specific versions. Instead of sharing a script that requires users to install the correct version of many different modules, you build an image that has the modules already installed. Then, you only need Docker to run the script task.
Images are self-contained units, but you can also use them as a template. An image may be configured to do one thing, but you can run containers from the image in different ways to do different things.

Connecting to an interactive container

An interactive container is one that has an open connection to the Docker command line, so you work with the container as if you were connected to a remote machine. You can run an interactive container from that same Nano Server image by specifying the interactive options and a command to run when the container starts:
 > docker container run --interactive --tty dockeronwindows/ch02-powershell-env `
powershell

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\> Write-Output 'This is an interactive container'
This is an interactive container
PS C:\> exit
The --interactive option runs an interactive container, and the --tty flag attaches a dummy terminal connection to the container. The powershell statement after the name of the container image is the command to run when the container starts. By specifying a command, you replace the startup command that's been set up in the image. In this case, I start a PowerShell session, and that runs instead of the configured command, so the environment printout script doesn't run.
An interactive container keeps running as long as the command inside is running. While you're connected to PowerShell, running docker container ls on another window on the host will show the container is still running. When you type exit in the container, the PowerShell session ends, so there's no process running and the container exits too.
Interactive containers are useful when you're building your own container images, as you can work through the steps interactively first and verify that everything will work as you expect. They're good exploratory tools too. You'll see as you move further into this book that Docker can host complex distributed systems in a virtual network, with each component running in its own container. If you want to examine parts of the system, you can run an interactive container inside the network and check on individual components, without having to make the parts publicly accessible.

Keeping a process running in a background container

The last type of container is the one that you'll use most in production - the background container, which keeps a long-running process running in the background. It's a container that behaves like a Windows Service. In Docker terminology, it's called a detached container, and it's the Docker service that keeps it running in the background. Inside the container, the process runs in the foreground. The process might be a web server or a console application polling a message queue for work, but as long as the process keeps running, Docker will keep the container alive.
I can run a background container from the same image again, specifying the detach option and a command that runs for some minutes:
 > docker container run --detach dockeronwindows/ch02-powershell-env ` 
powershell Test-Connection 'localhost' -Count 100

ce7b2604f681871a8dcd2ffd8898257fad26b24edec7135e76aedd47cdcdc427
In this case, when the container has launched control returns to the terminal; the long random string is the ID of the new container. You can run docker container ls and see the container running, and the docker container logs command shows you the console output from the container. For commands operating on specific containers, you can reference them by the container name or by part of the container ID:
 > docker container logs ce7

Source Destination IPV4Address IPV6Address
------ ----------- ----------- -----------
CE7B2604F681 localhost
CE7B2604F681 localhost
The --detach flag detaches the container so it moves into the background, and the command in this case just pings localhost repeatedly one hundred times. After a few minutes, the PowerShell command completes so there's no process running and the container exits. That's a key thing to remembe...

Table of contents