Docker - Easy Way
Rajeev Singh
.NET Architect | Delivering Cutting-Edge Software Architecture | Cloud, Microservices, and Enterprise Solutions
Definition
Docker is a powerful tool that provides a platform to package solutions for deployment. We can think like a box containing your OS/Code/Dependencies and the application runs using that box's resources only.
It is similar to VM except VM has its own guest OS but docker only works on host OS.
Install Docker For Windows
Refer below link to install Docker Desktop on Windows. You can use any operating system, but this example is for Windows however almost all mentioned steps are similar across all operating systems
Create a simple HTML and run it within docker environment
FROM nginx
COPY sample-docker /usr/share/nginx/html
docker build -t sample-docker .
docker run -–name <your-custom-name> -p 9800:80 sample-docker:<tag>
You can use -p switch to bind a port to a container. Each container runs under a docker host and obtains a unique random IP address. For ex. if a container assign a IP address of 172.168.1.1 and runs a .net application then you can access your application using https://172.168.1.1:80 when your browser runs inside the docker host. But your browser or any client that runs outside the host needs a proper port mapping to access the docker host container.
Few Essential Docker Commands
领英推荐
Dockerfile with .Net Application
However, when you create a new .Net Core application using Visual Studio IDE, In the template you will see an option to add a docker capability which adds automatically a docker file with predefined code to spin a container from an image. But you can use your own docker file for your .net application later.
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ./sample-docker.csproj ./
RUN dotnet restore "sample-docker.csproj"
COPY . .
RUN dotnet publish ./sample-docker.csproj -c Release -o /app/
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet","sample-docker.dll"]
In the above command, file dotnet publish is most important to build your project and publish. You can simply build first and then publish but dotnet publish automatically build, so you can directly use dotnet publish to do both operation together. Below is the explanation for this command
here -c stands for configuration and -o stands for output. You can simply get the help of dotnet publish by input dotnet publish -h command in your terminal window.
dotnet publish [<PROJECT>|<SOLUTION>] [-a|--arch <ARCHITECTURE>]
[-c|--configuration <CONFIGURATION>] [--disable-build-servers]
[-f|--framework <FRAMEWORK>] [--force] [--interactive]
[--manifest <PATH_TO_MANIFEST_FILE>] [--no-build] [--no-dependencies]
[--no-restore] [--nologo] [-o|--output <OUTPUT_DIRECTORY>]
[--os <OS>] [-r|--runtime <RUNTIME_IDENTIFIER>]
[--sc|--self-contained [true|false]] [--no-self-contained]
[-s|--source <SOURCE>] [--tl:[auto|on|off]]
[--use-current-runtime, --ucr [true|false]]
[-v|--verbosity <LEVEL>] [--version-suffix <VERSION_SUFFIX>]
Just to check if the publish command is working, you can copy this command and run it on your terminal, you will notice an app directory created with supporting output (dll etc.), perhaps in your C:, and then go to the app directory and run dotnet <your dll file name>. In the output, copy the URL and paste it into your browser to see the output and the same command you can see above dockerfile ENTRYPOINT section.
Now you want to run your application using docker, which you can do by running docker build -t sample-docker:1.0.0 .. You can simply check your image by firing docker images. Now you can use docker run -p 9000:5000 -e DOTNET_URLS=https://+:5000 --name=sample-docker sample-docker:1.0.0 here we have set the environment variable with -e switch called DOTNET_URLS because of dot net application, name=sample-docker is container name and sample-docker:1.0.0 is image name with it's tag information.
Push Images to Hub
You can use docker push <hub repository name>/<image name>:<tag name> but make sure you are logged in terminal. To logic, you can use docker login. You can observe in your docker hub portal that one image got created. This image you can pull to create container by using docker pull <name of hub image>:<tag> and then build container by using docker run -p 9001:5000 -e DOTNET_URLS=https://+:5000 <image name:<tag> (use dotnet_urls when you are run .net image)
Deploy your image to Azure Container Instance
Deploy to Azure Container App
You will get an Image source option which you can use the Docker hub and put your image name. Additionally, you need to select Ingress1. tag's option.
Footnotes
Technical Architect at Smart Cloud
7 个月Good article.