AWS ElasticBeanstalk多容器Rails / Nginx / Postgres:无效的端口号:“ tcp://172.17.0.2:5432”

时间:2018-08-05 00:54:41

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

我正在尝试在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": {}
        }
    ]

1 个答案:

答案 0 :(得分:0)

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

因此您的数据库YAML看起来更像: ... production: <<: *default database: <%= ENV["DB_NAME"] %> host: db port: 5432 ...