Docker学习(三) Docker Compose

简介

Docker Compose是一个管理Docker集群的工具。

它通过YAML文件定义了

    · Docker容器间的依赖

    · 设置容器的环境变量

    · 数据持久化策略(如《Docker学习(一) 镜像与容器》4、在容器内安装RoR环境(具体做法省略)里提到的)

安装

$ sudo apt-get install python-pip

$ sudo pip install docker-compose

试用

写配置文件,来个最简单的例子,文件名:docker-compose-test.yml

version: '2'
services:
  thinking:
    image: ubuntu

运行

$ docker-compose -f docker-compose-test.yml up
Creating network "desktop_default" with the default driver
Creating desktop_thinking_1 ... done
Attaching to desktop_thinking_1
desktop_thinking_1 exited with code 0

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
29e8c2278ffd        ubuntu              "/bin/bash"         19 seconds ago      Exited (0) 18 seconds ago                       desktop_thinking_1

怪事年年有,改脚本为:

扫描二维码关注公众号,回复: 1570288 查看本文章
version: '2'
services:
  thinking:
    image: ubuntu
    tty: true

删掉前面启动的废物

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
29e8c2278ffd        ubuntu              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       desktop_thinking_1
$ docker rm 29e8c2278ffd
29e8c2278ffd
$ docker network list
NETWORK ID          NAME                DRIVER              SCOPE
421718219c6c        bridge              bridge              local
407c898f052e        desktop_default     bridge              local
37b352ae0dbf        host                host                local
9625fcfe68fd        none                null                local
$ docker network rm 407c898f052e
407c898f052e

重试

$ docker-compose -f docker-compose-test.yml up
Creating network "desktop_default" with the default driver
Creating desktop_thinking_1 ... done
Attaching to desktop_thinking_1

thinking_1  | root@6683fecc8ba5:/#

换种方式

$ docker-compose -f docker-compose-test.yml up -d
Creating network "desktop_default" with the default driver

Creating desktop_thinking_1 ... done

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f6eb5ff5d3af        ubuntu              "/bin/bash"         51 seconds ago      Up 49 seconds                           desktop_thinking_1
$ docker network list
NETWORK ID          NAME                DRIVER              SCOPE
421718219c6c        bridge              bridge              local
85d6103b3e6a        desktop_default     bridge              local
37b352ae0dbf        host                host                local
9625fcfe68fd        none                null                local

搞一个难度稍微复杂一点的,文件名docker-compose.yml

version: '2'

networks:
  thinking:
  
services:
  test_1.thinking.com:
    image: ubuntu
    ports:
      - "3333:3333"
    container_name: test_1.thinking.com
    command: /bin/bash
    tty: true
    networks:
      - thinking

  test_2.thinking.com:
    image: ubuntu
    ports:
      - "4444:4444"
    container_name: test_2.thinking.com
    command: /bin/bash
    tty: true
    networks:
      - thinking

$ docker-compose -f docker-compose.yml up -d
Creating network "desktop_thinking" with the default driver
Creating test_1.thinking.com ... done
Creating test_2.thinking.com ... done
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
cb8dad81260a        ubuntu              "/bin/bash"         6 seconds ago       Up 5 seconds        0.0.0.0:3333->3333/tcp   test_1.thinking.com
251d6be64c37        ubuntu              "/bin/bash"         37 seconds ago      Up 36 seconds       0.0.0.0:4444->4444/tcp   test_2.thinking.com
f6eb5ff5d3af        ubuntu              "/bin/bash"         4 minutes ago       Up 4 minutes                                 desktop_thinking_1
$ docker network list
NETWORK ID          NAME                DRIVER              SCOPE
421718219c6c        bridge              bridge              local
85d6103b3e6a        desktop_default     bridge              local
f0be99571567        desktop_thinking    bridge              local
37b352ae0dbf        host                host                local
9625fcfe68fd        none                null                local

分别进入test_1.thinking.com和test_2.thinking.com

$ docker exec -it test_1.thinking.com /bin/bash
root@3380956f8851:/#

查询IP地址

root@3380956f8851:/# ifconfig

bash: ifconfig: command not found

安装ifconfig(和ping)

# apt-get update

# apt install net-tools

# apt install iputils-ping

# ifconfig                
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.2  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:02  txqueuelen 0  (Ethernet)
        RX packets 6489  bytes 26041395 (26.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6350  bytes 515235 (515.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 802 (802.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 802 (802.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

对test_2.thinking.com查询

# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.3  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:03  txqueuelen 0  (Ethernet)
        RX packets 6107  bytes 26015443 (26.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5870  bytes 510179 (510.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 802 (802.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 802 (802.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

这说明test_1.thinking.com和test_2.thinking.com位于同一个虚拟局域网。尝试test_2.thinking.com ping test_1.thinking.com

# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.110 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 172.18.0.2: icmp_seq=3 ttl=64 time=0.071 ms
64 bytes from 172.18.0.2: icmp_seq=4 ttl=64 time=0.061 ms

64 bytes from 172.18.0.2: icmp_seq=5 ttl=64 time=0.075 ms

可通!

ping test_1.thinking.com
PING test_1.thinking.com (172.18.0.2) 56(84) bytes of data.
64 bytes from test_1.thinking.com.desktop_thinking (172.18.0.2): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from test_1.thinking.com.desktop_thinking (172.18.0.2): icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from test_1.thinking.com.desktop_thinking (172.18.0.2): icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from test_1.thinking.com.desktop_thinking (172.18.0.2): icmp_seq=4 ttl=64 time=0.054 ms

64 bytes from test_1.thinking.com.desktop_thinking (172.18.0.2): icmp_seq=5 ttl=64 time=0.053 ms

亦可!!!



猜你喜欢

转载自blog.csdn.net/yongyu_it/article/details/80476423