连接到DynamoDB Docker Container

时间:2018-05-15 13:41:47

标签: php amazon-web-services docker amazon-dynamodb

我正在学习PHP并试图在dynamodb docker容器上创建一个表。我的docker compose中有3个容器,web(nginx),带有AWS SDK的php(7.1-fpm)和dynamodb。 (下面的Dockerfiles)。

我收到以下错误:

Unable to create table: Error executing "CreateTable" on "http://dynamodb:8000"; AWS HTTP error: cURL error 6: Could not resolve host: dynamodb (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

我有以下docker-composer.yml

web:
  image: nginx:latest
  ports:
    - "8080:80"
    - "8000:8000" // as you can see i tried to open the port on web if that was the problem
  volumes:
    - ./code:/code
    - ./site.conf:/etc/nginx/conf.d/site.conf
  links:
    - php
    - dynamodb:db // i also tried to access it by dynamodb or db
php:
  build: php/
  volumes:
    - ./code:/code
dynamodb:
  build: dynamodb/
  expose:
    - 8000 // I think this is redundant I also have expose in the Dockerfile

DynamoDb Dockerfile

FROM openjdk:7

MAINTAINER <redacted>

RUN mkdir -p opt/dynamodb
WORKDIR /opt/dynamodb

RUN wget http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz -q -O - | tar -xz

ENV AWS_ACCESS_KEY_ID MINE
ENV AWS_SECRET_ACCESS_KEY MINE TOO

EXPOSE 8000

ENTRYPOINT ["java", "-jar", "DynamoDBLocal.jar", "-sharedDb"]

PHP Dockerfile:

FROM php:7.1-fpm

RUN apt-get update && \
    apt-get -y install git libz-dev libtidy-dev && \
    docker-php-ext-install tidy && \
    docker-php-ext-install zip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# PHP Configuration required for PHP SDK
RUN touch /usr/local/etc/php/conf.d/memory.ini \
    && echo "memory_limit = 2048M;" >> /usr/local/etc/php/conf.d/memory.ini

RUN touch /usr/local/etc/php/conf.d/phar.ini \
    && echo "phar.readonly = Off;" >> /usr/local/etc/php/conf.d/phar.ini

RUN touch /usr/local/etc/php/conf.d/timezone.ini \
    && echo "date.timezone ='America/Los_Angeles'" >> /usr/local/etc/php/conf.d/timezone.ini

RUN cd / && \
    git clone https://github.com/aws/aws-sdk-php.git

ENV AWS_ACCESS_KEY_ID MINE
ENV AWS_SECRET_ACCESS_KEY MINE TOO


RUN cd /aws-sdk-php && \
    composer install && \
    make build && \
    make test

index.php

<?php
require("/aws-sdk-php/vendor/autoload.php");
use Aws\DynamoDb\Exception\DynamoDbException;

$sdk = new Aws\Sdk([
        'endpoint'  => 'http://localhost:8000', // i have tried localhost, db, and dynamodb here
        'region'    => 'us-east-2',
        'version'   => 'latest',
        'credentials' => [
                'key' => 'mine',
                'secret' => 'mine too'
        ]

]);

$dynamodb = $sdk->createDynamoDb();

$params = [
        'TableName'  => 'Movies',
        'KeySchema'  => [
          [
                'AttributeName' => 'year',
                'KeyType'       => 'HASH'
          ],
          [
                'AttributeName' => 'title',
                'KeyType'       => 'RANGE'
          ]
        ],
        'AttributeDefinitions'  => [
          [
                'AttributeName' => 'year',
                'AttributeType' => 'N'
          ],
          [
                'AttributeName' => 'title',
                'AttributeType' => 'S'
          ],
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits' => 10,
            'WriteCapacityUnits' => 10
        ]
];

try {
        $result = $dynamodb->createTable($params);
        echo 'Created table. Status: ' .
                $result['TableDescription']['TableStatus'] ."\n";
} catch (DynamoDbException $e) {
        echo "Unable to create table: \n";
        echo $e->getMessage() . "\n";
}
?>

我是Docker,PHP和AWS的新手,所以如果我错过了任何必需的信息,请告诉我。

我尝试了什么?

我尝试过使用localhost而不是dynamodb(还尝试在web容器中使用带有dynamodb:db链接的db)

我必须尝试在web 8000:8000上映射端口,我对任何建议持开放态度。

2 个答案:

答案 0 :(得分:0)

对于任何可能遇到与我类似问题的人:

我需要将dynamodb容器链接到php容器,因为php容器正在运行代码而不是web容器。

这个docker-compose文件让我排序:

{{1}}

答案 1 :(得分:0)

使用ENTRYPOINT代替RUN

# Dockerfile
COPY ./run.sh /run.sh

ENTRYPOINT ["chmod", "+x", "/run.sh"]
#!/bin/sh

cd /aws-sdk-php
composer install # this line can put on Dockerfile too.
make build
make test