Containerizing a WebMap .Net Application with Docker
By encapsulating your .NET Framework application within a Docker container, you gain advantages such as isolation, reproducibility, and scalability. This step-by-step guide will walk you through the process of creating a Docker image for your application, allowing it to run seamlessly on any system equipped with Docker.
Prerequisites
Install Docker: Ensure that Docker is installed on your machine. You can download Docker from the official website: Docker Engine.
Steps
Deploy the application: We just need the published application (the binaries or dlls of the project) therefore we would need to execute the following commands (open a terminal in root of the project):
dotnet restore && dotnet publish -c Release -o out
Skip this step if you already have the binaries. You have to see something like that:Dockerfile: Put the dockerfile in the root of the output or binaries folder.
# Use the official .NET SDK image as a build environment
# You can find the latest version of the SDK here: https://hub.docker.com/r/microsoft/dotnet-sdk
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
# Set the working directory inside the container
WORKDIR /app
# Set the ASPNETCORE_URLS variable
# You can set whatever port you want
# ENV ASPNETCORE_URLS=http://0.0.0.0:<PORT>
ENV ASPNETCORE_URLS=http://0.0.0.0:80
#Expose the same port that was assigned in the ASPNETCORE_URLS
EXPOSE 80
# Copy the binaries app folder in the root
COPY deploy .
# Set the entrypoint
ENTRYPOINT ["dotnet", "app/yourApp.dll"]
Build the docker image: Open a command prompt or terminal in the directory where your
Dockerfile
is located, and run the following command to build the Docker image:
docker build -t <your-image-name> .
Replace "your-image-name"
with a meaningful name for your Docker image.
Run the docker container: Once the image is built, you can run the container from it:
docker run -d -p <EXTERNAL_PORT>:<EXPOSED_PORT> --name <your-container-name> <your-image-name>
Test the containerized application: Visit
http://localhost:<EXTERNAL_PORT>
in your web browser to test your WebMap .NET Framework application running inside the Docker container. In case the localhost doesn't shows nothing, we have to find the ip container. Just execute the following command:
docker inspect '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <your-container-name>
Create a container with a .yaml file
Another way around to skip those commands and just execute one script is using a .yaml file.
Prerequisites
Install docker-compose
Steps
Create a .yaml file
version: '3.8'
services:
my_service:
container_name: <CHOOSE_CONTAINER_NAME>
build:
context: .
dockerfile: Dockerfile
ports:
- "<EXTERNAL_PORT>:<INTERNAL_PORT_EXPOSE>" # Example port mapping, adjust as needed
Execute the following command
docker compose up --build
Finally, you already created a WebMap application container without executing a lot of commands.
Last updated
Was this helpful?