(C# ASP.NET Core) How to run a Docker Image with Docker Desktop

This is a walkthrough on running a docker image. We start by explaining the commands for listing the docker images on a computer. Then we explain the commands for running a docker image, and for gracefully shutting down the container. Finally, we examine that when the images are run in this manner, the data is not persisted to disk.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Run the Docker Desktop

An image has to be run with the help of the Docker program. So first of all double click the docker shortcut icon to ensure that the window opens. This causes all the necessary background services to start.

You can close the window because we shall be using the commandline.

Video Explanation

Please watch the following youtube video:

List all Images

Open a command prompt and run the command - docker images - to list all the images we have on our local computer.


docker images 

Verify that the image we created in the previous tutorial is among the list.

The images are listed under the headings - REPOSITORY, TAG, ID, CREATED and SIZE

REPOSITORY is the name of the image. The name of the image is by default picked from the name of your project. A TAG is an identifier for an image. The default tag is called latest.

IMAGE ID is the first few characters of an SHA256 digest of the image configuration.

Run the container

KEEP VISUAL STUDIO and IIS Server/Kestrel everything CLOSED because we will be using docker to run our image now!

Open a command prompt and type the command -


docker run --name mycontainer --rm -d -p 43190:80 hellodocker
  

Let us learn the various parts of this command.

The parameter --name is the name of your container. It will be used at the time of stopping the container.

The parameter --rm means remove the container from memory after it is stopped.

The parameter -d means run in the background. We will later test it in the browser.

The parameter -p maps the port 43190 to port 80. It effectively means that your app will be available on port 43190.

Now hit the enter key to run this command!

Verify the app in the Browser

Open the browser to verify that your app is now running inside the browser.

Enter some data and see everything is functioning. Your data is being saved. The container has provided a RW memory for saving the data.

Stop the Container

Open a command prompt and type the command -


docker stop mycontainer
  

Try to open the app again on the browser.

The app fails to open because the container has been removed.

The Problem of Persistence

Open a command prompt and again type the command -


docker run --name mycontainer --rm -d -p 43190:80 hellodocker
  

Open the app again.

The app opens but the data is not persisted. It is because the app was running in memory. The data has not been persisted to disk.

We shall address this issue in our next tutorial. Thanks!


This Blog Post/Article "(C# ASP.NET Core) How to run a Docker Image with Docker Desktop" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.