一般步骤

1、定义Dockerfile,方便迁移到任何地方;

2、编写docker-compose.yml文件;

3、运行docker-compose up启动服务

示例

准备工作:提前下载好镜像:

docker pull mysql docker pull wordpress

需要新建一个空白目录,例如wptest。新建一个docker-compose.yml

version: '2' services: web: image: wordpress:latest links: - db ports: - "8002:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: 123456 db: image: mysql environment: - MYSQL_ROOT_PASSWORD=123456

以上命令的意思是新建db和wordpress容器。等同于:

$ docker run --name db -e MYSQL_ROOT_PASSWORD=123456 -d mysql $ docker run --name some-wordpress --link db:mysql -p 8002:80 -d wordpress

注意,如果你是直接从fig迁移过来的,且web里links是- db:mysql,这里会提示没有给wordpress设置环境变量,这里需要添加环境变量WORDPRESS_DB_HOST和WORDPRESS_DB_PASSWORD。

好,我们启动应用:

# docker-compose up Creating wptest_db_1... Creating wptest_wordpress_1... Attaching to wptest_db_1, wptest_wordpress_1 wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html

就成功了。浏览器访问 http://localhost:8002(或 http://host-ip:8002)即可

默认是前台运行并打印日志到控制台。如果想后台运行,可以:

docker-compose up -d

服务后台后,可以使用下列命令查看状态:

# docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------- figtest_db_1 docker-entrypoint.sh mysqld Up 3306/tcp figtest_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8002->80/tcp # docker-compose logs Attaching to wptest_wordpress_1, wptest_db_1 db_1 | 2016-10-4T14:38:46.98030Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). db_1 | 2016-10-4T14:38:46.99974Z 0 [Note] mysqld (mysqld 5.7.15) starting as process 1 ... db_1 | 2016-10-4T14:38:46.27191Z 0 [Note] InnoDB: PUNCH HOLE support available

停止服务:

# docker-compose stop Stopping wptest_wordpress_1... Stopping wptest_db_1...

重新启动服务:

docker-compose restart

docker-compose.yml参考

每个docker-compose.yml必须定义image或者build中的一个,其它的是可选的。

image

指定镜像tag或者ID。示例:

image: redis image: ubuntu:14.04 image: tutum/influxdb image: example-registry.com:4000/postgresql image: a4bc65fd

注意,在version 1里同时使用image和build是不允许的,version 2则可以,如果同时指定了两者,会将build出来的镜像打上名为image标签。

build

用来指定一个包含Dockerfile文件的路径。一般是当前目录.。Fig将build并生成一个随机命名的镜像。

注意,在version 1里bulid仅支持值为字符串。version 2里支持对象格式。

build: ./dir build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1

context为路径,dockerfile为需要替换默认docker-compose的文件名,args为构建(build)过程中的环境变量,用于替换Dockerfile里定义的ARG参数,容器中不可用。示例:

Dockerfile:

ARG buildno ARG password RUN echo "Build number: $buildno" RUN script-requiring-password.sh "$password"

docker-compose.yml:

build: context: . args: buildno: 1 password: secret build: context: . args: - buildno=1 - password=secret

command

用来覆盖缺省命令。示例:

command: bundle exec thin -p 3000

command也支持数组形式:

command: [bundle, exec, thin, -p, 3000]

links

用于链接另一容器服务,如需要使用到另一容器的mysql服务。可以给出服务名和别名;也可以仅给出服务名,这样别名将和服务名相同。同docker run --link。示例:

links: - db - db:mysql - redis

使用了别名将自动会在容器的/etc/hosts文件里创建相应记录:

172.17.2.186 db 172.17.2.186 mysql 172.17.2.187 redis

所以我们在容器里就可以直接使用别名作为服务的主机名。

ports

用于暴露端口。同docker run -p。示例:

ports: - "3000" - "8000:8000" - "49100:22" - "127.0.0.1:8001:8001"

expose

expose提供container之间的端口访问,不会暴露给主机使用。同docker run --expose。

expose: - "3000" - "8000"

volumes

挂载数据卷。同docker run -v。示例:

volumes: - /var/lib/mysql - cache/:/tmp/cache - ~/configs:/etc/configs/:ro

volumes_from

挂载数据卷容器,挂载是容器。同docker run --volumes-from。示例:

volumes_from: - service_name - service_name:ro - container:container_name - container:container_name:rw

container:container_name格式仅支持version 2。

environment

添加环境变量。同docker run -e。可以是数组或者字典格式:

environment: RACK_ENV: development SESSION_SECRET: environment: - RACK_ENV=development - SESSION_SECRET

depends_on

用于指定服务依赖,一般是mysql、redis等。

指定了依赖,将会优先于服务创建并启动依赖。

links也可以指定依赖。

external_links

链接搭配docker-compose.yml文件或者Compose之外定义的服务,通常是提供共享或公共服务。格式与links相似:

external_links: - redis_1 - project_db_1:mysql - project_db_1:postgresql

注意,external_links链接的服务与当前服务必须是同一个网络环境。

extra_hosts

添加主机名映射。

extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"

将会在/etc/hosts创建记录:

162.242.195.82 somehost 50.31.209.229 otherhost

extends

继承自当前yml文件或者其它文件中定义的服务,可以选择性的覆盖原有配置。

extends: file: common.yml service: webapp

service必须有,file可选。service是需要继承的服务,例如web、database。

net

设置网络模式。同docker的--net参数。

net: "bridge" net: "none" net: "container:[name or id]" net: "host"

dns

自定义dns服务器。

