Using a Dockerfile to create and use a container

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.

Getting ready

In this recipe, you use the container host, CH1, that you set up in the Configuring a container host recipe.

How to do it...

  1. Create a folder and 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
  2. Create a script to run in the container to create a new site in the container:
    $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
  3. Save the script block to the file:
    $SB | Out-File $SitePathConfig.ps1
  4. Create and test a new A record for our soon-to-be containerized site:
    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
  5. Create a Dockerfile that contains build instructions:
    $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
  6. Build the image:
    docker build -t rkwebc .
  7. Run the image:
    docker run -d -t --name rkwebc -p 80:80 rkwebc | Out-Null
  8. Check that the rkwebc container is running:
    docker ps
  9. Get the page using Invoke-WebRequest:
    Invoke-WebRequest -UseBasicParsing HTTP://RKweb.Reskit.Org
  10. View the page using a browser:
    Start-Process 'HTTP://RKWeb.Reskit.Org'
  11. Clean up forcibly:
    docker container rm rkwebc -f | Out-Null

How it works...

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:

How it works...

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:

How it works...

In step 9, you use Invoke-WebRequest to view the newly created website. The output looks like this:

How it works...

In Step 10, you view the site in the browser, which looks like:

How it works...

There's more...

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/.

See also

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.196.172