我知道使用较旧版本的docker-compose,我们可以创建另一个只包含数据卷的容器,并使用volumes_from链接它以使其成为“仅数据容器”.但是,我想测试使用新语法.
这是基于@L_301_0@.
我有另一个脚本运行,等待这个postgres容器在其他容器运行之前启动,例如:
- container:
- build: .
- volumes:
- - ./scripts/wait-for-postgres.sh:/code/wait-for-postgres.sh
- entrypoint: ./wait-for-postgres.sh "command"
脚本看起来像:
- #!/bin/bash
- set -e
- export PGPASSWORD=postgres
- cmd="$@"
- until psql -h "postgres" -U "postgres" -c '\l'; do
- >&2 echo "Postgres is unavailable - sleeping"
- sleep 1
- done
- >&2 echo "Postgres is up - executing command"
- exec $cmd
这是从码头网站上获取的.
这只会导致容器停滞而根本不会出现,我甚至无法使用我需要的表来初始化postgres容器.
运行检查脚本不需要版本2,因为postgres一旦启动就会开始监听,您可以使用depends_on来定义依赖关系.以下是我在postgres上设置postgres,volume和server(glassfish)的方法:
- version: '2'
- services:
- my-app:
- image: my-glassfish-image
- depends_on:
- - my-db
- my-db:
- image: my-postgres-image
- volumes:
- - postgres-db-volume:/data/postgres
- volumes:
- postgres-db-volume: