在 docker - 部署一个复杂的springboot服务 该文中,咱们部署了一个“复杂”的springboot服务,实现了一个计数服务。经过此次部署操做,咱们了解到部署多容器的APP至少须要通过如下几个步骤:redis
(1)写Dockerfile构建镜像或者从docker registry中拉取镜像spring
(2)构建多个容器docker
(3)管理这些容器(启动、中止等)springboot
可见,若是APP涉及到的容器不少,要管理这些容器是比较复杂的,至少命令行敲到想哭。那么,有没有一个工具,帮咱们批处理这些容器呢?bash
docker-compose是docker公司推出的一个服务编排工具,换句话说就是一个批处理容器的工具。该工具能够经过yml文件定义多容器的应用,并建立和管理这些容器。网络
在文章 安装 docker-compose 中,已经详细介绍了compose的几种安装方法,能够参考安装。app
docker-compose.yml是compose的默认的脚本名字,在执行compose命令构建的时候,若是不指定文件名,将会默认使用docker-compose.yml文件,和Dockerfile文件相似。socket
docker-compose.yml是有版本的,如今最新版本是v3版本,v1版本不推荐使用,v2和v3是可兼容的,并且区别不是很大。这里须要提到一点区别,v2只能用于多个容器部署在一个宿主主机,而v3能够集群方式部署在多个宿主主机(swarm)。另外,不一样的compose文件版本对docker的版本是有要求的。若是有兴趣,能够在docker的官网中详细了解,版本间的区别连接的地址。咱们采用官方推荐的v3版本学习docker-compose。tcp
在compose中,主要有四个基本节点: version、services、networks、volumes。是否是很熟悉,其实compose就是使用脚本方式代替咱们以前经过命令行启动每一个服务。下面是一个基本的compose脚本。工具
version: '3' services: springboot-docker: image: myimage:1.0 build: context: . dockerfile: Dockerfile ports: - 8080:8080 networks: - mybridge volumes: - mydata:/app networks: mybridge: driver: bridge volumes: mydata:
本文将经过一个简单的 docker-compose 文件,熟悉经常使用的docker-compose命令。docker-compose.yml文件存放在dockercompose文件夹中。
version: '3' services: busybox: image: busybox restart: always command: /bin/echo redis: image: redis:4.0.11 ports: - 6379:6379 volumes: - /data/redis:/data restart: always command: redis-server
咱们查看 docker-compose 有多少种命令,有个大概的了解。docker-compose命令便可查看。
[root@localhost dockercompose]# 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 --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
这里的选项中有两个是经常使用的:-f 和 -p
-f :指定文件。在运行up命令启动容器时,若是没有指定docker-compose.yml文件,将会读取当前目录下的docker-compose.yml文件
-p:启动后的服务的名字默认状况下采用compose.yml文件所在的目录+服务名+副本数命名,例如上面的例子的名字为: dockercompose_redis_1 。若是指定-p,则会用项目名代替该文件目录名。
下面开始介绍一些经常使用的docker-compose命令。
docker-compose create | 建立全部的服务 |
docker-compose start | 启动被中止或未启动的服务 |
docker-compose up | 建立全部服务而且启动服务,即同时执行了create和start命令 |
docker-compose stop | 中止全部服务 |
docker-compose kill | 强行中止全部服务 |
docker-compose rm | 删除中止的服务 |
docker-compose restart | 重启全部服务) |
docker-compose down | 中止、删除全部的服务以及网络、镜像 |
其中,up命令启动全部的服务时,若是没有使用-d命令,会在前台启动全部的服务,前台窗口将打印服务的启动日志。建议经过 -d 启动。例如经过up启动上面的redis服务。
[root@localhost dockercompose]# docker-compose up -d Creating network "dockercompose_default" with the default driver
而后能够经过 docker-compose logs -f 获取输出的日志
该命令能够查看容器列表,能够获取到容器的简单信息。
[root@localhost dockercompose]# docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------- dockercompose_busybox_1 /bin/echo Up dockercompose_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
上面的信息能够获得容器名、启动命令、容器状态以及端口的映射状况。
该命令用于扩展容器,为service设置多个容器(副本)。咱们为上面的busybox服务扩展为2个副本。
[root@localhost dockercompose]# docker-compose scale busybox=2 WARNING: The scale command is deprecated. Use the up command with the --scale flag instead. Starting dockercompose_busybox_1 ... done Creating dockercompose_busybox_2 ... done
稍等一下,经过ps命令能够看到已经启动了两个busybox服务。
前面的输出中,提示该命令已通过期,使用--scale选项指定副本。
咱们down了服务后,从新使用up命令并指定副本数
[root@localhost dockercompose]# docker-compose up -d --scale busybox=2
[root@localhost dockercompose]# docker-compose ps Name Command State Ports ---------------------------------------------------------------------------------------------- dockercompose_busybox_1 /bin/echo Restarting dockercompose_busybox_2 /bin/echo Restarting dockercompose_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
不明白为何一直都是restarting。。。。。忽略它吧。。。。通常不会在一个宿主部署两个一样的服务