简体   繁体   English

连接到 DynamoDB 时出错 Docker 容器

[英]Error connecting to DynamoDB Docker Container

I am learning PHP and trying to create a table on a dynamodb docker container.我正在学习 PHP 并尝试在 dynamodb docker 容器上创建一个表。 I have 3 containers in my docker compose, web (nginx), php(7.1-fpm) with AWS SDK, and dynamodb.我的 docker compose、web (nginx)、php(7.1-fpm) with AWS SDK 和 dynamodb 中有 3 个容器。 (Dockerfiles below). (下面的 Dockerfile)。

I get the following error:我收到以下错误:

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)

I have the following docker-composer.yml我有以下 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 : 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";
}
?>

I am new to Docker, PHP, and AWS so if I missed any required information let me know.我是 Docker、PHP 和 AWS 的新手,所以如果我错过了任何必需的信息,请告诉我。

What have I tried?我尝试了什么?

I have tried using localhost instead of dynamodb(have also tried using db with a dynamodb:db link in the web container)我尝试使用 localhost 而不是 dynamodb(也尝试在 web 容器中使用 db 和 dynamodb:db 链接)

I have to tried to map the port on web 8000:8000, I am open to any suggestions.我必须尝试 map web 8000:8000 上的端口,我愿意接受任何建议。

For anyone who may run into a similar problem as me:对于可能遇到与我类似问题的任何人:

I needed to link the dynamodb container to the php container, because the php container is running the code not the web container.我需要将 dynamodb 容器链接到 php 容器,因为 php 容器正在运行代码而不是 Web 容器。

This docker-compose file got me sorted:这个 docker-compose 文件让我排序:

web:
  image: nginx:latest
  ports:
    - "8080:80"
    - "8000:8000"
  volumes:
    - ./code:/code
    - ./site.conf:/etc/nginx/conf.d/site.conf
  links:
    - php
    - dynamodb:db // <-- Probably unnecessary, requires more testing
php:
  build: php/
  volumes:
    - ./code:/code
  links:
    - dynamodb // <-- Important
dynamodb:
  build: dynamodb/
  expose:
    - 8000

use ENTRYPOINT instead of RUN使用 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

The link feature is legacy now as per the documentation:根据文档,链接功能现在是旧版:

The --link flag is a legacy feature of Docker. It may eventually be removed. --link 标志是 Docker 的遗留功能。它最终可能会被删除。 Unless you absolutely need to continue using it, we recommend that you use user-defined.networks to facilitate communication between two containers instead of using --link.除非您绝对需要继续使用它,否则我们建议您使用 user-defined.networks 来促进两个容器之间的通信,而不是使用 --link。 One feature that user-defined.networks do not support that you can do with --link is sharing environment variables between containers. user-defined.networks 不支持的一项功能,您可以使用 --link 来实现,即在容器之间共享环境变量。 However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.但是,您可以使用其他机制(例如卷)以更可控的方式在容器之间共享环境变量。

The user defined.network now facilitates the communication and here's how you can do it: user defined.network 现在可以促进通信,这是您可以如何做到的:

web:
  image: nginx:latest
  ports:
    - "8080:80"
    - "8000:8000"
  volumes:
    - ./code:/code
    - ./site.conf:/etc/nginx/conf.d/site.conf
  networks:
    - app-network

php:
  build: php/
  volumes:
    - ./code:/code
  networks:
    - app-network

dynamodb:
  build: dynamodb/
  expose:
    - 8000
  networks:
    - app-network

networks:
  app-network:
     driver: bridge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM