美文网首页
Docker Compose 多容器部署

Docker Compose 多容器部署

作者: Haishenming | 来源:发表于2018-07-15 15:29 被阅读0次

    需求描述

    很多时候我们需要多个容器配合使用,但是一个一个的去配置部署是一件很麻烦的事情,这时候就需要一个能够一处配置,到处使用的方案,于是就有了compose。

    Compose 安装

    安装比较简单,查一下文档就知道了,比如这里要安装1.21.2版本:

    sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose     # 下载可执行文件
    
    sudo chmod +x /usr/local/bin/docker-compose   # 如果需要的话,添加可执行权限
    
    docker-compose --version  # 试一试安装成功了没有
    
    

    一个例子

    compose默认使用名为docker-compose.yml的配置文件,我们先来试一试。

    下面的例子创建了一组container,主体部分是一个wordpress服务,使用mysql作为数据库。

    docker-compose.yml

    version: '3'
    
    services:
    
      wordpress:
        image: wordpress
        ports:
          - 8080:80
        depends_on:
          - mysql
        environment:
          WORDPRESS_DB_HOST: mysql:3306
          WORDPRESS_DB_PASSWORD: root
        networks:
          - my-bridge
    
      mysql:
        image: mysql:5.7
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress
        volumes:
          - mysql-data:/var/lib/mysql
        networks:
          - my-bridge
    
    volumes:
      mysql-data:
    
    networks:
      my-bridge:
        driver: bridge
    
    

    然后在命令行执行

    docker-compose up
    

    程序便会在当前目录下找到docker-compose.yml文件并根据配置创建相应的container。

    如果要指定yml文件, 可执行

    docker-compose -f youfile.yml up
    

    这样的话,运行的日志会持续打印,如果要让其后台运行的话,和docker一样,加入-d参数就可以了。

    然后执行一下docker ps,看看container是否正常启动。

    最后打开浏览器,看一看是否能打开wordpress

    更多命令

    compose 还有许多功能,我们可以使用docker-compose --help查看

    docker-compose --help
    
    Define and run multi-container applications with Docker.
    
    Usage:
      docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
      docker-compose -h|--help
    
    Options:
      -f, --file FILE             Specify an alternate compose file
                                  (default: docker-compose.yml)
      -p, --project-name NAME     Specify an alternate project name
                                  (default: directory name)
      --verbose                   Show more output
      --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
      --no-ansi                   Do not print ANSI control characters
      -v, --version               Print version and exit
      -H, --host HOST             Daemon socket to connect to
    
      --tls                       Use TLS; implied by --tlsverify
      --tlscacert CA_PATH         Trust certs signed only by this CA
      --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
      --tlskey TLS_KEY_PATH       Path to TLS key file
      --tlsverify                 Use TLS and verify the remote
      --skip-hostname-check       Don't check the daemon's hostname against the
                                  name specified in the client certificate
      --project-directory PATH    Specify an alternate working directory
                                  (default: the path of the Compose file)
      --compatibility             If set, Compose will attempt to convert deploy
                                  keys in v3 files to their non-Swarm equivalent
    
    Commands:
      build              Build or rebuild services
      bundle             Generate a Docker bundle from the Compose file
      config             Validate and view the Compose file
      create             Create services
      down               Stop and remove containers, networks, images, and volumes
      events             Receive real time events from containers
      exec               Execute a command in a running container
      help               Get help on a command
      images             List images
      kill               Kill containers
      logs               View output from containers
      pause              Pause services
      port               Print the public port for a port binding
      ps                 List containers
      pull               Pull service images
      push               Push service images
      restart            Restart services
      rm                 Remove stopped containers
      run                Run a one-off command
      scale              Set number of containers for a service
      start              Start services
      stop               Stop services
      top                Display the running processes
      unpause            Unpause services
      up                 Create and start containers
      version            Show the Docker-Compose version information
    
    

    其中很多命令和docker的差不多,比较容易理解。

    再来一个例子

    如果我们需要自己创建一个image来启动container 怎么弄的

    Dockerfile

    FROM python:2.7
    LABEL maintaner="Peng Xiao xiaoquwl@gmail.com"
    COPY . /app
    WORKDIR /app
    RUN pip install flask redis
    EXPOSE 5000
    CMD [ "python", "app.py" ]
    

    docker-compose.yml

    version: "3"
    
    services:
    
      redis:
        image: redis
    
      web:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - 8080:5000
        environment:
          REDIS_HOST: redis
    
    

    然后创建的方式和上面一样。

    相关文章

      网友评论

          本文标题:Docker Compose 多容器部署

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