Hello-world image

Hello world image creation

Let us create the image by following the below steps.

Step 1: Creating the C# Console Application

  1. Open a command prompt or terminal.

  2. Navigate to the desired location where the project will be created.

  3. Create a new console application by running the following command:

    dotnet new console -n HelloApp

    This command creates a new C# console application named "HelloApp".

Step 2: Building and Testing the Application

  1. Navigate into the project folder using the command:

    cd HelloApp
  2. Build the project by executing the following command:

    dotnet build

    Ensure that the build process completes without any errors.

  3. Test the application by running the command:

    dotnet run

    Confirm that the "Hello World!" message is displayed successfully.

Step 3: Creating a Dockerfile

  1. In the project folder ("HelloApp"), create a new file named Dockerfile (without any file extension).

  2. Open the Dockerfile and add the following content:

    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
    WORKDIR /app
    
    COPY . .
    RUN dotnet publish -c Release -o out
    
    FROM mcr.microsoft.com/dotnet/runtime:7.0
    WORKDIR /app
    COPY --from=build /app/out .
    
    ENTRYPOINT ["dotnet", "HelloApp.dll"]

What happens to the build stage if I also have a runtime stage in an image?

Once the build stage completes its tasks, it is discarded, and only the artifacts copied to the runtime stage are retained in the final image. This helps to keep the final image size minimal by excluding any unnecessary build tools and dependencies.

By using a multi-stage build, you can separate the build environment from the runtime environment and produce a final image that only contains the essential runtime components. This approach offers several benefits, including smaller image sizes, improved security, and easier deployment. Step 4: Building the Docker Image

  1. Open a terminal or command prompt and navigate to the project folder ("HelloApp") containing the Dockerfile.

  2. Build the Docker image using the following command (replace your-image-name with a name of your choice):

    docker build -t hello-dotnet .

    This command builds the Docker image based on the instructions specified in the Dockerfile. The -t flag assigns a tag or name to the image.

Step 5: Running the Docker Container

  1. Run the Docker container based on the image you just built with the following command (replace your-image-name with the chosen image name):

    docker run hello-dotnet

    The container will be created and executed, running the C# console application inside it. The "Hello World!" message should be displayed in the terminal.

Where is the image file?

The location of the image on your system will depend on your Docker configuration and the operating system you are using. Docker manages the storage and organization of images internally, so you typically don't need to access the image files directly in the filesystem!

But if you issue the command to list the images you should see something like this:

docker images

Exercise

In a similar fashion, try to build a countdown app which will not exit immediately. Steps are shown in the official tutorial below:

Last updated