Docker&Kubernetes ❀ Docker Compose使用方法与相关解释

Posted 无糖可乐没有灵魂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Docker Compose使用方法与相关解释相关的知识,希望对你有一定的参考价值。

文章目录

1、概念说明


Docker提倡概念是 一个容器一个进程,假设一个服务需要多个进程组成,那么就需要多个容器组成一个系统,相互分工和配合对外提供完整服务;
在启动容器时,同一台主机下如果两个容器之间需要有数据交流,使用 --link 选项建立两个容器之间的互联,前提是建立mariadb已经开启了;
Docker compose 容器编排工具 允许用户在一个模板(YAML格式)中定义一组相关联的容器,会根据 --link 等参数对启动的优先级进行排序;

2、服务安装


[root@localhost ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod a+x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose --version
cker-compose version 1.24.1, build 4667896b

文件百度网盘连接
链接:https://pan.baidu.com/s/15xPqYJQj4386iFm_Tbt3iQ
提取码:cvyg
文件MD5值:7048a965a86e6eed1622e0990e9a7ab4 docker-compose

3、使用教程


3.1 常见命令:

  • -f :指定使用的yaml文件路径;
  • ps :显示所有容器信息;
[root@localhost ~]# docker-compose ps
  • restart :重启容器;
  • logs :查看日志信息;
[root@localhost ~]# docker-compose logs
  • config -q :验证yaml配置文件是否正确;
  • stop :停止容器;
  • start :启动容器;
  • up -d :启动容器项目;
  • pause :暂停容器;
  • unpause :恢复容器;
  • rm :删除容器;
  • kill : 停止容器;
  • pull : 下载服务镜像;
  • port :打印出绑定的公共端口;
[root@localhost ~]# docker-compose port test 8080
  • build : 构建或者重新构建服务;
  • scale : 设置指定服务运行容器的个数,以service=num形式指定;
[root@localhost ~]# docker-compose scale user=3 movie=3
  • run :在某个服务上执行某条命令;
[root@localhost ~]# docker-compose run web bash

命令帮助内容参考;

[root@localhost ~]# 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 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

3.2 Compose.yml属性

  • version :指定docker-compose.yml文件的写法格式;
  • services :指定多个容器集合;
  • build :配置构建时,Compose会利用自动构建镜像,该值可以是一个路径也可以是一个对象,用于指定DockerFile参数;
build: ./dir
---------------
build:
    context: ./dir
    dockerfile: Dockerfile
    args:
        buildno: 1
  • command : 覆盖容器启动后默认执行的命令;
command: bundle exec thin -p 3000
----------------------------------
command: [bundle,exec,thin,-p,3000]
  • dns :配置DNS服务,可以是一个值或列表;
  • dns_search :配置DNS搜索域,可以是一个值或列表;
  • environment :环境变量配置,可以用数组或字典两种方式;
  • env_file:从文件中获取环境变量,可以指定一个文件路径或路径列表,其优先级低于environment指定的环境变量;
  • expose : 暴露端口,只将端口暴露给连接的服务,而不暴露给主机;
[root@localhost ~]# cat docker-compose.yaml 
version: '2'               #该Docker Compose文件使用的是V2;

services:                  
  db:                      #定义服务;
    image: mysql:5.7       #镜像名称与版本;
    restart: always
    environment:           #定义环境变量;
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    depends_on:            #等同于--link
      - db
    image: wordpress:latest
    restart: always
    ports:                 #端口映射关系;
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress

#后台启动并运行容器
[root@localhost ~]# docker-compose up -d

查看结果;

[root@localhost ~]# docker-compose images
Container   Repository   Tag   Image Id   Size
----------------------------------------------

[root@localhost ~]# docker-compose ps
Name   Command   State   Ports
------------------------------

以上是关于Docker&Kubernetes ❀ Docker Compose使用方法与相关解释的主要内容,如果未能解决你的问题,请参考以下文章

Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理

Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理

Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法

Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法

Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理

Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理