Docker中部署.Net Core MVC笔记

1. 安装Docker

#依赖包安装
yum install -y yum-utils device-mapper-persistent-data lvm2
#增加 docker repo
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#安装docker
yum install -y docker-ce
docker -v
#启动
service docker start
#开启自启
chkconfig docker on
#镜像加速
echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker
systemctl daemon-reload
#重启Docker
service docker restart

2. 发布应用配置Dockerfile

cd FirstCoreMvc
dotnet publish -o ~/firstmvc
ls
touch Dockerfile
vi Dockerfile
cp Dockerfile ~/firstmvc/Dockerfile

#添加基础镜像
FROM microsoft/dotnet:2.2-aspnetcore-runtime
#容器中系统的工作空间
WORKDIR /app
#拷贝当前文件夹下的文件到容器中系统工作空间
COPY . /app
#设置Docker容器对外暴露端口
EXPOSE 80
#容器中使用 ["dotnet","系统启动的dll"] 来运行应用程序
#使用ENTRYPOINT ["dotnet","系统启动的dll"]
#或使用 CMD ["dotnet","系统启动的dll"]
ENTRYPOINT ["dotnet","FirstCoreMvc.dll"]

3. 运行

cd firstmvc
docker build -t firstmvc:1.0.0 .  #最后有一个点
docker run --name=firstmvc -d -p 8188:80 -v ~/firstmvc:/app firstmvc:1.0.0
#如果报错(iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8       0 -j DNAT --to-destination 172.17.0.2:5000 ! -i docker0: iptables: No chain/targ       et/match by that name.
#则运行 iptables -F

4. 测试服务

#浏览器中打开 http://ip:8188/
curl http://localhost:8188

5. 查看启动日志 docker logs <容器名称或者id>

 docker logs firstmvc 

6. 推送到镜像仓库 先注册账号 https://hub.docker.com/

docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxxxx   #注册好的账号
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
docker images   #查看所有镜像
docker tag firstmvc:v1.0.0 xxxxxx/firstmvc:v1.0.0    #镜像重命名,命名规范/
docker push xxxxxx/firstmvc:v1.0.0    #推送镜像到仓库

#在其他机器执行docker run,从镜像仓库直接拉取镜像
docker run --name=firstmvc -d -p 8181:80 -v publish:/app xxxxxx/firstmvc:v1.0.0    

你可能感兴趣的:(Docker中部署.Net Core MVC笔记)