This week I have to give an introductory talk on DevOps and Docker and therefore I decided to prepare a simple Step by step: ASP.NET Core on Docker sample.

Assuming you have Docker installed and running, follow these 4 simple steps:

Create a dockerfile


On your Docker box create a dockerfile with the following contents

 1    # We use the microsoft/dotnet image as a starting point.
 2    FROM microsoft/dotnet
 3
 4    # Install git
 5    RUN apt-get install git -y
 6
 7    # Create a folder to clone our source code
 8    RUN mkdir repositories
 9
10    # Set our working folder
11    WORKDIR repositories
12
13    # Clone the source code
14    RUN git clone https://github.com/cmendible/aspnet-core-helloworld.git
15
16    # Set our working folder
17    WORKDIR aspnet-core-helloworld/src/dotnetstarter
18
19    # Expose port 5000 for the application.
20    EXPOSE 5000
21
22    # Restore nuget packages
23    RUN dotnet restore
24
25    # Start the application using dotnet!!!
26    ENTRYPOINT dotnet run

Create a docker image


With the dockerfile in place run the following command

1   sudo docker build -t hello_world .

Now you have an image named hello_world with all the dependencies and code needed to run the sample.

Test the Docker image


To test the Docker image run the following command

1   docker run -it -p 5000:5000 hello_world

If everything runs as expected you can head to your browser and navigate to http://localhost:5000 and confirm that the application is running!

Run the Docker image as a daemon process


Now that you know that everything is working as expected use the following command to run the Docker image as a daemon process

1   docker run -t -d -p 5000:5000 hello_world

Feel free to logout or close the connection with your Docker box and the application will keep running.

You can get a copy of the docker file here: https://github.com/cmendible/dotnetcore.samples/tree/main/docker.helloworld

Hope it helps!