windows下的asp.net core开发及docker下的发布

时间:2022-02-13 07:32:49

参照下面,搭建好开发环境、Docker及配置好Docker加速器

http://www.cnblogs.com/windchen/p/6257846.html

参照下面,将windows共享目录挂载到Ubuntu虚拟机

http://www.cnblogs.com/windchen/p/6236299.html

假设:D:\projects --> /mnt/projects

参照下面,在Ubuntu上安装好docker compose

http://www.cnblogs.com/windchen/p/6239494.html

下载asp.net core运行镜像和构建发布镜像

docker pull microsoft/aspnetcore
docker pull microsoft/aspnetcore-build
docker pull nginx

建立asp.net core网站

打开windows命令行工具,建立目录

mkdir D:\projects\first-web\src
cd D:\projects\first-web\src
dotnet new -t web

修改网站Url

打开Program.cs文件,添加红色字部分

 public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}

利用docker compose建立build、publish环境

登入Ubuntu,进入Project目录,建立docker-compose.yml文件

cd /mnt/projects/first-web
mkdir -p docker/build
vi docker/build/docker-compose.yml

输入内容:

version: ""

services:
build-env:
build: ../../src
container_name: build-env

建立script:

cd /mnt/projects/first-web
mkdir scripts
vi scripts/build-publish.sh
chmod +x scripts/build-publish.sh

输入如下内容:

basepath=$(cd `dirname $`; pwd)
echo "goto build folder"
cd $basepath/../docker/build
echo "run build"
docker-compose build
echo "run down"
docker-compose down
echo "run up"
docker-compose up
echo "copy published files"
docker cp build-env:/publish/bin $basepath/../

建立生成build镜像的Dockerfile

cd /mnt/projects/first-web
vi src/Dockerfile

输入内容:

FROM microsoft/aspnetcore-build
WORKDIR /build COPY project.json .
RUN dotnet restore COPY . .
RUN dotnet publish -o /publish/bin/ -c Release

现在执行build-publish.sh就可以将code build并发布到Project的bin目录了

cd /mnt/projects/first-web
./scripts/build-publish.sh
ls bin/

利用docker compose建立proxy和web环境

建立生成web环境的Dockerfile

cd /mnt/projects/first-web
vi bin/Dockerfile

输入内容:

FROM microsoft/aspnetcore

WORKDIR /web

COPY . .

建立生成proxy镜像的Dockerfile

cd /mnt/projects/first-web
mkdir -p docker/nginx
vi docker/nginx/Dockerfile

输入如下内容:

FROM nginx

COPY my.nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 

在上面相同目录建立my.nginx.conf,输入

server {
listen ; location /{
proxy_pass http://web:5000;
}
}

建立启动container的compose文件

cd /mnt/projects/first-web
vi docker/docker-compose.yml

输入内容:

version: ""

services:
web:
build: ../bin
container_name: web
entrypoint: "dotnet build.dll"
proxy:
build: ./nginx
container_name: proxy
ports:
- :
links:
- web
tty: true

建立启动nginx和web的shell脚本

cd /mnt/projects/first-web
vi scripts/start-web.sh
chmod +x scripts/start-web.sh

输入如下内容:

basepath=$(cd `dirname $`; pwd)
echo "goto build folder"
cd $basepath/../docker/
echo "run build"
docker-compose build
echo "run down"
docker-compose down
echo "run up"
docker-compose up -d

至此准备工作已经完成,可以启动proxy跟web了

cd /mnt/projects/first-web
./start-web.sh

查看网站是否正常运行:

curl http://localhost