1.什麼是Docker-Compose 上一章我們講了通過Dockerfile創建鏡像,這在一個小項目中是沒問題的,但如果在一個包含多個項目的情況下,我們每次部署都需要執行多次創建鏡像與運行容器的命令,這樣就比較麻煩,為瞭解決這種情況,Docker-Compose出現了。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-compose sudo chmod +x /usr/local/bin/docker-compose
查看安裝是否成功
3.創建兩個.netcore項目,不啟用https
ServerProvider項目
新增Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime MAINTAINER yishi.chen LABEL description="this is a serverprovider website" LABEL version="1.0" ARG serverport WORKDIR /app COPY bin/Release/netcoreapp2.1/publish/ . EXPOSE $serverport ENTRYPOINT ["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-runtime MAINTAINER yishi.chen LABEL description="this is a serverconsumer website" LABEL version="1.0" ARG consumerport WORKDIR /app COPY bin/Release/netcoreapp2.1/publish/ . EXPOSE $consumerport ENTRYPOINT ["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 down Removing c_consumer ... done Removing c_provider ... done Removing network webdocker_default
7.查看生成的鏡像
[root@cys-test-centos WebDocker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE webdocker_s_consumer latest cc95654856e1 17 minutes ago 253MB webdocker_s_provider latest 5b744758b56b 17 minutes ago 253MB microsoft/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"]