Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Task 02 - Build a Docker container for the app

Introduction

You’ll run the legacy Contoso Hotel app in a Docker container. Docker is an open-source platform that automates the deployment, scaling, and management of applications. It uses containerization technology to package an application and its dependencies into a container, ensuring that it runs consistently across different environments.

Description

In this task, you’ll create the Docker container and add app components to the container.

The key steps are as follows:

  1. Launch Docker Desktop.
  2. Verify that the Docker engine is running.
  3. Build a Docker container for the app.

Success Criteria

  • The Docker engine is running.
  • You’ve built a Docker container for the app.

Learning Resources

Solution

Expand this section to view the solution
  1. Open Docker Desktop. Wait for the app to start. After the app starts, wait for the app to start Docker Engine.

    x2zpfcur.png

    After Docker Engine starts, the Docker UI should resemble the following screenshot. Docker will display any running containers.

    3vhwsgbg.png

  2. Minimize Docker Desktop but don’t close the app.

  3. Enter the following command at the Visual Studio Terminal window prompt and select Enter. This command allows you to run commands as an administrator.

     Start-Process powershell -Verb runAs
    
  4. In the User Account Control window that displays, select Yes. A PowerShell window opens.

    hj3c4ve2.png

  5. Enter the following command at the PowerShell prompt and then select Enter. This command configures the Docker daemon to start automatically.

     Set-Service -Name com.docker.service -StartupType Automatic
    

    The Set-Service command will fail if you’re not running PowerShell as an administrator.

  6. Enter the following command at the PowerShell prompt and then select Enter. This command manually starts the Docker daemon.

     Start-Service -Name com.docker.service
    

    ld27t68z.png

  7. Enter the following command at the PowerShell prompt and then select Enter. This command checks the status of the Docker daemon. Verify that the results show the Docker daemon is running.

     Get-Service -Name com.docker.service
    

    2w42g4so.png

  8. Minimize the PowerShell window. Return to Visual Studio Code.

  9. Modify the file path, if needed, for the following command to point to the Downloads\ContosoHotel folder that you created. Enter the command at the Visual Studio Code Terminal window and then select Enter. This command switches the context to the folder where the cloned repository resides.

     cd $PATH_TO_DOWNLOADS_FOLDER\ContosoHotel
    

    m6q69ffk.png

  10. Before building the container, we must update the Dockerfile to use a compatible Python Docker image. In Visual Studio Code, select File, then Open File

  11. Navigate to Downloads\ContosoHotel, then open Dockerfile

    e98uxuml.jpg

  12. Change line 2 to the following:

     FROM python:3.11-slim-bookworm
    

    vuqrzy2k.jpg

    The latest Python base image used in the line before, may be incompatible with other packages in the lab.

  13. Save your changes.

  14. Enter the following command at the Terminal window prompt and then select Enter. This command builds the container for the app. Wait while the container builds.

     docker build -t "pycontosohotel:v1.0.0" .
    

    yhdwim2f.png

    It may take 2-3 minutes to build the container.

  15. Leave Visual Studio Code open. You’ll use the tool in the next task.