当前正在构建一个程序包以使用AWS测试一些devOps配置.使用Swift Vapor3,Postgresql 11,Docker构建应用程序.给定我的github Repo,假设您已本地安装了postgresql,并使用了用户名:test,密码:test,则该项目的构建/测试/运行与vapor build vapor test很好.
version: "3.5"
services:
api:
container_name: vapor_it_container
build:
context: .
dockerfile: web.Dockerfile
image: api:dev
networks:
- vapor-it
environment:
POSTGRES_PASSWORD: 'test'
POSTGRES_DB: 'test'
POSTGRES_USER: 'test'
POSTGRES_HOST: db
POSTGRES_PORT: 5432
ports:
- 8080:8080
volumes:
- .:/app
working_dir: /app
stdin_open: true
tty: true
entrypoint: bash
restart: always
depends_on:
- db
db:
container_name: postgres_container
image: postgres:11.2-alpine
restart: unless-stopped
networks:
- vapor-it
ports:
- 5432:5432
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_HOST: db
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data
volumes:
- database_data:/var/lib/postgresql/data
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: test@test.com
PGADMIN_DEFAULT_PASSWORD: admin
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- vapor-it
restart: unless-stopped
networks:
vapor-it:
driver: bridge
volumes:
database_data:
pgadmin:
# driver: local
另外,在阅读Docker postgres文档时,我在“注意事项”部分遇到了这一点.
If there is no database when postgres starts in a container,then postgres will create the default database for you. While this is the expected behavior of postgres,this means that it will not accept incoming connections during that time. This may cause issues when using automation tools,such as docker-compose,that start several containers simultaneously.07001
我尚未进行这些更改,因为我不确定如何制作该文件或配置的外观.有没有人做过这样的事情,并且在连接Postgresql和使用蒸气作为后端方面有一定的经验?
不过,这只是理论,并不总是适用.在您的情况下,也许您确实只需要Vapor应用程序来等待Postgres可用,在这种情况下,您可以使用包装脚本来轮询数据库,并仅在数据库准备好后才启动主应用程序.
See this suggested wrapper script from the Docker docs:
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$@"
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
command: ["./wait-for-postgres.sh","db","vapor-app","run"]