Containers can be used in a variety of ways. In most cases, you are going to want to build your own custom images, complete with an operating system, OS features, and applications. A great way to build your image is to use a Dockerfile containing the instructions for building a new image, and then use the docker build
command to create a customized container you can then run.
In this recipe, you use the container host, CH1
, that you set up in the Configuring a container host recipe.
Set-Location
to the folder on CH1
:$SitePath = 'C:RKWebContainer' $NIHT = @{ Path = $SitePath ItemType = 'Directory' ErrorAction = 'SilentlyContinue' } New-Item @NIHT | Out-Null Set-Location -Path $NIHT.Path
$SB = { # 2.1 create folder in the container $SitePath = 'C:RKWebContainer' $NIHT = @{ Path = $SitePath ItemType = 'Directory' ErrorAction = 'SilentlyContinue' } New-Item @NIHT | Out-Null Set-Location -Path $NIHT.Path # 2.2 Create a page for the site $PAGE = @' <!DOCTYPE html> <html> <head><title>Main Page for RKWeb.Reskit.Org</title></head> <body><p><center><b> HOME PAGE FOR RKWEBr.RESKIT.ORG</b></p> Containers and PowerShell Rock! </center/</body></html> '@ $PAGE | Out-File $SitePathIndex.html | Out-Null # 2.3 Create a new web site in the container that uses Host headers $WSHT = @{ PhysicalPath = $SitePath Name = 'RKWeb' HostHeader = 'RKWeb.Reskit.Org' } New-Website @WSHT } # End of $SB script block
$SB | Out-File $SitePathConfig.ps1
Invoke-Command -Computer DC1.Reskit.Org -ScriptBlock { $DNSHT = @{ ZoneName = 'Reskit.Org' Name = 'RKWeb' IpAddress = '10.10.10.221' } Add-DnsServerResourceRecordA @DNSHT } Resolve-DnsName -Name Rkweb.Reskit.Org
$DF = @" FROM mcr.microsoft.com/windows/servercore:1809 LABEL Description="RKWEB Container" Vendor="PS Partnership" Version="1.0.0.42" RUN powershell -Command Add-WindowsFeature Web-Server WORKDIR C:\RKWebContainer COPY Config.ps1 Config.ps1 RUN powershell -Command .config.ps1 "@ $DF | Out-File -FilePath .Dockerfile -Encoding ASCII
docker build -t rkwebc .
docker run -d -t --name rkwebc -p 80:80 rkwebc | Out-Null
rkwebc
container is running:docker ps
Invoke-WebRequest
:Invoke-WebRequest -UseBasicParsing HTTP://RKweb.Reskit.Org
Start-Process 'HTTP://RKWeb.Reskit.Org'
docker container rm rkwebc -f | Out-Null
In step 1, you create a folder, C:RKWebContainer
in CH1
, to all the files needed in this recipe. In step 2, you create a script block that you later use to create a website in the container. In step 3, you save this script block to a file on CH1
. In step 4, you set up a new DNS A record for RKweb.Reskit.Org
that points to the same IP address as the CH1
host. In step 5, you create a file, Dockerfile
, that contains the Docker image-building instructions. These steps produce no output.
In step 6, you use the docker build
command to build a customized image, rkwebc
, on CH1
. The output looks like this:
With your image created, in step 7, you run the image as a container, which produces no output. In step 8, you use the docker ps
command to view the rkwebc
container running on CH1
, which looks like this:
In step 9, you use Invoke-WebRequest
to view the newly created website. The output looks like this:
In Step 10, you view the site in the browser, which looks like:
In this recipe, you use a base container image that you have to download from the Docker registry (mcr.microsoft.com/windows/servercore:1809
). Then, you build a container that has the web server feature added and in which you can run the Config.ps1
file to configure the container to run your website. For more information on Dockerfiles, see this link: https://docs.docker.com/engine/reference/builder/.
In this recipe, you build and run a Dockerfile to create an image. In the Dockerfile, you use the RUN
instruction to run PowerShell twice during the building of the image. To optimize the container, you could run the two commands (to add the Windows feature and to run the Config.Ps1
script) as a single Docker image. For some tips on how to build container images, see https://cloud.google.com/blog/products/gcp/7-best-practices-for-building-containers.
3.135.196.172