Docker Compose

@bingoogolapple 2017-01-22 01:42:12发表于 bingoogolapple/bingoogolapple.github.io Docker

docker-compose.yml

version: '2'   # 指定compose版本号
services:
    phoenix:
        image: nginx    #指定服务要使用的镜像
        ports:
            - "8080:80"   # 主机端口:容器端口
    dragon:
        image: nginx
        ports:
            - "8081:80"
➜  DockerNote ls
docker-compose.yml
➜  DockerNote docker-compose up
Creating network "dockernote_default" with the default driver        // 创建一个叫 dockernote_default 的网络
Creating dockernote_dragon_1         // 项目名字作为前缀,由于这里没有指定项目名字,会使用项目目录的名字
Creating dockernote_phoenix_1
Attaching to dockernote_dragon_1, dockernote_phoenix_1

// 此时可以打开浏览器访问  http://192.168.99.100:8081         http://192.168.99.100:8080

后台运行服务

➜  DockerNote docker-compose up -d
Starting dockernote_dragon_1
Starting dockernote_phoenix_1
➜  DockerNote docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
2c1c4e0be788        nginx               "nginx -g 'daemon off"   8 minutes ago       Up 4 seconds        443/tcp, 0.0.0.0:8080->80/tcp                   dockernote_phoenix_1
f3f6a02f6cb4        nginx               "nginx -g 'daemon off"   8 minutes ago       Up 4 seconds        443/tcp, 0.0.0.0:8081->80/tcp                   dockernote_dragon_1

服务生命周期

➜  DockerNote docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
2c1c4e0be788        nginx               "nginx -g 'daemon off"   17 minutes ago      Up 8 minutes        443/tcp, 0.0.0.0:8080->80/tcp   dockernote_phoenix_1
f3f6a02f6cb4        nginx               "nginx -g 'daemon off"   17 minutes ago      Up 8 minutes        443/tcp, 0.0.0.0:8081->80/tcp   dockernote_dragon_1
➜  DockerNote docker-compose stop phoenix     // 停止指定服务
Stopping dockernote_phoenix_1 ... done
➜  DockerNote docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
f3f6a02f6cb4        nginx               "nginx -g 'daemon off"   18 minutes ago      Up 9 minutes        443/tcp, 0.0.0.0:8081->80/tcp   dockernote_dragon_1
➜  DockerNote docker-compose stop        // 停止所有在compose文件中定义的服务
Stopping dockernote_dragon_1 ... done
➜  DockerNote docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
➜  DockerNote docker-compose ps
        Name                 Command          State    Ports 
------------------------------------------------------------
dockernote_dragon_1    nginx -g daemon off;   Exit 0         
dockernote_phoenix_1   nginx -g daemon off;   Exit 0 
➜  DockerNote docker-compose start     // 启动所有服务,或在start后面指定服务的名字来启动指定的服务
Starting phoenix ... done
Starting dragon ... done
➜  DockerNote docker-compose logs -f     // 查看服务的日志
➜  DockerNote docker-compose exec phoenix bash    // 登陆到某一个服务容器里
root@2c1c4e0be788:/# exit                                                                                                                                              
exit

➜  DockerNote docker-compose stop
Stopping dockernote_phoenix_1 ... done
Stopping dockernote_dragon_1 ... done
➜  DockerNote docker-compose rm    // 删除所有服务的容器,这里不会删除创建的网络和数据卷
WARNING: Not including one-off containers created by `docker-compose run`.
To include them, use `docker-compose rm --all`.
This will be the default behavior in the next version of Compose.

Going to remove dockernote_phoenix_1, dockernote_dragon_1
Are you sure? [yN] y
Removing dockernote_phoenix_1 ... done
Removing dockernote_dragon_1 ... done
➜  DockerNote docker-compose ps
Name   Command   State   Ports 
------------------------------
➜  DockerNote docker network ls   // 查看网络列表
NETWORK ID          NAME                 DRIVER
51f5af98eba6        bridge               bridge              
629fa8588739        dockernote_default   bridge              
5b3a9e3a6c9e        host                 host                
8060a77be0f9        none                 null                
1fc17e46d238        web                  bridge              
➜  DockerNote docker-compose down     // 删除所有创建的东西,如果有单独挂载数据卷,数据卷不会被删除
Removing network dockernote_default
➜  DockerNote docker network ls  
NETWORK ID          NAME                DRIVER
51f5af98eba6        bridge              bridge              
5b3a9e3a6c9e        host                host                
8060a77be0f9        none                null                
1fc17e46d238        web                 bridge

网络

