Exercise - Push a microservice image to Docker Hub

Completed

In order for Kubernetes to create a container image, it needs a place from which to get it. Docker Hub is a central place to upload Docker images. Many products, including Kubernetes, can create containers based on images in Docker Hub.

Note

You will complete this exercise in a GitHub Codespace that has Docker and the .NET SDK pre-installed. When you use these techniques in your own development environment, make sure you have these prerequisites installed.

Open the development environment

You can choose to use a GitHub codespace that hosts the exercise, or complete the exercise locally in Visual Studio Code.

To use a codespace, create a preconfigured GitHub Codespace with this Codespace creation link.

The process takes several minutes while GitHub creates and configures the codespace. Once finished, the code used for the rest of this module is in the /dotnet-kubernetes directory.

To use Visual Studio Code, clone the https://github.com/MicrosoftDocs/mslearn-dotnet-cloudnative repository to your local machine. Then:

  1. Install any system requiements to run Dev Container in Visual Studio Code.
  2. Make sure Docker is running.
  3. In a new Visual Studio Code window open the folder of the cloned repository
  4. Press Ctrl+Shift+P to open the command palette.
  5. Search: >Dev Containers: Rebuild and Reopen in Container
  6. Select eShopLite - dotnet-kubernetes from the drop down. Visual Studio Code creates your development container locally.

Verify the Docker images by creating containers in the codespace

There are two containers in the Contoso Shop project. Before pushing the images to Docker Hub, let's use them to create the containers in the codespace. After the containers are created and running, we'll be able to browse the Contoso company website and verify the microservices are running OK.

Follow these steps to create and run Docker containers in the codespace.

  1. Switch to the TERMINAL tab and run the following command to go to the code root:

    cd dotnet-kubernetes
    
  2. Run the following command to build the containers:

    dotnet publish /p:PublishProfile=DefaultContainer
    

    It might take a while to build the containers.

  3. Run the following command to run the app and attach the containers:

    docker compose up
    
  4. To test the front end service, switch to the PORTS tab, then to the right of the local address for the Front End port, select the globe icon. The browser displays the homepage.

  5. Select Products. The catalog shows Contoso's merchandise.

  6. Close the web site, return to the TERMINAL tab, and then press CTRL + C. Docker compose halts the containers.

Sign in to Docker Hub

The next step in uploading the images to Docker Hub is to sign into Docker Hub. From the command prompt, enter the following:

docker login

Important

Use the same username and password from when you created your Docker account. You can visit the Docker Hub website to reset your password, if needed.

Upload the images to Docker Hub

  1. Enter the following code to retag or rename the Docker images you created under your Docker username.

    docker tag store [YOUR DOCKER USER NAME]/storeimage
    docker tag products [YOUR DOCKER USER NAME]/productservice
    
  2. Then finally upload, or push, the Docker images to Docker Hub.

    docker push [YOUR DOCKER USER NAME]/storeimage
    docker push [YOUR DOCKER USER NAME]/productservice
    

    If you receive an authentication error, you can run docker logout followed by docker login to reauthenticate.

In this exercise, you used Dockerfiles and docker compose to create two Docker images and containers, and pushed those images to Docker Hub.

Now, you're ready to use Kubernetes to manage Contoso's microservices deployment.