【问题标题】:Amazon S3 + Docker - "403 Forbidden: The difference between the request time and the current time is too large"Amazon S3 + Docker - “403 Forbidden:请求时间与当前时间相差太大”
【发布时间】:2026-02-05 01:50:02
【问题描述】:

我正在尝试在 docker 容器中运行我的 django 应用程序,其中包含从 Amazon S3 提供的静态文件。当我在 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 或任何其他类型的反向代理服务器。

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

尝试的解决方案

其他 * 问题使用 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"]

【问题讨论】:

  • 你在使用 boot2docker 吗?我遇到了时间问题,因为当您的计算机休眠时,boot2docker vm 的时钟会停止。
  • 是的,我是。我会尝试重新启动 boot2docker

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


【解决方案1】:

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

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

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

【讨论】:

  • 这解决了我的问题。我忘记了我 mac 上的 Docker 主机确实是 boot2docker vm。没想到那里看。谢谢!
  • 没问题。同样的事情让我陷入了一次循环。
  • 使用“Docker for Mac”的相同症状,它的工作方式与 boot2docker 类似(但有一个 Linux VM 在某处运行的事实对你来说更隐蔽)。从菜单栏中的“鲸鱼菜单”重新启动 docker 修复了 AWS/S3 403 错误。
  • @Alex 同样的事情发生在我身上..在 Ubuntu 14.04 系统中一切正常,但在 Windows 8 (XAMPP) 中却没有
  • 找到解决方案...XAMPP 服务器的系统日期提前 1 天。
【解决方案2】:

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

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

【讨论】:

  • 在构建映像之前,我尝试将主机与 sudo ntpd -gq 同步,但仍然出现错误。我还希望在构建映像时运行 collectstatic 命令,因此我不能传递任何标志来强制容器的时区与我的主机匹配,尽管如果 docker 默认设置这确实不应该有所作为到我的主机时间。
  • 查看错误,我想问题与旧时间参考有关,而不是时间偏移。可能问题与docker镜像构建过程中的图层缓存有关。在这种情况下,使用--no-cache=true 选项构建映像会有所帮助。
  • 你能发布你的 Dockerfile 吗? (省略任何敏感信息)。
【解决方案3】:

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

【讨论】:

    最近更新 更多