无法从CodeBuild连接到Docker容器?

问题描述

我已通过设置PrivilegedMode: true代码构建配置为运行docker。我有docker compose文件,例如:

version: "3.3"

services:
  postgresql:
    image: postgres:11
    ports: 
      - '5432:5432'
    command: -c ssl=on -c ssl_cert_file=/workdir/tests/cert/server.crt -c ssl_key_file=/workdir/tests/cert/server.key
    volumes: 
      - ./:/workdir
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: user
      POSTGRES_DB: test

  migration:
    image: flyway/flyway
    environment:
      DB_HOST: postgresql
      STAGE: dev
      SCHEMA_LOCATION: filesystem:/workdir/dbschema
    depends_on:
      - postgresql
    volumes:
      - ".:/workdir"
    entrypoint: /workdir/bin/flyway.sh
    links:
      - postgresql
  

我有两个容器postgresqlflywayFlyway是执行数据库架构迁移的工具,基本上它所做的就是连接到postgresql并应用sql语句。在flyway.sh中,我有以下脚本代码

#!/bin/sh

sleep 20 

echo "Run flyway to migrate db schema"
flyway migrate -url=jdbc:postgresql://$HOST/$dbnAME -user=$USER_NAME -locations=$SCHEMA_LOCATION -password=$PASSWORD -schemas=$STAGE

它睡眠20秒以等待postgresql可用。但是在codebuild中运行时出现此错误

migration_1   | Run flyway to migrate db schema
migration_1   | Flyway Community Edition 7.0.4 by Redgate
migration_1   | ERROR: 
migration_1   | Unable to obtain connection from database (jdbc:postgresql://postgresql/test) for user 'user': The connection attempt Failed.

似乎无法连接到数据库实例。我还尝试通过postgresql从codebuild容器连接到127.0.0.1:5432,但也失败了。问题似乎是codebuild不能使网络可用于这些容器。我有办法解决这个问题吗?

从本地(MacOS)运行它们对我来说没有问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)