用Docker compose部署服务, 示例

用Docker compose部署服务

  • docker compose可以方便我们快捷高效地管理容器的启动、停止、重启等操作,它类似于linux下的shell脚本,基于yaml语法,在该文件里我们可以描述应用的架构,比如用什么镜像、数据卷、网络模式、监听端口等信息。我们可以在一个compose文件中定义一个多容器的应用(比如jumpserver),然后通过该compose来启动这个应用。
  • 安装compose方法如下
    curl -L https://github.com/docker/compose/releases/download/1.17.0-rc1/docker-compose-uname -s-uname -m > /usr/local/bin/docker-compose
    chmod 755 !$
  • docker-compose version 查看版本信息
    Compose区分Version 1和Version 2(Compose 1.6.0+,Docker Engine 1.10.0+)。Version 2支持更多的指令。Version 1没有声明版本默认是"version 1"。Version 1将来会被弃用。

用Docker compose部署服务 – 示例

  • vim docker-compose.yml //内容到https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/25docker/docker-compose.yml 查看
version: "2"
services:
  app1:
    image: centos_nginx
    ports:
      - "8080:80"
    networks:
      - "net1"
    volumes:
      - /data/:/data
  app2:
    image: centos_with_nettool
    networks:
      - "net2"
    volumes:
      - /data/:/data1
    entrypoint: tail -f /etc/passwd
networks:
  net1:
    driver: bridge
  net2:
    driver: bridge
  • docker-compose up -d 可以启动两个容器
    docker-compose --help
    docker-compose ps/down/stop/start/rm
  • 关于docker-compose语法的参考文档 http://www.web3.xin/index/article/182.html
  • 使用记录
<root@linux1 ~>$ wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/25docker/docker-compose-Linux-x86_64   #这个下载链接快一点;
--2020-01-09 14:52:43--  https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/25docker/docker-compose-Linux-x86_64
正在解析主机 coding.net (coding.net)... 115.159.21.113
正在连接 coding.net (coding.net)|115.159.21.113|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:8856744 (8.4M) [application/octet-stream]
正在保存至: “docker-compose-Linux-x86_64”

100%[==============================================================>] 8,856,744   1.81MB/s 用时 5.9s   

2020-01-09 14:52:49 (1.43 MB/s) - 已保存 “docker-compose-Linux-x86_64” [8856744/8856744])
<root@linux1 ~>$ mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose  #文件可直接使用,移动到/bin下;
mv:是否覆盖"/usr/local/bin/docker-compose"? y
<root@linux1 ~>$ chmod 755 !$  #改成可执行权限;
chmod 755 /usr/local/bin/docker-compose
<root@linux1 ~>$ docker-compose version
docker-compose version 1.17.0-rc1, build a0f95af
docker-py version: 2.5.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
<root@linux1 ~>$ vim docker-compose.yml   #编缉简单的yaml文件;
<root@linux1 ~>$ cat docker-compose.yml 
version: "2"
services:
  app1:
    image: centos_nginx
    ports:
      - "8088:80"
    networks:
      - "net1"
    volumes:
      - /data/:/data
  app2:
    image: centos
    networks:
      - "net2"
    volumes:
      - /data/:/data1
    entrypoint: tail -f /etc/passwd
networks:
  net1:
    driver: bridge
  net2:
    driver: bridge
<root@linux1 ~>$ docker-compose up -d   #创建和启动;
Creating network "root_net2" with driver "bridge"
Creating network "root_net1" with driver "bridge"
Creating root_app2_1 ... 
Creating root_app1_1 ... 
Creating root_app2_1
Creating root_app1_1 ... done
<root@linux1 ~>$ docker-compose ps  #查看compose情况;
   Name                  Command               State          Ports        
---------------------------------------------------------------------------
root_app1_1   /bin/sh -c nginx && tail - ...   Up      0.0.0.0:8088->80/tcp
root_app2_1   tail -f /etc/passwd              Up                          
<root@linux1 ~>$ docker ps -a   #查看容器情况;
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                  NAMES
453dbd62b32b        centos_nginx        "/bin/sh -c 'nginx &…"   29 seconds ago      Up 24 seconds            0.0.0.0:8088->80/tcp   root_app1_1
fdd6a8c739b5        centos              "tail -f /etc/passwd"    29 seconds ago      Up 24 seconds                                   root_app2_1
4cc841db61a8        registry            "/entrypoint.sh /etc…"   3 hours ago         Exited (2) 2 hours ago                          nifty_cerf
<root@linux1 ~>$ curl 127.0.0.1:8088 -I   #测试nginx服务;
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 09 Jan 2020 07:01:43 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 15:04:31 GMT
Connection: keep-alive
ETag: "5d52d17f-264"
Accept-Ranges: bytes

<root@linux1 ~>$ docker exec -it 453dbd bash   #测试-v目录同步情况;
[root@453dbd62b32b ansible]# ls /data/
backup  backup2  err.log  gitroot  logs  mysql  mysql.bak  redis_data  syny  wwwroot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值