Skip to content

About

Docker: How to create and run a docker image from a . NET 8 Web API application

Resources

Stars

3 stars

Watchers

2 watching

Forks

Latest commit

 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

Containerize a .NET 8 Web API application with Docker and upload/download the image to/from Docker Hub

0. Prerequisites

Install Docker Desktop

Create a new account in Docker Hub

Install Visual Studio 2022 Community edition

1. Create a .NET 8 Web API in Visual Studio 2022 community

IMPORTANT! Install and Run Docker Desktop before starting creating the application in Visual Studio 2022

image

Run Visual Studio 2022 Community Edition and create a new project

image

Select the .NET Web API template

image

Set the project name and location

image

Select the .NET 8 framework, also select Enable Docker for creating a dockerfile automatically

image

Click on the dockerfile to see the content

image

This is the dockefile source code

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["WebAPIdotNET8.csproj", "."]
RUN dotnet restore "./././WebAPIdotNET8.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./WebAPIdotNET8.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./WebAPIdotNET8.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAPIdotNET8.dll"]

2. Create and run the Docker image

We can automatically create and run the application Docker Image pressing the Docker button in Visual Studio 2022

image

See the docker image in Docker Desktop

image

See the docker running container in Docker Desktop

image

Also we can see the image with the command prompt command:

docker image

And we can see the running containers with the command:

docker ps

Finally see the Web API running container endpoints

image

image

IMPORTANT NOTE:

Another option for creating automatically a dockerfile is to add "docker support" in our application

image

Then we select the docker virtual machine operating system, in our case we select "Linux"

image

3. Upload/download the Docker image to/from the Docker Hub

Login in Docker Hub and create a new account (Sign Up)

image

After creating an account we Sing in

image

Press the "Create Repository" button

image

We set the new repo name, we select public or private repo and we include a repo description.

image

We can Push images to this repo with the following commands:

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname

We can see the repositories list

image

4. Create the docker image and upload to the new repo in Docker Hub

This is the general syntax for creating a new image that we would like to upload to the Docker Hub repo.

Pay attention we have to include the docker hub repo name then the docker image name and finally the docker image tag.

docker build -t dockerreponame/dockerimagename:tag .

For a real example, we right click on the project name and we select the menu option "Open in Terminal" then we run the following command:

docker build -t luiscoco/webapidotnet8:latest .

image

image

We check we create the docker image and we run the docker container

image

Then we use the docker push command to upload the image to the Docker Hub repository:

docker push luiscoco/webapidotnet8:latest

Also we can pull the docker image from the Docker Hub repository with the following command:

docker pull luiscoco/webapidotnet8

image

See the new docker image in the repo

image

image

image

5. We pull the docker image from Docker hub and we run in Docker Desktop

We pull the image with the command:

docker pull luiscoco/webapidotnet8

image

Then we open Docker Desktop and we confirm the docker image was downloaded but it is not yet running in a container

image

To run the docker image in a container, we press the run button (see the following image), or we type this command

image

Or we can type the following command, taking into account we map the launchSetting.json http port with the 8080 port in dockerfile

image

docker run -d -p 5269:8080 luiscoco/webapidotnet8:latest

And also we can see the docker logs, see this picture

image

Now in Docker Desktop we can see the docker container is running

image

We can also navigate to the Web API endpoints:

http://localhost:5269/weatherforecast

image

About

Docker: How to create and run a docker image from a . NET 8 Web API application

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors