Step by step: ASP.NET Core on Docker

Step by step: ASP.NET Core on Docker

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

    # We use the microsoft/dotnet image as a starting point.
    FROM microsoft/dotnet

    # Install git
    RUN apt-get install git -y

    # Create a folder to clone our source code
    RUN mkdir repositories

    # Set our working folder
    WORKDIR repositories

    # Clone the source code
    RUN git clone https://github.com/cmendible/aspnet-core-helloworld.git

    # Set our working folder
    WORKDIR aspnet-core-helloworld/src/dotnetstarter

    # Expose port 5000 for the application.
    EXPOSE 5000

    # Restore nuget packages
    RUN dotnet restore

    # Start the application using dotnet!!!
    ENTRYPOINT dotnet run

Create a docker image


With the dockerfile in place run the following command

   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

   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

   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!

Last modified December 12, 2024: new post (bf52b37)