Good day, Cloud Gurus,
and welcome back for this lesson
jam packed with demos,
where we're going to take a look at
Managing Windows Containers.
Let's take a look at what we're going to cover.
First up, we'll take a look at the Docker Commands
using the Docker CLI.
Then we'll take a look at
creating your own container images.
And then finally,
we'll wrap up with a demo where we will manage containers
with Windows Admin Center.
Let's take a look.
All right, now let's take a look at some Docker commands
using the Docker CLI.
Now all the Docker Commands have a similar syntax.
They all start with a Docker-based command
and then they're followed by a child command.
And from there, what commands you'll need to provide,
depends on which child command you've decided to use.
Sometimes you'll need to provide another child command
and other times you'll need to provide
options for the command.
And sometimes you'll also need to provide a target
like an image or a container.
But what I found confusing
when I first started using the Docker CLI,
was the names for some of the commands
and that's because Docker has full commands
and then shorthand aliases for the most common commands.
Let's take a look at some of the common
container management tasks
and the commands you can run to perform that task.
Let's say you want to pull an image
from a repository and store it locally.
You can do that with the docker pull command
and you can provide the name of the image
and optionally the tag for the image.
And if you are pulling from a repository
other than the Docker Hub,
you can specify a full URI to the image.
For example, to pull the asp.net core sample app
you can run the docker pull command,
specify the URI to the image, and then specify the tag.
In this example, the name of the image is samples
and the tag is aspnetapp.
You can also list the images on your container host
by running the Docker image subcommand
and using either the list or the shorthand command ls,
and Docker images also works.
There's almost too many choices for the command to run
to list your images on your Docker host.
And to run a container image,
you can use the docker run command
and specify the image you want to run.
There are also a number of useful options
for the docker run command,
which we'll take a look at in the demo in just a moment.
And you can stop running a container
with the docker stop command,
and specify either the container ID or the container name.
And there's also a docker kill command,
which forcibly stops a container.
Now let's take a quick look at a demo
of some of these commands on our Docker host.
All right, so here we are on our container host
that we deployed in the previous lesson,
let's pull a sample container image
from the Microsoft container registry and run it.
To do that,
we'll use the docker pull command
we'll specify the location,
the image name, which is samples,
and the tag, which is aspnetapp.
We can go ahead and run that.
Now, Docker will go ahead and pull
all of the layers for our image down to our container host.
This will just take a moment to complete,
so we'll come back once this is finished downloading.
All right, the download of that image is now complete.
Now let's list the images available
on our container host with the docker image list command.
And you remember, we can just run ls
but we'll run lists
and we can see we have that aspnetapp
sample container image.
Now let's run our container image
using the docker run command,
and we'll specify the d parameter,
which means it'll run in the background
and not in this terminal.
And we'll specify the p perimeter to publish ports
from our container to our host.
In this example, we're publishing port 80
on our container to port 8000 on our host.
And then finally,
we're specifying the container image that we want to run.
We can go ahead and run that command
and you can see how quickly that container started up.
That's so much faster than a virtual machine.
Now let's go ahead and run the docker ps command
to list the running containers.
And we can see, we have our container image running here
and we have a container name and a container ID.
All right, now let's go ahead
and use Microsoft Edge to access the website
that's hosted in our container.
Go ahead and launch Microsoft Edge
and we'll paste in the URL to our container,
which is just localhost and then port 8000.
And we can see the asp.net sample app running
from our container.
Now let's go ahead and stop and remove our container.
So we'll go ahead and close Edge
and we'll stop our container with the docker stop command,
and then we'll provide the container ID
and I'll just copy their container ID from here,
paste that in.
All right, and we've now successfully stopped our container.
Now let's go ahead and remove our container
using the docker rm alias
and we'll paste in our container ID again,
now let's confirm that our container has been removed
using the docker ps command
and we'll specify the a parameter that lists all containers,
including stopped containers.
All right, and we can see we have no containers running
on our host anymore.
That concludes this demo.
Let's continue on with the lesson.
Now, as we mentioned in the previous lesson,
you can use a Dockerfile
to create a custom container image.
So let's take a look at creating a Dockerfile.
Firstly, you need to create your Dockerfile
and the file doesn't have a file extension.
Just the name Dockerfile.
Then you use the FROM instruction to specify the base image
for your container.
The FROM instruction is required for all Dockerfiles
and should be the first line in your file.
In this example,
we're setting the base image to be windows servercore 2022
and then you can use a variety
of different instructions to build your image.
Let's take a look at some of the key instructions
to be aware of.
The run instruction will run a command that you specify.
In this example, we're executing PowerShell
and specifying to run the command
to install internet information services, which is IIS,
which is the Windows web server role.
The WORKDIR instruction
is used to set the working directory
for any subsequent commands.
In this example,
we're setting the working directory to the root folder
for the default site in IIS and in Windows,
we have to double our back slashes to escape them,
otherwise they'll be ignored.
The copy instruction is used to add files
to your container image.
In this example, we're adding the index.html file
from our local working folder to our remote working folder,
which is the directory we just set
using the WORKDIR command.
And the add instruction works similar
to the copy instruction
and is used to add files to your image
but it has the advantage of being able to add files
from a remote URL like we're doing here.
We're adding the service monitor executable
that is used to monitor IIS
when it's running in a container.
And the EXPOSE instruction is used to expose network ports
from the container to the container host.
And finally, a Dockerfile should specify at least one CMD
or ENTRYPOINT instruction.
ENTRYPOINT should be defined when using the container
as an executable and command should be used
as a way of defining default arguments
for an entry point or executing command's ad hoc
in a container.
Now, normally you wouldn't have this many commands
in a Dockerfile if you can help it.
Each command creates a layer in your image
and so it can increase the size of your container image.
So you should keep your Dockerfiles as short
and sweet as possible.
Let's take a look at a demo
of creating and running a container,
using an image we create with a Dockerfile.
All right, so here we are back on our container host.
Before we go ahead and create our own Dockerfile,
let's first take a look at the Dockerfile
that Microsoft uses create the IIS base container image.
We'll go ahead and launch Edge
and we'll navigate to the GitHub repository
that contains Microsoft's Dockerfile,
and we can see here
that Microsoft is using the FROM instruction
to specify that this container image
should use windows servercore 2022 as the base image.
Then they're using the run instruction
to run some PowerShell commands,
they're installing the IIS web server role,
and they're using invoke web request to download
the service monitor executable,
and store it in C:Servicemonitor.exe.
Then they're using the EXPOSE instruction
to expose port 80 from this container,
and they're using the entry point instruction
to specify that this container should run servicemonitor.exe
when it starts
and monitor the world wide web publishing service.
Let's go ahead and create our own Dockerfile now,
so we'll close this and then we'll go file new.
We'll just create a text file
and we'll save this as Dockerfile.
And I'm just using the quotes when I'm saving this file
to make sure I don't get a file extension for this file.
The name needs to be just Dockerfile with no extension.
And I'm saving in the C app directory,
which is the directory that contains the source files
for our application.
So I'll go ahead and select Save.
Now, I'll go ahead and paste in the Dockerfile
that I've created earlier to see
you don't have to wait for me to type that out.
You can see here, I'm using the FROM instruction
to use that IIS image that we just took a look at.
And then I'm using the WORKDIR instruction
to set the working directory
to the directory used by the default site.
And then I'm copying in index.html to index.html
in that wwwroot directory.
We can go ahead and save this.
Now let's go ahead and build our container image
using the docker build command.
But first we need to change our working directory
in PowerShell, to that c app directory
where our index.html file is.
All right, now let's go ahead and run docker build
and we'll specify the tag for our container image.
We'll just use mycontainerimage
and we'll specify the source directory for our files
which is the current directory.
So we'll use the dot for that.
We'll go ahead and press Enter.
Now, it's going ahead
and pulling the IIS server core base image.
And then we'll go ahead and run these commands
and build our container image.
This will just take a moment to complete.
So we'll come back when this is done.
All right, our container image has been successfully built
with the name mycontainerimage
and it's being tagged automatically using the latest tag.
Let's go ahead and list the container images
on this host now.
To do that,
we'll use the docker images command.
All right, and then we have the container image
that we just built
and the base image that was used to build our container
and the image that we used in our previous demo.
Now let's go ahead and run a new container
based on the image that we just created.
To do that,
we'll use the docker run command and we'll use port 8000
to expose port 80 from the container.
We'll run the container detached
and we'll specify the container image,
which is mycontainerimage and the latest tag.
Go ahead and run that.
All right, and that image is now running.
Now let's launch Microsoft Edge
and take a look at our website.
Go ahead and launch Microsoft Edge.
And then we'll navigate to localhost 8000,
which is the port we exposed from our container.
And then we have the HTML page
that we put in our container image.
All right, that concludes this demo.
Let's continue on with the lesson.
Now you can also manage containers
with Windows Admin Center
and there aren't any special requirements here.
If you have Windows Admin Center
and a server with Docker installed,
you can manage the images and containers on that server
using Windows Admin Center.
Let's take a look.
In this demo,
we will install the containers extension
in Windows Admin Center on our Windows Admin Center server.
And we'll use the extension to manage containers
on our container host
including containers and container images.
It's kind of neat. Let's take a look.
All right,
so here we are on our Windows Admin Center server.
Let's get started by installing the containers extension.
So we'll navigate to Settings,
and we'll navigate to Extensions,
and we'll select the Containers extension.
And then we'll go ahead and install that extension.
This will just take a moment to install
and this page will reload once it's completed.
All right, now let's navigate to our container host.
So we'll navigate back to the Windows Admin Center homepage
and then we'll navigate into our container host
and we'll just wait a moment for all the tools to load up
and we'll navigate to the containers node,
And we get a nice overview page here
with the number of images we have on our server,
and the number of containers we have running.
Let's navigate into the containers node.
And we see the container that we have running
from our previous demo.
Now let's go ahead and navigate to the images node
and we can see the container images,
including that mycontainerimage that we created previously.
Let's go ahead and select create new.
And from here, you can use Windows Admin Center
to create Dockerfiles and create new container images.
We'll go ahead and hit Cancel.
Let's go ahead and run another instance
of the IIS container image from Microsoft.
So we'll go ahead and select the IIS container image.
And then we'll go ahead and select Run.
We'll provide a name for this container.
We'll use mynewcontainer for the name
and you can see here that you can use Hyper-V isolation
which would run your container
in a lightweight virtual machine
and provide a greater level of isolation for your container.
We'll go ahead and publish the ports.
We'll publish port 80 on port 8001.
And then we can go ahead and click Run.
We'll just give that container a moment to start
and then we'll navigate back to containers
and we can see that container is now running
on our container host.
All right, that concludes this demo.
Let's go ahead and wrap up
with a bit of a summary
of what we've covered in this lesson.
You can use the docker build command
to create a container image from a Dockerfile.
And in a Dockerfile,
you use the FROM instruction to set the base image
for your container and all Dockerfiles require
a FROM instruction.
And in a Dockerfile,
you use the CMD and ENTRYPOINT instructions
to set the default executable for your container.
And it's important to remember
that you can use Windows Admin Center
to manage your container instances
and images on your Windows container hosts.
All right, that concludes this lesson.
Thanks for being with me.
I hope you found this useful
and I look forward to seeing you on the next lesson.