使用docker-compose搭建AspNetCore开发环境
1 使用docker-compose搭建開發環境
我們的目標很簡單:使用docker-compose把若干個docker容器組合起來就成了。
首先使用Nginx代理所有的Web程序,這樣只需要在主機上監聽一個端口就可以了,不污染主機。再組合各Web程序、Redis/Memcached、SqlServerOnLinux。
新建一個目錄sites,所有和集群相關的都放在這里,目錄結構如下所示
新建docker-compose.yml
version: "2"
services:
? ? redis:
? ? ? ? build: ./redis/
? ? ? ? restart: always
? ? ? ? container_name: mac-redis?
? ? identity:
? ? ? ? build: ~/identity/src/Web/ ?#identity項目的Dockerfile所在的目錄
? ? ? ? restart: always
? ? ? ? volumes:
? ? ? ? ? ? - ~/identity/src/Web/bin/Debug/netcoreapp1.1/publish:/app #把編譯好的序集添加到數據卷中
? ? ? ? links:
? ? ? ? ? ? - 'ucenter:api.ucenter.com' #因為identity項目依賴ucenter項目,identity目內部通過url:api.ucenter.com調用ucenter的webapi,所以這里要給設置一個和ur一樣的別名,這樣identity項目訪問`api.ucenter.com`就會被轉發到ucenter容器。
? ? ? ? ? ? - redis
? ? ? ? extra_hosts:
? ? ? ? ? ? - "dbserver:192.168.199.143" #之前搭建了一個sqlserver容器,那個是單獨放在個Linux機器里面,所以這里就直接通過地址調用,注意要開啟遠程訪問。
? ? ? ? container_name: identity
? ? ? ? logging:
? ? ? ? ? ? driver: "json-file"
? ? ? ? ? ? options:
? ? ? ? ? ? ? ? max-size: "50k" #因為隨著程序的運行,日志會越來越多,導致每次加載時間越越長,所以規定日志文件大小,節省時間
? ? ? ? ? ? ? ? max-file: "10"
? ? ucenter:
? ? ? ? build: ~/UCenter/src/WebAPI #ucenter項目的Dockerfile所在的目錄
? ? ? ? restart: always
? ? ? ? volumes:
? ? ? ? ? ? - ~/UCenter/src/WebAPI/bin/Debug/netcoreapp1.1/publish:/app
? ? ? ? links:
? ? ? ? ? ? - redis
? ? ? ? extra_hosts:
? ? ? ? ? ? - "dbserver:192.168.199.143"
? ? ? ? container_name: ucenter
? ? nginx-host:
? ? ? ? build: ./nginx #nginx的Dockerfile所在的目錄
? ? ? ? restart: always
? ? ? ? ports:
? ? ? ? ? ? - "80:80" #監聽主機的80端口,或者其它的端口都可以
? ? ? ? links:
? ? ? ? ? ? - identity
? ? ? ? ? ? - ucenter
? ? ? ? volumes:
? ? ? ? ? ? - ./nginx/sites-enabled:/etc/nginx/sites-enabled #nginx的配置文件放在據卷中,以后需要改動的時候,重啟nginx就可以了,不用重新build
? ? ? ? ? ? - /WebCommon:/www/data #各個項目共用的靜態文件,一般走cdn的,在開發環境里就nginx代理
? ? ? ? container_name: nginx-host
? ? ? ? logging:
? ? ? ? ? ? driver: "json-file"
? ? ? ? ? ? options:
? ? ? ? ? ? ? ? max-size: "50k"
? ? ? ? ? ? ? ? max-file: "10"
上面在集群中配置了4個服務,一個nginx負責監聽主機的80端口,并轉發到相應的服務中去。
identity是我開發的項目,依賴于ucenter項目
項目中都依賴redis緩存,還有通過host解析的數據庫服務dbserver。
2 下面先搭建nginx服務
在sites->nginx目錄下面,新建sites-enabled目錄,再新建一個名為default的配置文件
server {
? ? listen 80;
? ? server_name account.xxx.com; #identity項目的域名
? ? location / {
? ? proxy_pass ? ? ? ? ?http://identity; #轉發到identity服務處理
? ? proxy_set_header ? ?Host $host;
? ? proxy_set_header ? ?X-Forwarded-For $proxy_add_x_forwarded_for;
? ? proxy_connect_timeout ? 150;
? ? proxy_send_timeout ?100;
? ? proxy_read_timeout ?100;
? ? proxy_buffers ? ? ? 4 32k;
? ? client_max_body_size ? ?8m;
? ? client_body_buffer_size 128;
? }
}
server {
? listen 80;
? server_name ucenter.xxx.com; #ucenter項目的域名
? location / {
? ? proxy_pass ? ? ? ? ?http://ucenter; #轉發到ucenter服務處理
? ? proxy_set_header ? ?Host $host;
? ? proxy_set_header ? ?X-Forwarded-For $proxy_add_x_forwarded_for;
? ? proxy_connect_timeout ? 150;
? ? proxy_send_timeout ?100;
? ? proxy_read_timeout ?100;
? ? proxy_buffers ? ? ? 4 32k;
? ? client_max_body_size ? ?8m;
? ? client_body_buffer_size 128;
? }
}
server {
? ? listen 80;
? ? server_name cdn.xxx.com; #這里把靜態文件打包成一個服務,替代cdn
? ? root /www/data;
? ? location / {
? ? }
}
這個是nginx的配置文件,主要配置Nginx的代理方式。下面編寫生成Nginx的Dockerfile,sites->nginx下面新建Dockerfile
FROM tutum/nginx VOLUME nginx/sites-enabled通過這個Dockerfile就可以創建Nginx了,
3 安裝redis
再sites下新建目錄:mkdir redis
vim Dockerfile
FROM redis COPY redis.conf localredis/redis.conf CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]vim redis.conf, 復制粘貼redis的配置,這是redis的官方默認配置。
docker build -t mac-redis .我給它起了個名字叫mac-redis
鏡像創建好之后,先測試一下能不能用,docker run -d --name mac-redis mac-redis。這時候mac-redis的容器已經在后臺跑起來了,我們再用redis-cli測試這個redis服務,docker run -it --link mac-redis:redis --rm redis redis-cli -h redis -p 6379使用--rm參數,用完就清除這個容器。測試如下:
redis:6379> set key1 value1OKredis:6379> get key1"value1"看來redis服務沒問題,exit退出。再把測試用的redis服務容器也刪除掉docker rm -f mac-redis。
4 使用docker跑aspnetcore程序
這個是最簡單的了,根據微軟的鏡像來就行了,園子里也有大量的教程。
在這里我把它們放在集群中,只要在docker-compose.yml中配置各個aspnetcore程序的Dockerfile所在的路徑即可,在我們的yml文件中有
build: ~src/Web/ ?identity的源代碼所在的目錄為~/identity/,Dockerfile在其中的src/web里面,如下:
FROM microsoft/aspnetcore:1.1.1LABEL Name=identity Version=0.0.1 ENV ASPNETCORE_ENVIRONMENT DevelopmentENTRYPOINT ["dotnet", "Identity.Web.dll"]如果有多個Dockerfile,可以在yml中指定Dockerfile的名稱。
另外一個ucenter的Dockerfile如下:
FROM microsoft/aspnetcore:1.1.1LABEL Name=ucenter Version=0.0.1 ENTRYPOINT ["dotnet", "UCenter.WebAPI.dll"]代碼修改了之后,dotnet publis && docker restart xxx(xxx就是容器的名稱)就生效了。
使用chrome瀏覽器訪問
最后進入sites目錄,docker-compose up啟動集群,由于nginx監聽的是主機的80端口,所以需要把綁定的url指向主機的80端口,一種方法是直接在hosts中加解析,但是不推薦這樣做,因為如果要訪問線上的項目的話,又要改hosts。為了不污染主機環境,推薦使用chrome瀏覽器,chrome有多用戶功能,因此新建一個用戶,使用SwitchyOmega插件,將所需要的URl代理到主機的80端口,這樣以后開發的時候用這個chrome的這個用戶就可以了,完全不影響主機環境。
在開發環節中,debug是必不可少的需求,所以下篇介紹如何用VSCode在Docker中debug。
原文地址:http://www.cnblogs.com/kexxxfeng/p/7125714.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的使用docker-compose搭建AspNetCore开发环境的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: DDD理论学习系列(8)-- 应用服务a
 - 下一篇: 拥抱.NET Core系列:依赖注入(1