Getting started using .Net Core Docker Docker Docker whole record first introductory lesson --.Net Core use Docker full record

https://www.cnblogs.com/flame7/p/9210986.html

The first introductory lesson --.Net Core Docker Docker use full record

 

Micro-service architecture is undoubtedly the most fiery of the development framework, while Docker as the preferred tool for micro-services architecture, we must understand the master.

I passed the time of day, check online documentation to learn basic concepts, installation Docker, test Docker command, by Docker, the successful deployment of run Asp.NET core sample program, considered a basic entry.

This article is a summary of Docker own introductory chapter, concise, quick start, the shortest time to see the learning outcomes, as a foundation for further study Docker.

 

Learning premise: Do not be afraid 

The face of the unknown, people tend to fear the heart, this man is born, so, before we enter new areas, we must first overcome their own psychological fear of difficulty. Do not be difficult, Kanji Yan to give up, so you never learn. 

Faced with new and uncharted territory, we take it as a beacon and let it guide our way forward.

 

First: understand concepts 

Image (Image) : believe we see the word, understand what it means, we can understand it as the operating system installation disk, Ghost image. I'll give it a definition, is called: Basic runtime copy of Docker. 

The container (Container) : Docker running instances, called a container. That is, run a mirror (Image) in the state. 

Mirror warehouse (Repository) : Docker provide for a variety of environments has been packaged mirror for developers, these images constitute a mirror warehouse. Developers just need to find their own Docker mirror, download to a local, add up your application, you can run certain tools of the mirror, without modification can be run directly.

Docker host (Host) : Docker container running computer or virtual machine for executing daemon Docker's. 

Docker client (Client): is a tool to communicate with the Docker host daemon, such as: Docker console. 

 

2: Installation Docker

Different operating systems in a manner different to Win7 system, for example, need to use docker toolbox to install, you can use a mirror to download Ali cloud.

Download: http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/

After the installation is complete, start to find three icons on the desktop: Oracle VM VirtualBox, Kitematic (Alpha), Docker Quickstart ....

After the installation is complete, restart the computer is recommended.

  

Oracle VM VirtualBox: is a virtual machine program. Docker is running in the Linux environment, in order to run under Windows, you must make use of virtual machines. Future research may keep interested.

kitematic (alpha): a docker launch a GUI tool that can more easily operate Docker, very suitable for windows user habits, I recommend trying it . This alpha represents a preview version of it, probably is not perfect.

Docker Quickstart Terminal: is Docker console start the program, double-click the icon to launch Docker.

If all goes well, you can see the following interface.

  

If the following error occurs during startup, Looks like something went wrong in step 'Looking for vboxmanage.exe' ...

Meaning not found a virtual machine to start the program, you need to set up VirtualBox environment variables, system environment variables increase VBOX_MSI_INSTALL_PATH and VBOX_INSTALL_PATH  values are C: \ Program Files \ Oracle \ VirtualBox \, namely the installation path VirtualBox, followed by attention \ can not be less . After the completion of environment variable settings, try rebooting.

 

  

Third: Familiar with several commands 

I suggest using the actual work graphical interface  kitematic , really simple and practical, but Docker command still have to learn, and most importantly, use the command line more professional operation seems cool.

docker command are beginning docker, Here are a few simple, commonly used commands. Only for basic introduction, detailed specific parameter usage can be late to learn.

docker pull: pull the warehouse from the mirror image.

docker run: create a new container through the mirror and run. Note that, if you do not specify a local mirror, the mirror will go directly to the download library, make sure it is spelled correctly. 

docker stop: stop a container.

docker start: start a container.

docker restart: restarting a container.

docker ps: list container state machine currently running.

docker images or docker image ls: list the local mirror.

docker build: Use Dockerfile create your own image.

  

Fourth: operation and demonstration, starting from hello world

You can log on the official mirrors warehouse Docker, choose a mirror that interest test, where we choose the most simple hello-world.

Docker official mirror Warehouse Address: https://hub.docker.com 

 

Pulling (downloads) image, the docker console interface, enter the command: docker pull-Hello World

镜像很小,下载很快完成,然后查看一下本地镜像,输入命令:docker images

不出问题的话,应该可以看到hello-world的镜像文件,运行镜像,输入命令:docker run hello-world

然后可以看到,控制台返回信息提示,Docker已经正常运行。 

第五:创建自己的Docker镜像

