Containerize a .NET 8 Web API application with Docker and upload/download the image to/from Docker Hub
Install Docker Desktop
Create a new account in Docker Hub
Install Visual Studio 2022 Community edition
IMPORTANT! Install and Run Docker Desktop before starting creating the application in Visual Studio 2022
Run Visual Studio 2022 Community Edition and create a new project
Select the .NET Web API template
Set the project name and location
Select the .NET 8 framework, also select Enable Docker for creating a dockerfile automatically
Click on the dockerfile to see the content
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"]We can automatically create and run the application Docker Image pressing the Docker button in Visual Studio 2022
See the docker image in Docker Desktop
See the docker running container in Docker Desktop
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
IMPORTANT NOTE:
Another option for creating automatically a dockerfile is to add "docker support" in our application
Then we select the docker virtual machine operating system, in our case we select "Linux"
Login in Docker Hub and create a new account (Sign Up)
After creating an account we Sing in
Press the "Create Repository" button
We set the new repo name, we select public or private repo and we include a repo description.
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
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 .
We check we create the docker image and we run the docker container
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
See the new docker image in the repo
We pull the image with the command:
docker pull luiscoco/webapidotnet8
Then we open Docker Desktop and we confirm the docker image was downloaded but it is not yet running in a container
To run the docker image in a container, we press the run button (see the following image), or we type this command
Or we can type the following command, taking into account we map the launchSetting.json http port with the 8080 port in dockerfile
docker run -d -p 5269:8080 luiscoco/webapidotnet8:latest
And also we can see the docker logs, see this picture
Now in Docker Desktop we can see the docker container is running
We can also navigate to the Web API endpoints:































