在docker中重定向命令输出 [英] Redirecting command output in docker

查看:2703
本文介绍了在docker中重定向命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是Dockerfile

 #Dockerfile 
从dreen / flask
MAINTAINER dreen
WORKDIR / srv

#获取源
运行mkdir -p / srv
COPY perfektimprezy.tar.gz /srv/perfektimprezy.tar.gz
运行tar x -f perfektimprezy.tar.gz
RUN rm perfektimprezy.tar.gz

#运行服务器
EXPOSE 80
CMD [python,index.py,1> server.log,2>服务器.log]

如最后一行所示,我将stderr和stdout重定向到一个文件。现在我运行这个容器和shell进入它

  docker run -d -p 80:80 perfektimprezy 
docker exec - 它...容器的id ...bash

并观察以下事项: p>

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



没有 / srv / server。日志



ps aux | grep python 产生:

 根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重定向到文件?

解决方案

当您将JSON列表指定为 CMD Dockerfile 中,它不会在shell中执行,所以通常的shell函数,如stdout和stderr重定向,



文档


exec表单被解析为JSON数组,这意味着您必须使用双引号( )围绕单词不单引号(')。



与外壳形式不同,exec表单不调用命令shell,这意味着正常的shell处理不会发生充足的 CMD [echo,$ HOME] 不会在 $ HOME 上执行变量替换。如果你需要shell处理,那么可以使用shell窗体或直接执行shell,例如: CMD [sh,-c,echo,$ HOME]

您的命令实际上是执行 index.py 脚本并传递字符串1> server.log2> server.log作为这个python脚本的命令行参数。



使用以下代码之一(两者都应该工作):


  1. CMDpython index.py> server.log 2>& 1

  2. CMD [/ bin / sh,-c,python index.py> server.log 2& 1] li>


I want to do some simple logging for my server which is a small Flask app running in a Docker container.

Here is the 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"]

As you can see on the last line I redirect stderr and stdout to a file. Now I run this container and shell into it

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

And observe the following things:

The server is running and the website working

There is no /srv/server.log

ps aux | grep python yields:

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

But there are no logs... HOWEVER, if I docker attach to the container I can see the app generating output in the console.

How do I properly redirect stdout/err to a file when using Docker?

解决方案

When you specify a JSON list as CMD in a Dockerfile, it will not be executed in a shell, so the usual shell functions, like stdout and stderr redirection, won't work.

From the 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" ].

What your command actually does is executing your index.py script and passing the strings "1>server.log" and "2>server.log" as command-line arguments into that python script.

Use one of the following instead (both should work):

  1. CMD "python index.py > server.log 2>&1"
  2. CMD ["/bin/sh", "-c", "python index.py > server.log 2>&1"]

这篇关于在docker中重定向命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