➜  DockerNote docker-compose up -d
Creating network "dockernote_default" with the default driver
Creating dockernote_dragon_1
Creating dockernote_phoenix_1
➜  DockerNote docker network ls   
NETWORK ID          NAME                 DRIVER
51f5af98eba6        bridge               bridge              
1f8adb0d08e2        dockernote_default   bridge              
5b3a9e3a6c9e        host                 host                
8060a77be0f9        none                 null                
1fc17e46d238        web                  bridge              
➜  DockerNote docker-compose exec phoenix bash
root@3520ad75fe8d:/# ping dragon                                                                                                                                       
PING dragon (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: icmp_seq=0 ttl=64 time=0.078 ms
64 bytes from 172.19.0.2: icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from 172.19.0.2: icmp_seq=2 ttl=64 time=0.102 ms
^C--- dragon ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.075/0.085/0.102/0.000 ms
root@3520ad75fe8d:/# exit
exit
➜  DockerNote docker-compose exec dragon bash 
root@0ef017a3373f:/# ping phoenix
PING phoenix (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: icmp_seq=0 ttl=64 time=0.065 ms
64 bytes from 172.19.0.3: icmp_seq=1 ttl=64 time=0.076 ms
^C--- phoenix ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.065/0.071/0.076/0.000 ms
root@0ef017a3373f:/# exit
exit

docker-compose.yml

version: '2'   # 指定compose版本号
services:
    phoenix:
        image: nginx    #指定服务要使用的镜像
        ports:
            - "8080:80"   # 主机端口:容器端口
        networks:         # 指定服务属于的网络
            - "fairyland"
    dragon:
        image: nginx
        ports:
            - "8081:80"
        networks:
            - "fairyland"
    monkey:
        image: nginx
        ports:
            - "8082:80"
        networks:
            - "default"
networks:
    fairyland:       # 网络的名字
        driver: bridge      # 网络的类型「冒号后面必须要有一个空格」
➜  DockerNote docker-compose up -d
Creating network "dockernote_fairyland" with driver "bridge"
Recreating dockernote_phoenix_1
Recreating dockernote_dragon_1
Creating dockernote_monkey_1
➜  DockerNote docker-compose exec phoenix bash
root@c1026fb9d0f4:/# ping dragon                                                                                                                                       
PING dragon (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: icmp_seq=0 ttl=64 time=0.084 ms
64 bytes from 172.20.0.3: icmp_seq=1 ttl=64 time=0.096 ms
^C--- dragon ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.084/0.090/0.096/0.000 ms
root@c1026fb9d0f4:/# exit
exit
➜  DockerNote docker-compose exec dragon bash 
root@4135f95d4b8b:/# ping phoenix                              // phoenix 和 dragon在同一个网络里面                                                                                                      
PING phoenix (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: icmp_seq=0 ttl=64 time=0.065 ms
64 bytes from 172.20.0.2: icmp_seq=1 ttl=64 time=0.094 ms
64 bytes from 172.20.0.2: icmp_seq=2 ttl=64 time=0.076 ms
^C--- phoenix ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.065/0.078/0.094/0.000 ms
root@4135f95d4b8b:/# ping monkey        //   monkey 属于default这个网络,如果想要dragon和monkey能ping通,可以把dragon也加入到default网络
ping: unknown host

Volumes数据卷

docker-compose.yml

version: '2'   # 指定compose版本号
services:
    phoenix:
        image: nginx    #指定服务要使用的镜像
        ports:
            - "8080:80"   # 主机端口:容器端口
        networks:         # 指定服务属于的网络
            - "fairyland"
        volumes:
            - nest:/mnt    # 数据卷的名字:挂载到服务容器上的位置
    dragon:
        image: nginx
        ports:
            - "8081:80"
        networks:   # 既属于fairyland网络又属于default网络
            - "fairyland"
            - "default"
        volumes:
            - nest:/mnt
    monkey:
        image: nginx
        ports:
            - "8082:80"
        networks:
            - "default"
networks:
    fairyland:       # 网络的名字
        driver: bridge      # 网络的类型「冒号后面必须要有一个空格」
volumes:
    nest:   # 数据卷的名字
        driver: local    # 数据卷的类型,默认就是local
➜  DockerNote docker-compose up -d
Creating volume "dockernote_nest" with local driver
dockernote_monkey_1 is up-to-date
Recreating dockernote_dragon_1
Recreating dockernote_phoenix_1
➜  DockerNote docker-compose exec phoenix bash
root@b606210d5f90:/# cd /mnt/                                                                                                                                          
root@b606210d5f90:/mnt# touch phoenix_data_1     // 先在 phoenix 上创建一个文件
root@b606210d5f90:/mnt# ls
phoenix_data_1
root@b606210d5f90:/mnt# exit
exit
➜  DockerNote docker-compose exec dragon bash 
root@4ce23dc7cc65:/# cd /mnt/                                                                                                                                          
root@4ce23dc7cc65:/mnt# ls
phoenix_data_1                          // dragon上出现了在phoenix上创建的文件,因为dragon和phoenix这两个服务都使用了nest这个数据卷
root@4ce23dc7cc65:/mnt# exit
exit
指定位置的数据卷

docker-compose.yml

version: '2'   # 指定compose版本号
services:
    phoenix:
        image: nginx    #指定服务要使用的镜像
        ports:
            - "8080:80"   # 主机端口:容器端口
        networks:         # 指定服务属于的网络
            - "fairyland"
        volumes:
            - nest:/mnt    # 数据卷的名字:挂载到服务容器上的位置
            - ./app/web:/usr/share/nginx/html    # 指定位置的数据卷,将自己主机上xxxxx目录挂载到nginx默认主机的根目录
    dragon:
        image: nginx
        ports:
            - "8081:80"
        networks:   # 既属于fairyland网络又属于default网络
            - "fairyland"
            - "default"
        volumes:
            - nest:/mnt
            - ./app/web:/usr/share/nginx/html    # 指定位置的数据卷,将自己主机上xxxxx目录挂载到nginx默认主机的根目录
    monkey:
        image: nginx
        ports:
            - "8082:80"
        networks:
            - "default"
networks:
    fairyland:       # 网络的名字
        driver: bridge      # 网络的类型「冒号后面必须要有一个空格」
volumes:
    nest:   # 数据卷的名字
        driver: local    # 数据卷的类型,默认就是local

image

➜  DockerNote docker-compose up -d            
dockernote_monkey_1 is up-to-date
Recreating dockernote_dragon_1
Recreating dockernote_phoenix_1

此时访问  http://192.168.99.100:8080 就是显示上面这个html界面