dns: 8.8.8.8 dns: - 8.8.8.8 - 9.9.9.9

cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir

这些命令都是单个值,含义请参考docker run

cpu_shares: 73 cpu_quota: 50000 cpuset: 0,1 user: postgresql working_dir: /code domainname: foo.com hostname: foo ipc: host mac_address: 02:42:ac:11:65:43 mem_limit: 1000000000 mem_limit: 128M memswap_limit: 2000000000 privileged: true restart: always read_only: true shm_size: 64M stdin_open: true tty: true

命令行参考

$ 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 -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 (for example if your docker host is an IP address) 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 kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls 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 unpause Unpause services up Create and start containers version Show the Docker-Compose version information

批处理脚本

# 关闭所有正在运行容器 docker ps | awk '{print $1}' | xargs docker stop # 删除所有容器应用 docker ps -a | awk '{print $1}' | xargs docker rm # 或者 docker rm $(docker ps -a -q)

参考:

1、Overview of Docker Compose - Docker

https://docs.docker.com/compose/overview/

2、library/mysql - Docker Hub

https://hub.docker.com/_/mysql/

3、library/wordpress - Docker Hub

https://hub.docker.com/_/wordpress/

Docker Compose 使用示例的更多相关文章

  1. Docker学习笔记之常用的 Docker Compose 配置项

    0x00 概述 与 Dockerfile 一样,编写 Docker Compose 的配置文件是掌握和使用好 Docker Compose 的前提.编写 Docker Compose 配置文件,其本质 ...

  2. 一文掌握Docker Compose

    目录 Docker Compose介绍 Docker Compose安装 Docker Compose基本示例 1.基本文件及目录设置 2.创建一个Dockerfile 3.通过docker-comp ...

  3. .NET遇上Docker - 使用Docker Compose组织Ngnix和.NETCore运行

    本文工具准备: Docker for Windows Visual Studio 2015 与 Visual Studio Tools for Docker 或 Visual Studio 2017 ...

  4. Docker(四):Docker 三剑客之 Docker Compose

    前两篇文章我们介绍了 Dockerfile 的使用Docker(二):Dockerfile 使用介绍,我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就 ...

  5. Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的便利,是的,我也是这样认为的,I know your felling . 前期了解概念什么的确实 ...

  6. Docker 核心技术之Docker Compose

    Docker Compose 简介 Docker Compose是什么? Docker Compose是一个能一次性定义和管理多个Docker容器的工具. 详细地说: Compose中定义和启动的每一 ...

  7. Docker Compose 原理

    Docker 的优势非常明显,尤其是对于开发者来说,它提供了一种全新的软件发布机制.也就是说使用 docker 镜像作为软件产品的载体,使用 docker 容器提供独立的软件运行上下文环境,使用 do ...

  8. Docker Compose 简介

    Compose 是 docker 提供的一个命令行工具,用来定义和运行由多个容器组成的应用.使用 compose,我们可以通过 YAML 文件声明式的定义应用程序的各个服务,并由单个命令完成应用的创建 ...

  9. Docker系列10—容器编排工具Docker Compose详解

    本文收录在容器技术学习系列文章总目录 1.Docker Compose 概述 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用Compose文件来配置 ...

随机推荐

  1. C#中怎样在ToolStripMenuItem下再添加子级菜单

    场景 在右键菜单ContextMenuStrip下添加子菜单选项可以通过 ContextMenuStrip menuStrip ToolStripMenuItem mnuChartOption = n ...

  2. xlrd模块-读取Execl表格

    #xlrd模块 读取execl表格 import xlrd Execl = xlrd.open_workbook(r'Z:\Python学习\python26期视频\day76(allure参数.读e ...

  3. npm 基础命令

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npm 从5.2版开始,增加了 ...

  4. Oracle库基本操作

    --oracle 获取表名称,字段 with vA as ( SELECT USER_TAB_COLS.TABLE_NAME as 表名,USER_TAB_COLS.COLUMN_NAME as 列名 ...

  5. Python目录结构规范

    在设计大型项目时需要规范目录结构. 假设你的项目名为foo, 我比较建议的最方便快捷目录结构这样就足够了: Foo/ |-- bin/ | |-- foo | |-- foo/ | |-- tests ...

  6. [BZOJ4310] 跳蚤 - 后缀数组,二分,ST表

    [BZOJ4310] 跳蚤 Description 首先,他会把串分成不超过 \(k\) 个子串,然后对于每个子串 \(S\) ,他会从 \(S\) 的所有子串中选择字典序最大的那一个,并在选出来的 ...

  7. OpenGL 3D拾取文章(转)

    参考文章 深入探索3D拾取技术 OpenGL 3D拾取 射线和三角形的相交检测(ray triangle intersection test) 3D拾取的方法有两种 1.基于几何计算的射线-三角形相交 ...

  8. 创建本地yum源仓库

    更新本地yum源 yum仓库服务端配置如下 : 1. 创建yum仓库目录 mkdir -p /data/yum_data/ cd /data/yum_data/ #可以上传rpm包到此目录,此目录下面 ...

  9. .net_DevExpress控件使用经验总结

    (转)DevExpress控件使用经验总结DevExpress是一个比较有名的界面控件套件,提供了一系列的界面控件套件的DotNet界面控件.本文主要介绍我在使用DevExpress控件过程中,遇到或 ...

  10. 1015 Reversible Primes

    1. 题目 2. 抽象建模 无 3. 方法 无 4. 注意点 素数判断(1不是素数) 数值的倒转 5. 代码 #include<stdio.h> #include<math.h> ...