【问题标题】:AWS ElasticBeanstalk Multi-Container Rails/Nginx/Postgres: Invalid port number: "tcp://172.17.0.2:5432"AWS ElasticBeanstalk 多容器 Rails/Nginx/Postgres:端口号无效:“tcp://172.17.0.2:5432”
【发布时间】:2019-01-12 09:51:17
【问题描述】:

我正在尝试在 Amazon ElasticBeanstalk 上部署一个多容器 docker rails/postgres/nginx 应用程序(我已尝试将 postgres 容器移动到 Amazon RDS,但相关的安全组没有被公开 - 导致另一个帖子) .每当我尝试为我的应用程序访问环境的 URL 时,我都会得到:

端口号无效:“tcp://172.17.0.2:5432”

我相信我正在遵循构建、标记和推送图像、压缩源包、正确配置 Dockerrun.aws.json 以及更新应用程序并将其部署到环境的所有步骤。我希望我已经在下面包含了所有相关文件和跟踪信息。任何见解将不胜感激。谢谢。

Dockerrun.aws.json:

{
  "AWSEBDockerrunVersion": 2,
"containerDefinitions": [
    {
        "name": "app",
                    "image": "830894003218.dkr.ecr.us-east-1.amazonaws.com/fusionrepo:app",                         
        "essential": true,
        "mountPoints": [
            {
                "containerPath": "/app",
                "sourceVolume": "_"
            }
        ],
        "portMappings": [
            {
                "containerPort": 3000,
                "hostPort": 3000
            }
        ],
        "workingDirectory": "/app",
                    "links": ["db"],
              "memory": 128                     
    },
    {
        "name": "db",
        "essential": true,
        "image": "postgres:10.3-alpine",
        "mountPoints": [
            {
                "containerPath": "/var/lib/postgresql/data",
                "sourceVolume": "postgres"
            }
        ],
        "portMappings": [
            {
                "containerPort": 5432,
                "hostPort": 5432
            }
        ],                      
              "memory": 128                     
    },
    {
        "name": "web",
        "essential": true,
              "memory": 128,
                    "image": "830894003218.dkr.ecr.us-east-1.amazonaws.com/fusionrepo:web",                     
        "mountPoints": [
            {
                "containerPath": "/var/log/containers/nginx",
                "sourceVolume": "_"
            }
        ],
        "portMappings": [
            {
                "containerPort": 80,
                "hostPort": 80
            }
        ],
        "workingDirectory": "/app",
                    "links": ["app"],
              "memory": 128                     
    }
],
"family": "",
"volumes": [
    {
        "host": {
            "sourcePath": "/var/app/current"
        },
        "name": "_"
    },
    {
        "host": {
            "sourcePath": "postgres"
        },
        "name": "postgres"
    }
]

}

docker-compose.yml

    version: '3'
    volumes:
      postgres: {}
    services:
      app:
        build:
          context: .
          dockerfile: ./.docker/app/Dockerfile
        depends_on:
          - db
        env_file:
          - '.env'
        ports:
          - '3000:3000'
        volumes:
          - ./:/app
        working_dir: /app
      web:
        build:
          context: .
          dockerfile: ./.docker/web/Dockerfile
        depends_on:
          - app
        ports:
          - 80:80
        volumes:
          - ./:/app
        working_dir: /app
      db:
        image: 'postgres:10.3-alpine'
        volumes:
          - 'postgres:/var/lib/postgresql/data'
        env_file:
          - '.env'

config/database.yml

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see Rails configuration guide
      # http://guides.rubyonrails.org/configuring.html#database-pooling
      host: <%= ENV["DB_HOST"] %>
      username: <%= ENV["DB_USERNAME"] %>
      password: <%= ENV["DB_PASSWORD"] %>
      port: <%= ENV.fetch("DB_PORT") { 5432 } %>
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    development:
      <<: *default
      database: fusion_development
    test:
      <<: *default
      database: fusion_test
    production:
      <<: *default
      database: <%= ENV["DB_NAME"] %>
      host: <%= ENV["DB_HOST"] %>
      port: <%= ENV["DB_PORT"] %>
      username: <%= ENV["PRODUCTION_DB_USERNAME"] %>
      password: <%= ENV["PRODUCTION_DB_PASSWORD"] %>
      # host: <%= ENV["RDS_HOSTNAME"] %>
      # port: <%= ENV["RDS_PORT"] %>
      # username: <%= ENV["RDS_USERNAME"] %>
      # password: <%= ENV["RDS_PASSWORD"] %>
      # host: <%= ENV["PRODUCTION_DB_HOST"] %>
      # username: <%= ENV["PRODUCTION_DB_USERNAME"] %>
      # password: <%= ENV['PRODUCTION_DB_PASSWORD'] %>

