久盛官方备用登录页面久盛官方备用登录页面


久盛官方认证平台

CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose

1.什么是Docker-Compose

上一章我们讲了通过Dockerfile创建镜像,这在一个小项目中是没问题的,但如果在一个包含多个项目的情况下,我们每次部署都需要执行多次创建镜像与运行容器的命令,这样就比较麻烦,为了解决这种情况,Docker-Compose出现了。Docker-Compose主要就是为了解决在一台服务器创建镜像与运行容器复杂的问题,有了Docker-Compose我们就可以通过一条命令,生成多镜像与运行容器。

2.Docker-Compose安装

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-compose

查看安装是否成功

3.创建两个.netcore项目,不启用https

ServerProvider项目

新增Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtimeMAINTAINER yishi.chenLABEL description="this is a serverprovider website"LABEL version="1.0"ARG serverportWORKDIR /appCOPY bin/Release/netcoreapp2.1/publish/ .EXPOSE $serverportENTRYPOINT ["dotnet","ServerProvider.dll"]

Program.cs改动

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls($"http://*:{Environment.GetEnvironmentVariable("serverport")}") .UseStartup<Startup>();

ValuesController.cs改动

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;namespace ServerProvider.Controllers{ [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "this is serverprovider"s result" }; } }}

ServerConsumer项目

新增Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtimeMAINTAINER yishi.chenLABEL description="this is a serverconsumer website"LABEL version="1.0"ARG consumerportWORKDIR /appCOPY bin/Release/netcoreapp2.1/publish/ .EXPOSE $consumerportENTRYPOINT ["dotnet","ServerComsumer.dll"]

Program.cs改动

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls($"http://*:{Environment.GetEnvironmentVariable("consumerport")}") .UseStartup<Startup>();

ValueController.cs改动

using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.Net.Http.Headers;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;namespace ServerComsumer.Controllers{ [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 [HttpGet("{id}")] public async Task<ActionResult<string>> Get(int id) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); return await httpClient.GetAsync(Environment.GetEnvironmentVariable("serverurl")).Result.Content.ReadAsStringAsync(); } }}

新建docker-compose.yml编排文件

内容如下(docker-compose的配置请见https://www.cnblogs.com/chenyishi/p/9965479.html):

version: "3"services: s_provider: build: context: ./ServerProvider/ dockerfile: Dockerfile args: serverport: 1000 ports: - "1000:1000" environment: serverport: 1000 container_name: c_provider s_consumer: build: context: ./ServerComsumer/ dockerfile: Dockerfile args: consumerport: 2000 ports: - "2000:2000" links: - s_provider:s_provider environment: consumerport: 2000 serverurl: http://s_provider:1000/api/values/ container_name: c_consumer

 4.定位到docker-compose文件所在目录,运行docker-compose

[root@cys-test-centos WebDocker]# docker-compose up

运行成功,状态如下:

5.另起一个命令窗口,测试接口

[root@cys-test-centos ~]# curl http://localhost:1000/api/values["this is serverprovider"s result"]

root@cys-test-centos ~]# curl http://localhost:2000/api/values["value1","value2"]

[root@cys-test-centos ~]# curl http://localhost:2000/api/values/1["this is serverprovider"s result"]

以上是通过docker-compose 新建镜像,并运行容器,如果镜像已存在,则可以去掉构建的过程,我们稍微对docker-compose文件做一下调整

6.在上面步骤的前提下,先CtrlC停止容器,然后docker-compsoe down删除容器

[root@cys-test-centos WebDocker]# docker-compose downRemoving c_consumer ... doneRemoving c_provider ... doneRemoving network webdocker_default

7.查看生成的镜像

[root@cys-test-centos WebDocker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEwebdocker_s_consumer latest cc95654856e1 17 minutes ago 253MBwebdocker_s_provider latest 5b744758b56b 17 minutes ago 253MBmicrosoft/dotnet 2.1-aspnetcore-runtime db366d73508b 4 days ago 253MB

镜像名为:webdocker_s_provider与webdocker_s_consumer

8.修改docker-compose.yml文件

version: "3"services: s_provider: image: webdocker_s_provider ports: - "1000:1000" environment: serverport: 1000 container_name: c_provider s_consumer: image: webdocker_s_consumer ports: - "2000:2000" links: - s_provider:s_provider environment: consumerport: 2000 serverurl: http://s_provider:1000/api/values/ container_name: c_consumer

9.验证接口

[root@cys-test-centos ~]# curl http://localhost:2000/api/values/1["this is serverprovider"s result"]

 

到此Docker-Compose介绍完毕,下一章讲Docker三剑客之Docker-machine

欢迎阅读本文章: 赵胜彪

久盛国际官方备用登录页面

久盛官方认证平台