欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-02 22:26:21
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

查看安装是否成功

CentOS下Docker与.netcore(三)之 三剑客之一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编排文件

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

内容如下(docker-compose的配置请见):

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

运行成功,状态如下:

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

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"]

 

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