1 What is? Docker？
Docker Is a for development , An open platform for publishing and running applications .Docker Enables you to separate your applications from your infrastructure , So you can quickly deliver software . With the help of Docker, You can manage your infrastructure in the same way you manage your applications . By using Docker The way to quickly deliver , Test and deploy code , You can significantly reduce the delay between writing code and running it in a production environment .
2 Why Docker？
2.1 Solve the problem of environment configuration
We usually write programs , There will be a development environment , Test environment , Production environment , quite a lot bug It's all about the environment .
The idea of deployment in the past , Packaging in the development environment , Want to run to Window Operation in environment . We have to be here first Window Download it .NET Framework,IIS etc. , Configure the corresponding environment variables , Drop the bag in iis Of webapps Under the folder , To run .
Now? Docker The idea of , We can build our desired environment into a mirror image , And then we can push it online . When you want to use this environment , Just pull one online .
2.2 Resolve isolation between applications
You must have encountered multiple applications deployed on the same server , There's a problem with one app , Lead to CPU Occupy 100%, Other applications are also affected . There are also some applications, for example NET technology , Some applications use Php technology , All the dependent software of these different applications are installed on the same server , There could be all kinds of conflicts / Not compatible .
2.3 Docker And virtual machines
For virtual machines , The host server has three basic layers from bottom to top ： Infrastructure , Host operating system and hypervisor , The most important thing is that each virtual machine has its own operating system and all the necessary Libraries . about Docker, The host server has only the infrastructure and operating system , The most important thing is the container engine , The container engine keeps the container isolated , But sharing basic OS service .
Because containers require much less resources （ for example , They don't need a full operating system ）, So they're easy to deploy and start up quickly . This will increase the density , This means that more services can run on the same hardware unit , So as to reduce the cost .
As a side effect of running on the same kernel , And VM comparison , You get less isolation .
3 How to use Docker？
First , install Docker , Refer to official documentation .
then , establish Docker.WebApi01 project
Make sure you have selected “ Enable Docker Support ” Check box , Select the type of container you want （Windows or Linux）.
next , We turn on Dockerfile file , see also Dockerfile quote , Understand the command ：
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build WORKDIR /src COPY ["Docker.WebApi01/Docker.WebApi01.csproj", "Docker.WebApi01/"] RUN dotnet restore "Docker.WebApi01/Docker.WebApi01.csproj" COPY . . WORKDIR "/src/Docker.WebApi01" RUN dotnet build "Docker.WebApi01.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "Docker.WebApi01.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Docker.WebApi01.dll"]
The meaning of the above code ：
- FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base： Specify the underlying image file mcr.microsoft.com/dotnet/aspnet, Marked as 5.0-buster-slim, An alias for base.
- WORKDIR /app： Set the working directory to /app
- EXPOSE 80： The container 80 Port exposed , Allow external connections to this port .
- COPY ["Docker.WebApi01/Docker.WebApi01.csproj", "Docker.WebApi01/"]： take Docker.WebApi01/Docker.WebApi01.csproj Copied to the Docker.WebApi01/ Under the table of contents .
- RUN dotnet restore "Docker.WebApi01/Docker.WebApi01.csproj"： recovery Docker.WebApi01.csproj Project dependencies and tools .
- RUN dotnet build "Docker.WebApi01.csproj" -c Release -o /app/build： Generate Docker.WebApi01.csproj The project and all its dependencies .
- RUN dotnet publish "Docker.WebApi01.csproj" -c Release -o /app/publish： Publish the application and its dependencies to a folder for deployment to a managed system .
next , debugging
Select... From the debug drop-down list on the toolbar “Docker”, Then start debugging the application . You may see a message prompting you to trust the certificate ; Select the trust certificate to continue .
“ Output ” Window “ Container tools ” Option shows the operation in progress . The first time , It may take some time to download the base image , But it's much faster on subsequent runs .
Last , Build and run
docker build -t dockerwebapi01 -f ./Docker.WebApi01/Dockerfile . docker run --rm -it -p 49181:443 dockerwebapi01
Notice that there's a pit right here Dockerfile Directory execution will report the following error ：
=> ERROR [build 3/7] COPY [Docker.WebApi01/Docker.WebApi01.csproj, Docker.WebApi01/]
There are two solutions to this problem , One is to put Dockerfile Files in sln In the same directory , And then execute docker build, The other is the method used in this paper , stay sln Execute under directory , You need to specify the Dockerfile route
We type... In the browser ： https://localhost:49181/WeatherForecast Look at the effect