One , introduction

   Before I started this blog , Have tried to practice many times Docker Environment installation ,.Net Core Environment installed , Make a promotion for Tencent cloud here , If we want to learn , Practice .net
or Docker 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 , often 45 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 <>


Two , Environment introduction

   The environment on the practice platform is CentOS7.3 edition
Maybe you'll say I don't understand Linux What should I do? , No problem , I'm also Xiaobai , Just learn some basic commands and use them for the time being . After all, we are not specialized in Linux Under development .(Linux I'm sorry ).

  Docker container , if Docker If you don't know the concept of container , I recommend suspending first , Learn first Docker Basic concepts of , Find out Docker What is it? , It's better to be confused .

  dotnet image ( Or it could be aspnetcore I'll explain the relationship between the two later ).

  OK, Get started !


Three , Start the experiment , install Docker


   The building I entered Docker Environmental Science , Of course, we can start from other experiments . From building Docker Enter the environment and we can see the required steps , Easy to install , That's it . 

  1, Install first Docker Environmental Science
  yum install docker-io -y //CentOS Has Docker The package is in Extras In software source , Direct use   docker -v
// see docker Version of version   systemctl start docker.service // open Docker service   systemctl
enable docker.service// Start up Docker service


   install Docker Wait one to two minutes for environment ........ Installation completed !

   Next we need to Docker Service start


   Come here Docker The installation of the environment is finished .


    Of course, we need to configure Docker, Start configuration DockerHub:
  echo "OPTIONS='--registry-mirror='" >>
/etc/sysconfig/docker   systemctl daemon-reload // Reload systemd, Scan for new or changed units
  systemctl restart docker.service// restart docker service


Four , install .NETCore Environmental Science

   Next we will have two ways to install our .net core Environmental Science

   Mode 1 : stay Docker Get in container dotnet image

  docker pull microsoft/dotnet // install .net core image


    This way, it's offered to us .net core Complete formwork of , Compilation tools ,RunTime wait , goods are available in all varieties .

   We can go through docker Command to view the image we already have , be careful   It needs to be Linux View under the host , Don't run into the container
  docker images // View existing docker Image file


    Under the current command, we can see dotnet The image has been pulled down by us .OK

   continue , We started creating and running a dotnet Mirrored container
  docker run -it microsoft/dotnet // Start a dotnet image


   notice root Has the back changed , That means we've entered a container ,Docker When a new container is created , This container will be assigned a id, That's the string we saw .

    continue , Create a new one by command web application
  dotnet new mvc -n MyFirstDocker.Web // Create a project named MyFirstDocker.Web Of ASP.NET Core
MVC project

   We can go through ls Command to see all folder information in the current container ,MyFirstDocker.Web The project has been created


   stay MyFirstDocker In folder , There are many things we are familiar with


   next , Start project with command
  dotnet run // Launch project

   OK, be accomplished , Project launch , 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 Excuse me


    By command exit 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 container git,pull new file , Maybe , But if there are ten containers on this host , All running services , I have to install all ten containers git
, Based on this consideration , You can try this .

   I am here Linux Installed in the host .NET Core SDK

   Start by configuring the source of the installation package
  sudo rpm --import   sudo
sh -c'echo -e "[packages-microsoft-com-prod]   name=packages-microsoft-com-prod
  enabled=1   gpgcheck=1   gpgkey=https://" > /etc/yum.repos.d/dotnetdev.repo'

   Next, start the installation .net-sdk, edition 2.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 all Docker So easy to use   ha-ha


   Next let's Linux Files on the host can share files to Docker In container , So that we can directly Docker Used in Linux Host file , And run it , When there is a document change , Just update Linux Files on the host . There's a problem here , That is, Mingming has used the command of shared directory , however Docker There are no files in the container , This is because selinux Why , My solution is to
-v Add one --privileged=true, Of course, this plan is also known by Du Niang .

   current Docker There are already files in the shared directory in the container , May need dotnet restore Then you can use dotnet run Start up



   Mode 2 : stay Docker Get in container aspnetcore image
  docker pull microsoft/aspnetcore // install core image


   View the existing image , Successfully installed aspnetcore image


   Same as the previous operation , We create a new container based on the image
  docker run -it microsoft/aspnetcore // Run a to aspnetcore Container for mirror  
   Also create a new app , But it will be found that this application cannot succeed , We didn't SDK
  dotnet new mvc -n HelloDocker.Web // Create a project named HelloDocker.Web Of ASP.NET Core MVC project


   Here I want to explain dotnet Mirror and aspnetcore What is the relationship between images :

    stay dockerhub In the official website <>
With dotnet and aspnetcore Image of ,dotnet It can be regarded as an integrator , Contains .netcore Console aspnetcore All of , and aspnetcore So dotnet Mirror based , stay aspnetcore Only RunTime And tools to build the current project as a mirror , Which means that if we want to use it directly aspnetcore Is a mirror , Our project is to have been compiled , You can use this image directly .

   And when you use dotnet When mirroring , Includes compilation tools ,Runtime, Everything, everything , It's also hormone dotnet Mirror ratio aspnetcore The reason why the image is so large .


You can make use of mode one at the end of Linux Compiled project , Also share the directory to the aspnetcore mirrored Docker In 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 project dockerfile 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 command dockerfile file run , Package the entire project as an image , We can use docker 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 .

  @ Sage


   New year's Day , 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