码头检查

    sudo docker network inspect 2ab590882560
    [
        {
            "Name": "bridge",
            "Id": "2ab59088256063f635211f511f9cd6377a9963d832390e3ed3bfebc68f17679a",
            "Created": "2018-07-31T02:07:23.005934521Z",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.17.0.0/16",
                        "Gateway": "172.17.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {
                "103191687fa846d4cca480ca8f367328a2e7c23a75fe995318fa8f3b7ce5b7c4": {
                    "Name": "ecs-awseb-Fusion-Production-env-gvqsmaunxx-31-app-8eabdd9ca78198e71c00",
                    "EndpointID": "2b4292f1506a3aa403fd32b35b09d09a65137849a7beabc679289eac1c982fd3",
                    "MacAddress": "02:42:ac:11:00:03",
                    "IPv4Address": "172.17.0.3/16",
                    "IPv6Address": ""
                },
                "41477049948bb60ffcdec4ba13af7fcf83d81d1cfe4f3cfc471614acb7cb77de": {
                    "Name": "ecs-awseb-Fusion-Production-env-gvqsmaunxx-31-db-d682cfb7f3de89e69601",
                    "EndpointID": "72c68e7a5865f42adbabea98a65c1945d415fd5892baaed1d73b6ad34b8f8726",
                    "MacAddress": "02:42:ac:11:00:02",
                    "IPv4Address": "172.17.0.2/16",
                    "IPv6Address": ""
                },
                "f6924fab885c162fd21314df2779b55cbd26e67248283773b62d561f26c16566": {
                    "Name": "ecs-awseb-Fusion-Production-env-gvqsmaunxx-31-web-aea3eee3f891a8b63100",
                    "EndpointID": "39a9915179b956bc182c1e0be42d8299255493c0738a154df8b389362d7cc320",
                    "MacAddress": "02:42:ac:11:00:04",
                    "IPv4Address": "172.17.0.4/16",
                    "IPv6Address": ""
                }
            },
            "Options": {
                "com.docker.network.bridge.default_bridge": "true",
                "com.docker.network.bridge.enable_icc": "true",
                "com.docker.network.bridge.enable_ip_masquerade": "true",
                "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
                "com.docker.network.bridge.name": "docker0",
                "com.docker.network.driver.mtu": "1500"
            },
            "Labels": {}
        }
    ]

【问题讨论】:

    标签: ruby-on-rails postgresql amazon-web-services docker


    【解决方案1】:

    我的第一个建议是使用普通的 docker-compose 逻辑,然后不需要配置 DB_HOST,因为“app”容器将使用“db”自动连接到“db”容器的链接主机名。

    所以你的 db YAML 看起来更像: ... production: <<: *default database: <%= ENV["DB_NAME"] %> host: db port: 5432 ...

    【讨论】:

    • 感谢您的建议。我试了一下,至少现在我得到了一个不同的错误:could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? - 我们怀疑这与 postgres 配置本身有关,其中 listen_addresses = '*'" 可能未设置。
    • 好吧,我又试了一次,仍然得到无效的端口号:tcp://172.17.0.2:5432,即使我能够在应用程序容器中启动一个 shell 并发出一个 netcat nc -v 172.17.0.2 5432 和得到这个端口确实是开放的答案:172.17.0.2 (172.17.0.2:5432) open。我检查了more /var/lib/postgresql/data/postgresql.conf 中的数据库容器配置并设置了连接设置listen_addresses = '*'。所有相关端口在实例和容器中都是开放的。
    • netconnect "nc " 命令只会在 EC2 中运行,不会在 docker 中运行。它需要通过 docker 容器连接。尝试将 DB_HOST 添加到 Configuration->Software env 变量中。这似乎对我有用。
    猜你喜欢
    • 2017-08-16
    • 2014-12-20
    • 2016-12-06
    • 2016-06-26
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多