创建镜像需要使用Dockerfile文件,下面以asp.net core 为例,创建一个自己的镜像,示例中已经创建好了Dockerfile文件,直接使用即可,具体内容留着以后研究。

操作参考:为 .NET Core 应用程序生成 Docker 映像:https://docs.microsoft.com/zh-cn/dotnet/core/docker/building-net-docker-images 

首先:确保已经安装了Dotnet Core 2.0

然后:下载asp.net core 示例代码 :git clone https://github.com/dotnet/dotnet-docker-samples/

进入项目代码目录,运行示例,测试一下

cd aspnetapp

dotnet run

应用程序启动后,在 Web 浏览器中访问 http://localhost:5000。好了,一切正常,没问题。

创建镜像,输入Docker命令:

docker build -t aspnetapp .

生成镜像的过程需要下载依赖镜像包,大概需要十多分钟,执行过程中不要关闭控制台。 

镜像生成完成之后,运行一下试试:

docker run -it -p 5002:80 --name aspnetcore aspnetapp 

启动完成,打开浏览器:http://192.168.99.100:5002/

说明:-p 5002:80 表示将Docker主机的5002端口和Docker容器的80端口进行关联映射,即,外部访问使用5002端口,容器内部使用80端口,即 HTTP服务端口。

注意:这里是IP地址,至于这个IP是怎么来的,说实话,我也不知道,我尝试了几次,始终不能访问,然后打开kitematic,才发现只有这个IP才能访问。[哭脸]

 

 后记:

当看到程序正常启动运行时,是不是满满的成就感,学习就这么简单,最重要的是下定决心迈出行动的第一步。

本篇文章力求简洁,力图尽快看到学习成果,算是给大家深入学习提供的一块敲门砖,垫脚石。

另外文中的概念都是我自己理解的,可能跟标准答案不符,仅供理解和参考。

推荐几个深入学习的网址:

Docker官方镜像仓库:https://hub.docker.com 

Docker菜鸟教程:http://www.runoob.com/docker/docker-tutorial.html

Docker入门教程:http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html

为 .NET Core 应用程序生成 Docker 映像:https://docs.microsoft.com/zh-cn/dotnet/core/docker/building-net-docker-images 

微服务架构无疑是当前最火热的开发架构,而Docker作为微服务架构的首选工具,是我们必须要了解掌握的。

我通过一天的时间,网上查文档,了解基础概念,安装Docker,试验Docker命令,通过Docker,成功部署运行Asp.NET core示例程序,算是基本入门。

这篇文章是自己总结的Docker入门篇,力求简洁,快速入门,以最短的时间看到学习成果,为深入学习Docker做基础。

 

学习前提:不要畏惧 

面对未知,人们心里往往会产生恐惧,这是人与生俱来的,所以,我们在进入新的领域之前,首先要克服的是自己的畏难心理。不要因为困难,看几眼就放弃,那样你永远学不会。 

面对新的未知领域,我们要把它当成一座灯塔,让它指引我们前进的方向。

 

第一:了解几个概念 

镜像(Image):相信大家看到这个词,都明白什么意思,我们可以把它理解为操作系统的安装盘,Ghost镜像。我给它个定义,就叫:Docker基础运行环境副本。 

容器(Container):运行中的Docker实例,称为容器。也就是一个镜像(Image)的运行时状态。 

镜像仓库(Repository):Docker为开发者提供了面向各种环境的已经打包好的镜像,这些镜像构成了一个镜像仓库。开发者只需找到自己需要的Docker镜像,下载到本地,添加自己的应用上去,运行即可,某些工具类的镜像,可无需修改,直接运行。

Docker 主机(Host):运行着Docker容器的计算机或虚拟机,用于执行Docker的守护进程。 

Docker客户端(Client):是与Docker主机守护进程进行通信的工具,如:Docker控制台。 

 

第二:安装Docker

不同操作系统按照方式不同,以Win7系统为例,需要利用 docker toolbox 来安装,可以使用阿里云的镜像来下载。

下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/

安装完成之后,在桌面找到三个启动图标:Oracle VM VirtualBox,Kitematic(Alpha),Docker Quickstart …。

安装完成之后,建议重启一下计算机。

  

Oracle VM VirtualBox :是一个虚拟机程序。Docker是运行在Linux环境下的,要想在Windows下运行,必须借助虚拟机。感兴趣的话可以留着以后研究。

kitematic(alpha):是docker推出的GUI工具,可以更简便地操作Docker,非常适合windows用户使用习惯,推荐大家试试。这个alpha表示预览版吧,可能功能还不完善。

