赞
踩
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
官方安装方法(https://docs.docker.com/compose/install/)有时会非常缓慢,暂时不清楚啥原因
下载指定版本的docker-compose,注意以下命令的版本号请根据实际情况修改,截止目前为止最新的是1.24.0
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
授予可执行权限
sudo chmod +x /usr/local/bin/docker-compose
以上两行基本就算把docker-compose安装好了
可以查询版本号看看是否安装成功
docker-compose --version
如果发现没有docker-compose命令,检查下docker-compose路径,或者可以在其他目录(比如/usr/bin/docker-compose)创建一个链接指向docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
假如你条件受限无法科学上网,那么只能通过其他路径下载安装docker-compose
安装命令如下(ubuntu)
apt-get update
apt-get install -y docker-compose
可以查询版本号看看是否安装成功
docker-compose --version
假如你是通过官方安装,即通过curl下载安装的,那么卸载则直接移除目录即可
sudo rm /usr/local/bin/docker-compose
如果是通过其他工具安装,请参照各工具的卸载方法,例如是用pip安装的,则用以下命令卸载
pip uninstall docker-compose
如果是apt,则用以下命令卸载
apt-get --purge remove docker-compose
version: "3" #指定版本,不同版本支持的指令不同 services: webapp: # 容器名称,此名称可以用在docker-compose指令上 build: ./dir #构建路径,即dockerfile文件所在目录 depends_on: # 依赖的服务,这个决定了启动顺序,但只是保证了容器的启动顺序,并不保证容器内服务的启动顺序 - mysql-server container_name: name # 容器名称,相当于run指令的--name network_mode: host #网络模式,相当于run指令的--net restart: always # 重启模式,相当于run指令的--restart image: hello-world # 使用的镜像名称,相当于hello-world:latest external_links: #相当于run指令的--link ,不过external_link需要双方都是桥接模式bridge - external-mysql links: #与external_links不同的是这是内部,即在这个docker-compose声明的容器 - in-mysql ports: - "3306:3306" # 端口映射,相当于run指令的-p volumes: - /abc/d:/abc/d # 相当于run指令的-v environment: # 相当于run指令的-e - TZ=Asia/Shanghai #这里设置容器的时区为亚洲上海,也就解决了容器通过compose编排启动的时区问题!!!!解决了容器的时区问题!!! - ENV_1=param1 - ENV_2=param2 command: cmd #启动时执行的命令 mem_limit: 400M #限制容器使用内存,相当于run --memory memswap_limit: 1G #限制swap大小,相当于run --memory-swap deploy: replicas: 6 #服务实例个数 mysql: container_name: in-mysql image: mysql
我们的docker-compose.yml肯定不是一层不变的,有时会有一些参数的变化
默认支持的是docker-compose.override.yml,也是就说当你在执行 docker-compose up
时,docker-compose.override.yml的内容会覆盖到docker-compose.yml
当然如果你要覆盖的文件名不是docker-compose.override.yml,那么就要在up时指定,比如为docker-compose.prod.yml
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
当你应用程序的代码发生变化时,应当重新构建镜像和运行容器,比如重新部署一个叫web的服务
docker-compose build web
docker-compose up --no-deps -d web
–no deps标志防止compose同时重新创建Web依赖的任何服务
很遗憾,docker-compose本身并没有对服务启动顺序的支持,但很多时候我们的服务之间是需要启动顺序的
对此,docker官方文档中有提到三个工具,原文在这里 https://docs.docker.com/compose/startup-order/
此处以wait-for-it为例, war-for-it的用法很简单,就是把wait-for-it.sh下载下来,赋予可执行权限
详细用法如下
wait-for-it.sh host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
例如
guest@ubuntu16-cn:~/hwj/test$ ./wait-for-it.sh www.baidu.com:80 -- echo "hello"
wait-for-it.sh: waiting 15 seconds for www.baidu.com:80
wait-for-it.sh: www.baidu.com:80 is available after 0 seconds
hello
docker-compose.yml中假如需要等待db启动后才启动web,则如下示例
version: "2"
services:
web:
build: .
ports:
- "80:8000"
depends_on:
- "db"
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
db:
image: postgres
以下就是从github摘抄过来的 wait-for-it.sh
#!/usr/bin/env bash # Use this script to test if a given TCP host/port are available WAITFORIT_cmdname=${0##*/} echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } usage() { cat << USAGE >&2 Usage: $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] -h HOST | --host=HOST Host or IP under test -p PORT | --port=PORT TCP port under test Alternatively, you specify the host and port as host:port -s | --strict Only execute subcommand if the test succeeds -q | --quiet Don't output any status messages -t TIMEOUT | --timeout=TIMEOUT Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes USAGE exit 1 } wait_for() { if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" else echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" fi WAITFORIT_start_ts=$(date +%s) while : do if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then nc -z $WAITFORIT_HOST $WAITFORIT_PORT WAITFORIT_result=$? else (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 WAITFORIT_result=$? fi if [[ $WAITFORIT_result -eq 0 ]]; then WAITFORIT_end_ts=$(date +%s) echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" break fi sleep 1 done return $WAITFORIT_result } wait_for_wrapper() { # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 if [[ $WAITFORIT_QUIET -eq 1 ]]; then timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & else timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & fi WAITFORIT_PID=$! trap "kill -INT -$WAITFORIT_PID" INT wait $WAITFORIT_PID WAITFORIT_RESULT=$? if [[ $WAITFORIT_RESULT -ne 0 ]]; then echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" fi return $WAITFORIT_RESULT } # process arguments while [[ $# -gt 0 ]] do case "$1" in *:* ) WAITFORIT_hostport=(${1//:/ }) WAITFORIT_HOST=${WAITFORIT_hostport[0]} WAITFORIT_PORT=${WAITFORIT_hostport[1]} shift 1 ;; --child) WAITFORIT_CHILD=1 shift 1 ;; -q | --quiet) WAITFORIT_QUIET=1 shift 1 ;; -s | --strict) WAITFORIT_STRICT=1 shift 1 ;; -h) WAITFORIT_HOST="$2" if [[ $WAITFORIT_HOST == "" ]]; then break; fi shift 2 ;; --host=*) WAITFORIT_HOST="${1#*=}" shift 1 ;; -p) WAITFORIT_PORT="$2" if [[ $WAITFORIT_PORT == "" ]]; then break; fi shift 2 ;; --port=*) WAITFORIT_PORT="${1#*=}" shift 1 ;; -t) WAITFORIT_TIMEOUT="$2" if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi shift 2 ;; --timeout=*) WAITFORIT_TIMEOUT="${1#*=}" shift 1 ;; --) shift WAITFORIT_CLI=("$@") break ;; --help) usage ;; *) echoerr "Unknown argument: $1" usage ;; esac done if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then echoerr "Error: you need to provide a host and port to test." usage fi WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} # check to see if timeout is from busybox? WAITFORIT_TIMEOUT_PATH=$(type -p timeout) WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then WAITFORIT_ISBUSY=1 WAITFORIT_BUSYTIMEFLAG="-t" else WAITFORIT_ISBUSY=0 WAITFORIT_BUSYTIMEFLAG="" fi if [[ $WAITFORIT_CHILD -gt 0 ]]; then wait_for WAITFORIT_RESULT=$? exit $WAITFORIT_RESULT else if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then wait_for_wrapper WAITFORIT_RESULT=$? else wait_for WAITFORIT_RESULT=$? fi fi if [[ $WAITFORIT_CLI != "" ]]; then if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" exit $WAITFORIT_RESULT fi exec "${WAITFORIT_CLI[@]}" else exit $WAITFORIT_RESULT fi
vishnubob/wait-for-it: Pure bash script to test and wait on the availability of a TCP host and port
https://github.com/vishnubob/wait-for-it
docker-compose --help
获得指令帮助说明doceker-compose -f customname.yml up # 自定义yml文件名,默认都是docker-compose.yml名称
doceker-compose up -d # 表示后台启动
docker-compose restart # 重启
docker-compose stop #停止服务
docker-compose down # 停止服务并删除镜像
docker-compose down --volumes # 停止服务,删除镜像并删除数据卷
docker-compose ps # 查看当前正在跑的服务
(1条消息)我的docker随笔5:docker-compose的安装与使用 - 李迟的专栏 - CSDN博客
https://blog.csdn.net/subfate/article/details/80905985
Compose file version 3 reference | Docker Documentation
https://docs.docker.com/compose/compose-file/
docker-compose命令 - 坚强的小蚂蚁 - 博客园
https://www.cnblogs.com/regit/p/8309959.html
docker compose 服务启动顺序控制 - wang_yb - 博客园
https://www.cnblogs.com/wang_yb/p/9400291.html
docker-compose命令 - 坚强的小蚂蚁 - 博客园
https://www.cnblogs.com/regit/p/8309959.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。