1、安装docker,就不在介绍了,网上随意都是,参考:http://www.runoob.com/docker/centos-docker-install.html。但是需要注意的是,安装完成后,需要配置镜像加速地址,用阿里的地址也好,网易的也罢,其他的也行。自己做笔记,就说下阿里云的加速地址怎么搞,进入https://dev.aliyun.com,登录。
这个地址就是你的阿里加速地址
网易加速地址:http://hub-mirror.c.163.com
搞完后自己测试下,测试方法网上也都有。
2、下载dotnet镜像,镜像可以在docker hub上查看相关介绍,上面也介绍有怎么使用,这个东西比较大,得等一会。
docker pull microsoft/dotnet
3、我的docker是在linux上面安装的,所以我把发布好的网站放在linux下,然后在项目的跟目录添加一个Dockerfile文件:
#基于microsoft/dotnet来构建我们的镜像,后面可跟版本号,不跟默认最新
FROM microsoft/dotnet
#拷贝当前路径下的文件到镜像文件中的/dockertest文件夹中
COPY . /dockertest
#设置工作目录为 dockertest文件夹,即容器启动默认的文件夹
WORKDIR /dockertes
#设置Docker容器对外暴露6000端口(这里应该还是容器中的端口,不是本地端口,未实验,不能确定)
EXPOSE 6000
#使用`dotnet SampleProject.dll`来运行应用程序
CMD ["dotnet", "SampleProject.dll"]
4、然后将项目生成镜像,注意后面还有一个点
docker build -t dockertest .
生成完成后可以运行docker images命令来查看镜像是否生成
5、然后运行镜像,运行成功会出现一串字符串,运行失败会出现错误原因,出现错误可以进入docke内部执行命令查看具体什么原因,后解。60000端口是本地端口,6000是docker的端口
docker run -d -p 60000:6000 dockertest
6、运行成功,没问题,然后执行命令curl http://localhost:60000即可看到你网站输出的内容,表示成功。
踩坑:
An assembly specified in the application dependencies manifest (xxxxx.deps.json) was not found:
package: 'Microsoft.ApplicationInsights.AspNetCore', version: '2.1.1'
path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.0-linux-x64.xml;aspnetcore-store-2.0.0-osx-x64.xml;aspnetcore-store-2.0.0-win7-x64.xml;aspnetcore-store-2.0.0-win7-x86.xml
.net coer部署docker时候一直报错,这个错误原因是因为系统缺少东西,2种解决方案,一种是在操作系统中安装对应的包,另一种是程序发布时候将这个包一起打包在发布包中,不依赖系统,需要配置下解决方案。
解决方案一(没试过):apt-get install aspnetcore-store-2.0.0
解决方案二(使用的):右键你的项目->编辑xxx.csproj->在PropertyGroup标签中添加:
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
添加完成后的样子
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
然后重新发布网站,你会发现会多非常多的文件和文件夹,添加这个配置意思是打包所有相关依赖项。默认是不配置的,因为一般系统都安装有默认的程序包,但docker比较纯净所以没有这些。
docker相当于一个简易的linux操作系统,所以相关操作需要映射
进入容器:
docker run -itd 镜像 /bin/bash
docker attach 容器
以上两条命令进入容器有弊端,说是一个窗口阻塞会影响到其他窗口,所以推荐另一种方式
docker exec -it 容器id /bin/sh
退出容器操作:exit即可断开连接。
查看所有容器:docker ps -a
查看正在运行容器:docker ps
停止容器:docker stop 容器id
删除容器:docker rm 容器id
查看所有镜像:docker images
删除镜像:docker rmi 镜像名|镜像id
docker重启:systemctl restart docker
语法:docker run -p 本地端口:docker端口 镜像
CentOS卸载docker:
首先查看下docker:yum list installed | grep docker
然后将容器服务停止:service docker stop
然后执行命令卸载:
删除安装包
yum remove docker.x86_64
yum remove docker-client.x86_64
yum remove docker-common.x86_64
删除docker镜像
rm -rf /var/lib/docker
Dockerfile在build过程中会依次执行文件中的内容,实际上, docker 做的事, 也就是从基础镜像启一个容器, 然后执行一条命令, 修改之后提交此容器为新镜像. 以此类推, 直到所有命令都执行完. 所以在得到最终构建的镜像时, 会生成很多"中间镜像". 而如果 Dockerfile 中某条命令有错, 也是在当前中止, 过程中的"中间镜像"及"当前构建用的容器"仍然存在的.
参考资料:
http://www.cnblogs.com/stulzq/p/8628019.html
https://www.cnblogs.com/Leo_wl/p/5666669.html
http://www.bamn.cn/course/volume/4943
https://www.cnblogs.com/mahidol/p/9064943.html
转载于:https://my.oschina.net/uwith/blog/1839657