Amazon S3 + Docker - "403 Forbidden: The difference between the request time and the current time is



我正在尝试在 docker 容器中运行我的 django 应用程序,其中包含从 Amazon S3 提供的静态文件。当我在 Dockerfile 中运行 RUN $(which python3.4) /home/docker/code/vitru/manage.py collectstatic --noinput 时,我收到来自 Amazon S3 的 403 禁止访问错误,其中包含以下响应 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 或任何其他类型的反向代理服务器。

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

尝试的解决方案

其他堆栈溢出问题报告了使用 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

我还尝试在构建 docker 映像之前在本地计算机上同步时间,使用 sudo ntpd -gq ,但这也没有奏效。

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"]

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

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

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

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

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

重新启动 Docker for Mac 可以修复我的机器上的错误。

最新更新