linux – 在docker中重定向命令输出

我想为我的服务器做一些简单的日志记录,这是一个在Docker容器中运行的小型Flask应用程序.

这是Dockerfile

# Dockerfile
FROM dreen/flask
MAINTAINER dreen
workdir /srv

# Get source
RUN mkdir -p /srv
copY perfektimprezy.tar.gz /srv/perfektimprezy.tar.gz
RUN tar x -f perfektimprezy.tar.gz
RUN rm perfektimprezy.tar.gz

# Run server
EXPOSE 80
CMD ["python", "index.py", "1>server.log", "2>server.log"]

正如您在最后一行所看到的,我将stderr和stdout重定向一个文件.现在我运行这个容器和shell

docker run -d -p 80:80 perfektimprezy
docker exec -it "... id of container ..." bash

并观察以下事项:

服务器正在运行,网站正常运行

没有/srv/server.log

ps aux | grep python产量:

root         1  1.6  3.2  54172 16240 ?        Ss   13:43   0:00 python index.py 1>server.log 2>server.log
root        12  1.9  3.3 130388 16740 ?        Sl   13:43   0:00 /usr/bin/python index.py 1>server.log 2>server.log
root        32  0.0  0.0   8860   388 ?        R+   13:43   0:00 grep --color=auto python

但是没有日志……但是,如果我将docker附加到容器上,我可以在控制台中看到应用程序生成输出.

使用Docker时如何正确地将stdout / err重定向文件

解决方法:

在Dockerfile中将JSON列表指定为CMD时,它不会在shell中执行,因此通常的shell函数(如stdout和stderr重定向)将不起作用.

documentation

The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].

您的命令实际执行的是执行index.py脚本并将字符串“1> server.log”和“2> server.log”作为命令行参数传递到该python脚本中.

请改用以下其中一种(两者都应该有效):

> CMD“python index.py> server.log 2>& 1”
> CMD [“/ bin / sh”,“ – c”,“python index.py> server.log 2>& 1”]

相关文章

Docker是什么Docker是 Docker.Inc 公司开源的一个基于 LXC技...
本文为原创,原始地址为:http://www.cnblogs.com/fengzheng...
镜像操作列出镜像:$ sudo docker imagesREPOSITORY TAG IMA...
本文原创,原文地址为:http://www.cnblogs.com/fengzheng/p...
在 Docker 中,如果你修改了一个容器的内容并希望将这些更改...
在Docker中,--privileged 参数给予容器内的进程几乎相同的权...