美文网首页Docker
5. Docker Compose 安装使用

5. Docker Compose 安装使用

作者: andy0898 | 来源:发表于2016-10-19 19:22 被阅读2116次

    概况

    Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

    Using Compose is basically a three-step process.

    1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
    2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
    3. Lastly, run docker-compose up and Compose will start and run your entire app.

    Docker Compose 是定义和运行多容器 Docker 应用的工具。何谓多容器?Gogs 应用需要gogs 和 mysql这两个容器。如果没有Compose 工具,就必须分别安装好Mysql镜像和Gogs镜像,再分别启动。Compose 工具提供了一种能力,一个命令就可以创建和启动Gogs 应用

    参考网页: Docker Compose Overview

    Docker Compose 的安装

    前置条件

    • 桌面操作系统: Win 10 64位家庭中文版
    • 虚拟机软件 : Oracle VirtualBox 5.1.6
    • 虚拟机操作系统 : CentOS Linux release 7.2.1511 (Core)
    • Linux 内核 : 3.10.0-327.36.2.el7.x86_64
    • docker 安装版本:docker 1.12.2

    安装过程
    参考页面: Compose Install

    1. 下载 docker-compose 并安装
    [root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   600    0   600    0     0    363      0 --:--:--  0:00:01 --:--:--   363
      0 7798k    0 69186    0     0    304      0  7:17:50  0:03:47  7:14:03     0
    [root@localhost ~]# chmod +x /usr/local/bin/docker-compose
    [root@localhost ~]# ll /usr/local/bin/docker-compose
    -rwxr-xr-x. 1 root root 7986086 10月 19 13:04 /usr/local/bin/docker-compose
    [root@localhost ~]# docker-compose --version
    docker-compose version 1.8.1, build 878cff1
    
    1. 编写Docker Compose 服务配置文件 yml
    [root@localhost ~]# mkdir compose
    [root@localhost ~]# cd compose
    [root@localhost compose]# vi docker-compose.yml
    # mysql service
    mydb:
        image: mysql:latest
        ports:
          - "13307:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 123456
        volumes:
          - /opt/mydata2/:/var/lib/mysql
    # gogs service
    mygogs:
        image: gogs/gogs:latest
        ports:
          - "10033:22"
          - "10090:3000"
        volumes:
          - /var/gogs2/:/data
        links:
          - mydb
    [root@localhost compose]# docker-compose up
    
    1. 使用Compose 启动应用
    [root@localhost compose]# docker-compose up
    [root@localhost compose]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
    07a5e03d6353        gogs/gogs:latest    "docker/start.sh /bin"   4 minutes ago       Up 4 minutes        0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp   compose_mygogs_1
    fd10f315ad68        mysql:latest        "docker-entrypoint.sh"   4 minutes ago       Up 4 minutes        0.0.0.0:13307->3306/tcp                          compose_mydb_1
    

    说明:

    • 默认情况,docker-compose 会找当前目录下的 docker-compose.yml 文件,工程名使用目录名
    • 所以 docker ps 看到的容器名称是[工程名]_[服务名]_1
    1. docker-compose 命令参数
    [root@localhost compose]# mv docker-compose.yml gogs_app.yml
    [root@localhost compose]# ls
    gogs_app.yml
    [root@localhost compose]# docker-compose -f gogs_app.yml -p GogsApp up -d
    Creating gogsapp_mydb_1
    Creating gogsapp_mygogs_1
    [root@localhost compose]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                            NAMES
    a5c85165cb29        gogs/gogs:latest    "docker/start.sh /bin"   About a minute ago   Up About a minute   0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp   gogsapp_mygogs_1
    43e0a6d11c5a        mysql:latest        "docker-entrypoint.sh"   About a minute ago   Up About a minute   0.0.0.0:13307->3306/tcp                          gogsapp_mydb_1
    

    命令 docker-compose -f gogs_app.yml -p GogsApp up -d 说明如下:

    • -f gogs_app.yml 指定读取的 yml 文件
    • -p GogsApp 指定工程名
    • -d 指定是docker-compose 以后台进程的方式启动
    1. Compose 配置文件格式有两个版本,推荐使用Version 2.0

    here are two versions of the Compose file format:

    • Version 1, the legacy format. This is specified by omitting a version key at the root of the YAML.
    • Version 2, the recommended format. This is specified with a version: '2' entry at the root of the YAML.

    我们把上述例子修改成版本2.0,然后演示了启动应用、列出容器、停止容器、启动容器、卸载应用

    [root@localhost compose]# vi gogs_app.yml
    # compose demo
    version: '2'
    
    services:
        # mysql service
        mydb:
            image: mysql:latest
            ports:
                - "13307:3306"
            environment:
                MYSQL_ROOT_PASSWORD: 123456
            volumes:
                - /opt/mydata2/:/var/lib/mysql
        # gogs service
        mygogs:
            image: gogs/gogs:latest
            ports:
                - "10033:22"
                - "10090:3000"
            volumes:
                - /var/gogs2/:/data
            links:
                - mydb
    [root@localhost compose]# docker-compose -f gogs_app.yml -p gapp up -d
    Creating network "gapp_default" with the default driver
    Creating gapp_mydb_1
    Creating gapp_mygogs_1
    [root@localhost compose]# docker-compose -f gogs_app.yml -p gapp ps
        Name                   Command               State                       Ports
    -------------------------------------------------------------------------------------------------------
    gapp_mydb_1     docker-entrypoint.sh mysqld      Up      0.0.0.0:13307->3306/tcp
    gapp_mygogs_1   docker/start.sh /bin/s6-sv ...   Up      0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp
    [root@localhost compose]# docker-compose -f gogs_app.yml -p gapp stop
    Stopping gapp_mygogs_1 ... done
    Stopping gapp_mydb_1 ... done
    [root@localhost compose]# docker-compose -f gogs_app.yml -p gapp start
    Starting mydb ... done
    Starting mygogs ... done
    [root@localhost compose]# docker-compose -f gogs_app.yml -p gapp down
    Stopping gapp_mygogs_1 ... done
    Stopping gapp_mydb_1 ... done
    Removing gapp_mygogs_1 ... done
    Removing gapp_mydb_1 ... done
    Removing network gapp_default
    

    参考

    Docker Compose 配置文件说明
    Docker Compose Get Started
    YAML 模板文件

    相关文章

      网友评论

      本文标题:5. Docker Compose 安装使用

      本文链接:https://www.haomeiwen.com/subject/vklxuttx.html