单机编排工具compose

compose

 

docker    compose    是使用yml文件来定义容器关系的,因此掌握docker-compose.yml文件的写法才能更好的书写配置文件,方便管理多容器应用。

docker compose实际上是把yml文件解析成原生的docker命令然后执行的,他通过定义解析器依赖关系来按顺序启动容器。

使用compose最重要的是,减少使用繁琐的启动命令。 

一份标准的配置文件应该包含version、services、network这三大部分。

 

[root@localhost /]# docker-compose
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
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to
 # 设置Daemon的socket地址 
  --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 (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

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

 

services:                        
    web1:                                    #     服务名称
        image:    redis:14.04                    #    镜像名称或镜像ID

        build:                                #    自动构建镜像    
            # 如果build/image同时存在,那么compose会构建镜像并且把镜像命名为image后面的那个名字
            
            context: ../                    #    设定根目录
            dockerfile:    path/of/Dockerfile    #    指定Dockerfile文件
            arg:
                buildno:    1
                password:    secret            #     在构建过程中指定环境变量
            # arg:
                #    -buildno:    1            #    也可以这么写
                #    -password                  #     也可以设置空值,当设置布尔值时,要用引号引起来,否则会被当成字符串解析
        command:    bundle    exec    thin    -p    3000    # 可以覆盖默认的命令
            # command:    ['bundle',    'exec',    'thin',    '-p',    '3000']
            
        container_name:        app                #    指定容器的名字
            
        depends_on:
            
            -    web1                        # 指定服务名称,此服务必须先启动
                                                                                            
        dns:    8.8.8.8
            # 或者    dns:
                        - 8.8.8.8
                        - 9.9.9.9
        dns_search:    example.com                # 格式和dns一样
        
        tmpfs:    /run                        # 格式和上边一样,挂载临时目录到容器内部,和run的参数效果一样
                
                                                                                            
        entrypoint:        /code/entrypoinst.sh# 指定服务镜像的接入点                                    
        
        env_file:                            # 设置compose变量
            - ./common.env                    # 从隐藏文件env中获取
                                                                                                
        environment:
            SHOW    :    'true'                # 当和env_file冲突,以当前为准
            - SHOW    :    'true'                 
            - SHOW    :    

        expose:                                # 暴露端口
            - '3000'
            - '5000'
                                                                                            
                                                                                            
        external_links:
            -    project_db_1:mysql            # 连接
                                                                                            
                                                                                            
        extra_hosts:                        # 添加主机名的标签,就是向/etc/hosts文件中添加一些记录    
            -    "somehost:162.158.241.82"
        
        labels:
            - "email=ace@qq.com"
            
        links:                                # 容器之间互联                
            -reids:alpine                    
        
        logging:
            driver:syslog                    # 默认是json-file
            options:
                syslog-address:"tcp://192.168.0.42:123"
        
        pid:    "host"                        # 指定进程空间,将能够访问和操纵其他容器和宿主机的名称空间
        
        ports:
            - "49100:22"                    # 使用HOST:CONTAINER格式(大于60)或者只是指定容器的端口,宿主机会随机映射端口
            
            
        security_opt:
            - label:user:USER                # 覆盖默认的标签,简单来说就是管理全部服务的标签。
            - label:role:ROLE                # 
        
        
        
        stop_signale:    SIGUSR1                # 设置另一个信号来停止容器,默认情况下使用的是SIGTERM停止容器。
        
        
        volumes:                            # 设置容器数据卷
            # 可以直接使用[HOSTS:CONTAINER]
            # 可以直接使用[HOSTS:CONTAINER:ro]    这两种方式来挂载,只不过这一种是可读的
            
            - /var/lib/mysql                # 容器内部
            - /opt/data:/var/lib/mysql        # 使用绝对路径挂载
            - ./cache:/tmp/cache            # 以compose配置文件为中心的相对路径作为数据卷挂载到容器
            - ~/configs:/etc/configs/:ro
            - datavolume:/var/lib/mysql        # 已存在命名的数据卷
            
            
        volumes_from:
            - container:container_name:ro                    # 有ro,rw
            - service_name            
            
        cgroup_parent:    m-executor-abcd        # 指定一个容器的父级cgroup
 

 

 

容器部署django

 

FROM python:3.5.3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
COPY requirements.txt /code/
WORKDIR  /code
RUN pip3 install -r requirements.txt
ADD . /code/
1、编写Dockerfile
django==2.0.1
2、编写requirements.txt
version: '2'
services:
        db:
                image: postgres
        app:
                build: .
                command:        python3 manage.py runserver 0.0.0.0:8000
                volumes:
                        - .:/code
                ports:
                        - "8000:8000"
                depends_on:
                        - db
3、compose编排

 

docker-compose  run  app  django-admin.py startproject  example_demo   .
.
├── docker-compose.yml
├── Dockerfile
├── example_demo
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── requirements.txt
sudo    chown    -R    $USER:$USER    . 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER':'postgres',
        'HOST':'db',
        'PORT':5432
    }
}
4、编辑settings.py
docker-compose  up

 

posted @ 2018-12-25 22:07  慕沁  阅读(267)  评论(0)    收藏  举报