着上一篇 AspNetCore容器化(Docker)部署(一) —— 入門,在單個容器helloworld的基礎上引入nginx反向代理服務器組成多容器應用。html
配置轉接頭。詳見:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2linux
Startup.csnginx
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
從新構建helloworld鏡像並生成容器git
PS C:\Users\Administrator> docker images REPOSITORY TAG IMAGE ID CREATED SIZE helloworld v1.1 a57e147d8487 About an hour ago 265MB mcr.microsoft.com/dotnet/core/sdk 2.2-stretch e4747ec2aaff 2 weeks ago 1.74GB mcr.microsoft.com/dotnet/core/aspnet 2.2-stretch-slim f6d51449c477 2 weeks ago 260MB docker4w/nsenter-dockerd latest 2f1c802f322f 7 months ago 187kB PS C:\Users\Administrator> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c8ec48808318 helloworld:v1.1 "dotnet HelloWorld.d…" About an hour ago Up 12 minutes 0.0.0.0:81->80/tcp netcore_helloworld
nginx官方鏡像和構建教程 https://hub.docker.com/_/nginxgithub
1.建立一個Nginx配置文件docker
helloworld.confwindows
server { listen 801; #監聽801端口 location / { proxy_pass http://netcore_helloworld:80; #默認80端口可不加端口號,這裏轉發沒有使用IP而是containerId } }
2.自定義Nginx鏡像的Dockerfile服務器
#定義基礎鏡像 nginx:latest FROM nginx #複製自定義的配置文件到Nginx配置文件目錄下 COPY helloworld.conf /etc/nginx/conf.d/helloworld.conf #暴露80端口到外部 EXPOSE 80 #容器運行參數 CMD ["nginx", "-g", "daemon off;"]
3.構建mynginx鏡像網絡
PS C:\Users\Administrator> cd C:\Users\Administrator\source\repos\AspNetCore_Docker\MyNginx PS C:\Users\Administrator\source\repos\AspNetCore_Docker\MyNginx> docker build -t mynginx:v1.0 . Sending build context to Docker daemon 3.072kB Step 1/4 : FROM nginx ---> 62c261073ecf Step 2/4 : COPY helloworld.conf /etc/nginx/conf.d/helloworld.conf ---> 0514a786be49 Step 3/4 : EXPOSE 80 ---> Running in 0693d4c225ef Removing intermediate container 0693d4c225ef ---> 7546051297e3 Step 4/4 : CMD ["nginx", "-g", "daemon off;"] ---> Running in 7f7634a0d689 Removing intermediate container 7f7634a0d689 ---> 4b260c730139 Successfully built 4b260c730139 Successfully tagged mynginx:v1.0 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS C:\Users\Administrator\source\repos\AspNetCore_Docker\MyNginx> docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynginx v1.0 f554133b97ee 9 seconds ago 109MB helloworld v1.1 a57e147d8487 About an hour ago 265MB nginx latest 62c261073ecf 5 days ago 109MB mcr.microsoft.com/dotnet/core/sdk 2.2-stretch e4747ec2aaff 2 weeks ago 1.74GB mcr.microsoft.com/dotnet/core/aspnet 2.2-stretch-slim f6d51449c477 2 weeks ago 260MB docker4w/nsenter-dockerd latest 2f1c802f322f 7 months ago 187kB
4.建立Nginx容器
app
容器內80和801端口分別映射到外部(宿主機)的80和801端口,-p 外部端口:內部端口
PS C:\Users\Administrator> docker run --name mynginx -d -p 801:801 -p 80:80 f554133b97ee 1b2751b1dba6bb4b97f8c78cda8646709ea80da03e5136025cf52dc3a3cc740d PS C:\Users\Administrator> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dae033fe1b5c f554133b97ee "nginx -g 'daemon of…" 18 seconds ago Up 17 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:801->801/tcp mynginx c8ec48808318 helloworld:v1.1 "dotnet HelloWorld.d…" 8 minutes ago Up 8 minutes 0.0.0.0:81->80/tcp netcore_helloworld
5.驗證Nginx
80端口訪問正常
801端口轉發失敗,應該說是沒法識別http://netcore_helloworld這個地址,由於容器之間是相互隔離的。
因此須要使用bridge networks建立一個橋接網絡將多個容器(網絡)鏈接起來進行通訊。
另外,使用link的方式進行ip映射也能夠達到容器間通訊的效果,跟windows裏面修改hosts文件ip映射hostname相似,須要注意的是link這個(廢棄)已過期的特性會在未來的版本中移除,不推薦使用。
官方介紹 https://docs.docker.com/network/
1.建立一個network
docker network create [OPTIONS] NETWORK
PS C:\Users\Administrator> docker network create mybridge 475a135ea2524de25f5594e35f2640e39c22365336092dc34f17e1019252eab1 PS C:\Users\Administrator> docker network ls NETWORK ID NAME DRIVER SCOPE d1110fd56b8c bridge bridge local 194bf7b8bfea mybridge bridge local c8ddf53079b8 host host local 522c9e7cfbcb none null local
2.將容器加入到network
docker network connect [OPTIONS] NETWORK CONTAINER
加入以後須要重啓容器,或者在建立容器時就指定docker run --network=bridgename ....
PS C:\Users\Administrator> docker network connect mybridge dae033fe1b5c PS C:\Users\Administrator> docker network connect mybridge c8ec48808318 PS C:\Users\Administrator> docker restart dae033fe1b5c c8ec48808318 dae033fe1b5c c8ec48808318
查看mybridge的information
PS C:\Users\Administrator> docker inspect 194bf7b8bfea [ { "Name": "mybridge", "Id": "194bf7b8bfeafdbfb8842c0d6252962c8cb1367c60d8742c5830c4446b571198", "Created": "2019-05-21T06:07:35.1683249Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "c8ec488083184ff419cb1a2b1b67fb92520db276263ee43e97c3f30aa58314d0": { "Name": "netcore_helloworld", "EndpointID": "a21a766a83887b1c5ce44b75affb2bce78433ad307b8a5fa0f991969492ace4c", "MacAddress": "02:42:ac:12:00:04", "IPv4Address": "172.18.0.4/16", "IPv6Address": "" }, "dae033fe1b5c79c2604df3facb4c714a56e49f3501c34bda125a786b28faef88": { "Name": "mynginx", "EndpointID": "4119532667d868d735c15f584e6e7e27d48308808ef8b58e4d85a8b3e626791a", "MacAddress": "02:42:ac:12:00:03", "IPv4Address": "172.18.0.3/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]
3.驗證
示例代碼Github地址:https://github.com/wwwu/AspNetCore_Docker