Docker容器与主机不同步 [英] Docker container out of sync with host
问题描述
我有一个简单的Node应用程序,该应用程序将消息发送到AWS SQS.对于本地开发,我为AWS开发工具包提供region
,queueUrl
,accessKeyId
,secretAccessKey
.
I have a simple Node app which sends messages to AWS SQS. For local development I am providing AWS SDK with region
, queueUrl
, accessKeyId
, secretAccessKey
.
一切正常,直到我将应用程序泊坞窗化并作为容器运行.然后,每当SQS想要做某事时,我都会收到以下错误
Everything works fine until I dockerise the app and run as a container. Then whenever SQS wants to do something I get the following error
{ SignatureDoesNotMatch: Signature expired: 20161211T132303Z is now earlier than 20161211T142227Z (20161211T143727Z - 15 min.)
{ SignatureDoesNotMatch: Signature expired: 20161211T132303Z is now earlier than 20161211T142227Z (20161211T143727Z - 15 min.)
如果我添加correctClockSkew: true
,它可以解决问题.
If I add correctClockSkew: true
it corrects the problem.
在MacOS中运行Node时docker在做什么需要correctClockSkew: true
而不是
What is docker doing to require the correctClockSkew: true
but not when running Node in MacOS
节点应用
process.env.TZ = 'Europe/London';
const AWS = require('aws-sdk');
AWS.config.update({
region: 'eu-west-1',
correctClockSkew: true //this has to be set when running inside a docker container?
});
const sqs = new AWS.SQS({
apiVersion: '2012-11-05',
});
sqs.sendMessage({
QueueUrl: 'https://sqs.eu-west-1.amazonaws.com/522682236448/logback-paddle-prod-errors',
MessageBody: 'HelloSQS',
}, (err, data) => {
if (err) throw err;
});
Dockerfile
Dockerfile
FROM node
RUN mkdir -p /usr/lib/app
WORKDIR /usr/lib/app
COPY app/ /usr/lib/app/
RUN npm install
CMD ["node", "index.js"]
docker run -d user/image
编辑
最初,我创建了这个问题是因为我不断收到AWS错误的时间错误,现在我也通过ElasticSearch收到了它.为什么我的容器在大约15分钟的时间内与主机可靠地不同步.
Originally I created the question because I kept getting AWS incorrect time errors, now I am getting it with ElasticSearch too. Why is my container reliably out of sync with the host by about 15 mins.
推荐答案
Docker在Windows和MacOS上的VM内部运行,并且该VM的时钟可能与笔记本电脑的OS的时钟不同步.我见过很多解决方案,其中大多数是以下命令:
Docker runs inside of a VM on Windows and MacOS, and the clock of that VM can get out of sync with that of your laptop's OS. There are quite a few solutions I've seen, mostly one off commands including:
docker run -it --rm --privileged --pid=host debian nsenter -t 1 -m -u -n -i date -u $(date -u +%m%d%H%M%Y)
从此答案中可以找到
docker-machine ssh default "sudo date -u $(date -u +%m%d%H%M%Y)"
为此,我看到的最佳解决方案是在特权模式下运行ntp容器,以便它可以不断调整docker主机上的时间:
The best solution I've seen for this is to run an ntp container in privledged mode so it can constantly adjust the time on your docker host:
docker run -d --restart unless-stopped --name ntp --privileged tutum/ntpd
有关更多详细信息,请参阅docker hub仓库: https://hub.docker.com/r/tutum/ntpd/
See the docker hub repo for more details: https://hub.docker.com/r/tutum/ntpd/
这篇关于Docker容器与主机不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!