Before I started this blog, I've tried to practice many timesDocker Environmental installation,.Net Core Environment installed, Make a promotion for Tencent cloud here, If we want to learn, Practise hand.net
OrDocker But we have no development environment, Servers don't want to buy either, Then we can use the developer experiment provided by Tencent cloud, Each experimental project can be used once a day free of charge, often45 Minute, Learning technology by reading several articles, A few books are not enough,” Only practice can bring out true knowledge“.
Tencent cloud developer experiment connection address <https://cloud.tencent.com/developer/labs/gallery>
Two, Environment introduction
The environment on the practice platform isCentOS7.3 edition
Maybe you'll say I don't understandLinux How to do it? No problem, I'm also Xiaobai, Just learn some basic commands and use them for the time being. After all, we are not specialized inLinux Under development.(Linux I'm sorry).
Docker container, IfDocker If you don't know the concept of container, I recommend suspending first, Learn firstDocker Basic concepts of, Make clearDocker What's this, It's better to be confused.
dotnet image( It can also beaspnetcore I'll explain the relationship between the two later).
OK, Start working!
Three, Start-up experiment, installDocker
The building I enteredDocker Environmental Science, Of course, we can start from other experiments. From buildingDocker Enter the environment and we can see the required steps, Easy to install, That's it.
1, Installation firstDocker Environmental Science
yum install docker-io -y //CentOS Already willDocker The package is inExtras Software source, Direct use docker -v
// Seedocker Version version systemctl start docker.service // openDocker service systemctl
enable docker.service// Boot upDocker service
installDocker Wait one to two minutes for environment........ Installation completed!
Next we need toDocker Service start
Come hereDocker The installation of the environment is finished.
Of course, we need to configureDocker, Start configurationDockerHub：
echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >>
/etc/sysconfig/docker systemctl daemon-reload // Reload systemd, Scan for new or changed units
systemctl restart docker.service// restartdocker service
Four, install.NETCore Environmental Science
Next we will have two ways to install our.net core Environmental Science
One way： stayDocker Get in containerdotnet image
docker pull microsoft/dotnet // install.net core image
This way, it's offered to us.net core Complete formwork of, Compiler tool,RunTime Wait, Goods are available in all varieties.
We can go throughdocker Command to view the image we already have, Be careful This needs to beLinux View under the host, Don't run into the container
docker images // View existingdocker Mirror file
Under the current command, we can seedotnet The image has been pulled down by us.OK
Continue, We started creating and running adotnet Mirrored container
docker run -it microsoft/dotnet // Start adotnet image
noticeroot Has the back changed, That means we've entered a container,Docker When a new container is created, This container will be assigned aid, That's the string we saw.
Continue, Create a new one by commandweb application
dotnet new mvc -n MyFirstDocker.Web // Create a project namedMyFirstDocker.Web OfASP.NET Core
We can go throughls Command to see all folder information in the current container,MyFirstDocker.Web The project has been created
stayMyFirstDocker Inside folder, There are many things we are familiar with
Next, Start project with command
dotnet run // Startup project
OK, Be accomplished, Project initiation, No browser due to the experimental environment, We can only see Ha-ha There seems to be a way to see the page But I haven't learned Forgive me.
By orderexit Exit the current container, View existing container instances on the current host
docker ps -a // View the current existing container instance ( Regardless of status)
Here we start to think about the long-term plan, If I need to add new documents, What can I do, Install one in the containergit,pull new file, Maybe. But if there are ten containers on this host, All running services, I have to install all ten containersgit
, Based on this consideration, You can try this.
I am hereLinux Installed in the host.NET Core SDK
Start by configuring the source of the installation package
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo
sh -c'echo -e "[packages-microsoft-com-prod] name=packages-microsoft-com-prod
enabled=1 gpgcheck=1 gpgkey=https://
packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'
Next, start the installation.net-sdk, Edition2.0.3, This process takes a long time, Need to be patient
sudo yum update sudo yum -y install libunwind libicu sudo yum install
Okay, Let's move on
cd /home mkdir aspnetcoreapp cd aspnetcoreapp dotnet new web dotnet
restore dotnet run
By direct start, It can also run directly, But it's definitely not recommended, after allDocker It's so easy to use. Ha-ha
Next letLinux Files on the host can share files toDocker Container, So that we can directlyDocker Use inLinux Host file, And run it, When there is a document change, Just updateLinux Files on the host. There's a problem here, That is, Mingming has used the command of shared directory, howeverDocker There are no files in the container, This is due toselinux Reasons for, My solution is to
-v Plus one--privileged=true, Of course, this plan is also known by Du Niang.
currentDocker There are already files in the shared directory in the container, May needdotnet restore Then you can usedotnet run Start up
Mode two： stayDocker Get in containeraspnetcore image
docker pull microsoft/aspnetcore // installasp.net core image
View the existing image, Successfully installedaspnetcore image
Same as the previous operation, We create a new container based on the image
docker run -it microsoft/aspnetcore // Run a toaspnetcore Container for mirror
Also create a new app, But it will be found that this application cannot succeed, We didn'tSDK
dotnet new mvc -n HelloDocker.Web // Create a project namedHelloDocker.Web OfASP.NET Core MVC project
Here I want to explaindotnet Mirror image andaspnetcore What is the relationship between images:
staydockerhub Official website <https://hub.docker.com/r/microsoft/aspnetcore/>
Withdotnet andaspnetcore Mirror image,dotnet It can be regarded as an integrator, Contains.netcore Consoleaspnetcore All, andaspnetcore In order todotnet Mirror based, stayaspnetcore OnlyRunTime And tools to build the current project as a mirror, Which means that if we want to use it directlyaspnetcore Mirror image, Our project is to have been compiled, You can use this image directly.
And when you usedotnet Mirror image time, Includes compilation tools,Runtime, Everything, everything, This is also hormone.dotnet Image ratioaspnetcore The reason why the image is so large.
You can make use of mode one at the end ofLinux Compiled project, Also share the directory to theaspnetcore mirroredDocker Container, So we can run straight. No attempt due to time, I'll try it next time.
The advantage of this is that we can use the compiled project directly in the development environment,Docker Only the runtime environment is needed in the container.
Five,DockerFile Role of documents
dockerFile But good things, It's like holding a martial arts script, Do the same in one move.
Generally, build one in the projectdockerfile file, And then write our requirements in it
FROM microsoft/dotnet:latest WORKDIR /app
COPY . /app RUN dotnet restore EXPOSE 5000 ENV ASPNETCORE_URLS http://
*:5000 ENTRYPOINT ["dotnet","run"]
By commanddockerfile file run, Package the entire project as an image, We can usedocker images You can see the new image generated
docker build -t mydockerimage .
If we need to use this image, We can specify this image directly when creating the container, The generated environment is the same as the image you package
Thank you for your article reference, I saw this article when I was half puzzled, It solved a problem smoothly.
@ Saint http://www.cnblogs.com/sheng-jie/p/8107877.html
New year's Eve, I wish you all the best Auspicious New Year The most favorable auspices.
2018-1-1, I hope I can come back to see my own steps after the technology is completed