Docker Quickstart Terminal:是Docker控制台启动程序,双击图标启动Docker。

如果一切正常的话,可以看到以下界面。

  

如果启动过程中出现下面错误,Looks like something went wrong in step 'Looking for vboxmanage.exe'…

意思是没有找到虚拟机启动程序,需要设置VirtualBox的环境变量,系统环境变量中增加VBOX_MSI_INSTALL_PATHVBOX_INSTALL_PATH 值都为C:\Program Files\Oracle\VirtualBox\ ,即VirtualBox的安装路径,注意后面的\ 不能少。环境变量设置完成之后,可尝试重新启动。

 

  

第三:熟悉几个命令 

我建议实际工作中使用图形界面 kitematic,真的既简单又实用,但Docker命令还是要学习的,最重要的是,使用命令行操作显得更酷更专业。

docker 命令都是以docker开头,下面介绍几个简单,常用的命令。仅作基础介绍,具体参数用法可后期详细学习 。

docker pull:从镜像仓库中拉取镜像 。

docker run:通过镜像创建一个新的容器,并运行。需要注意的是,如果本地没有指定的镜像,会直接去镜像库下载,一定要保证拼写正确。 

docker stop:停止一个容器 。

docker start:启动一个容器 。

docker restart:重启一个容器 。

docker ps:列出当前运行的容器机器状态 。

docker images 或 docker image ls:列出本地镜像。

docker build:使用Dockerfile创建自己的镜像。

  

第四:操作演示,从hello world开始

你可以登录Docker官方镜像仓库,选择自己感兴趣的镜像进行试验,这里我们选择最简单的 hello-world。

Docker官方镜像仓库地址:https://hub.docker.com 

 

拉取(下载)镜像,在docker控制台界面,输入命令:docker pull hello-world

镜像很小,下载很快完成,然后查看一下本地镜像,输入命令:docker images

不出问题的话,应该可以看到hello-world的镜像文件,运行镜像,输入命令:docker run hello-world

然后可以看到,控制台返回信息提示,Docker已经正常运行。 

第五:创建自己的Docker镜像

创建镜像需要使用Dockerfile文件,下面以asp.net core 为例,创建一个自己的镜像,示例中已经创建好了Dockerfile文件,直接使用即可,具体内容留着以后研究。

操作参考:为 .NET Core 应用程序生成 Docker 映像:https://docs.microsoft.com/zh-cn/dotnet/core/docker/building-net-docker-images 

首先:确保已经安装了Dotnet Core 2.0

然后:下载asp.net core 示例代码 :git clone https://github.com/dotnet/dotnet-docker-samples/

进入项目代码目录,运行示例,测试一下

cd aspnetapp

dotnet run

应用程序启动后,在 Web 浏览器中访问 http://localhost:5000。好了,一切正常,没问题。

创建镜像,输入Docker命令:

docker build -t aspnetapp .

生成镜像的过程需要下载依赖镜像包,大概需要十多分钟,执行过程中不要关闭控制台。 

镜像生成完成之后,运行一下试试:

docker run -it -p 5002:80 --name aspnetcore aspnetapp 

启动完成,打开浏览器:http://192.168.99.100:5002/

说明:-p 5002:80 表示将Docker主机的5002端口和Docker容器的80端口进行关联映射,即,外部访问使用5002端口,容器内部使用80端口,即 HTTP服务端口。

注意:这里是IP地址,至于这个IP是怎么来的,说实话,我也不知道,我尝试了几次,始终不能访问,然后打开kitematic,才发现只有这个IP才能访问。[哭脸]

 

 后记:

当看到程序正常启动运行时,是不是满满的成就感,学习就这么简单,最重要的是下定决心迈出行动的第一步。

本篇文章力求简洁,力图尽快看到学习成果,算是给大家深入学习提供的一块敲门砖,垫脚石。

另外文中的概念都是我自己理解的,可能跟标准答案不符,仅供理解和参考。

推荐几个深入学习的网址:

Docker官方镜像仓库:https://hub.docker.com 

Docker菜鸟教程:http://www.runoob.com/docker/docker-tutorial.html

Docker入门教程:http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html

为 .NET Core 应用程序生成 Docker 映像:https://docs.microsoft.com/zh-cn/dotnet/core/docker/building-net-docker-images 

Guess you like

Origin www.cnblogs.com/frank0812/p/11627919.html