Amazon S3 + Docker - " 403禁止:请求时间和当前时间之间的差异太大"

时间:2014-12-28 08:54:21

标签: django ubuntu amazon-web-services amazon-s3 docker

我正在尝试使用从Amazon S3提供的静态文件在docker容器中运行我的django应用程序。当我在Dockerfile中运行RUN $(which python3.4) /home/docker/code/vitru/manage.py collectstatic --noinput时,我从Amazon S3获得403 Forbidden错误,其中包含以下响应XML

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>RequestTimeTooSkewed</Code>
    <Message>The difference between the request time and the current time is too large.</Message>
    <RequestTime>Sat, 27 Dec 2014 11:47:05 GMT</RequestTime>
    <ServerTime>2014-12-28T08:45:09Z</ServerTime>
    <MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>
    <RequestId>4189D5DAF2FA6649</RequestId>
    <HostId>lBAhbNfeV4C7lHdjLwcTpVVH2snd/BW18hsZEQFkxqfgrmdD5pgAJJbAP6ULArRo</HostId>
</Error>

我的docker容器正在运行Ubuntu 14.04 ...如果这有任何区别。 我也使用uWSGI运行应用程序,没有nginx或apache或任何其他类型的反向代理服务器。

我也在运行时收到错误,当文件被提供给网站时。

尝试解决方案

其他stackoverflow问题使用S3报告了类似的错误(没有特别与Docker一起使用),并且他们说当系统时钟不同步时会导致此错误,并且可以通过运行来修复

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start

所以我将以下内容添加到我的Dockerfile中,但它没有解决问题。

RUN apt-get install -y ntp
RUN ntpd -gq
RUN service ntp start

我还尝试在使用sudo ntpd -gq构建docker镜像之前在本地计算机上同步时间,但这也不起作用。

Dockerfile

FROM ubuntu:14.04

# Get most recent apt-get
RUN apt-get -y update

# Install python and other tools
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python3 python3-dev python-distribute
RUN apt-get install -y nginx supervisor
# Get Python3 version of pip
RUN apt-get -y install python3-setuptools
RUN easy_install3 pip

# Update system clock so S3 does not get 403 Error
# NOT WORKING
#RUN apt-get install -y ntp
#RUN ntpd -gq
#RUN service ntp start

RUN pip install uwsgi
RUN apt-get -y install libxml2-dev libxslt1-dev 

RUN apt-get install -y python-software-properties uwsgi-plugin-python3

# Install GEOS
RUN apt-get -y install binutils libproj-dev gdal-bin

# Install node.js
RUN apt-get install -y nodejs npm

# Install postgresql dependencies 
RUN apt-get update && \
    apt-get install -y postgresql libpq-dev && \
    rm -rf /var/lib/apt/lists

# Install pylibmc dependencies
RUN apt-get update
RUN apt-get install -y libmemcached-dev zlib1g-dev libssl-dev

ADD . /home/docker/code

# Setup config files
RUN ln -s /home/docker/code/supervisor-app.conf /etc/supervisor/conf.d/

RUN pip install -r /home/docker/code/vitru/requirements.txt


# Create directory for logs
RUN mkdir -p /var/logs

# Set environment as staging
ENV env staging

# Run django commands
# python3.4 is at /usr/bin/python3.4, but which works too
RUN $(which python3.4) /home/docker/code/vitru/manage.py collectstatic --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py syncdb --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py makemigrations --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py migrate --noinput


EXPOSE 8000

CMD ["supervisord", "-c", "/home/docker/code/supervisor-app.conf"]

3 个答案:

答案 0 :(得分:6)

在评论中注明,但其他人来到这里:

如果使用boot2docker(例如,在Windows或Mac上),则在您使用计算机时,boot2docker vm会出现已知的时间问题 - 请参阅here。由于docker容器的主机是boot2docker vm,因此它会同步它的时间。

我已成功重新启动boot2docker vm。这可能会导致丢失某些状态的问题,即如果您有一些数据量。

答案 1 :(得分:5)

Docker容器与主机共享时钟,因此同步主机时钟应解决问题。要强制容器的时区与您在-v /etc/localtime:/etc/localtime:ro中添加docker run的主机相同。

无论如何,您不应该在Dockerfile中启动服务。此文件包含为容器构建映像的步骤和命令,并且在Dockerfile中运行的任何进程将在构建过程后结束。要启动任何服务,您应该添加运行脚本或进程控制守护程序(作为supervisord),每次运行新容器时都会运行该守护程序。

答案 2 :(得分:2)

重新启动Docker for Mac可修复计算机上的错误。

相关问题