Redirecting output of nohup in docker not working

人盡茶涼 提交于 2021-02-11 12:57:46

问题


I have to run a flask application in docker. The application is started when I run start.sh (details below) which started the application using nohup command and redirects the output to a logfile.

This is running fine in normal Linux system (without docker), but when I run this inside a docker container, the log file does not show any output. In the docker container, the log file is generated with size 0 and the log output is dumped in nohup.out file.

I am not sure why the output is redirected to nohup.out instead of the specified logfile, while it is working fine in my current Linux setup.

start.sh:

nohup uwsgi --ini /home/myuser/myapp/play.ini &>> /home/myuser/myapp/logs/play.nohup.out &

Dockerfile:

#Download base image ubuntu 16.04
FROM ubuntu:16.04

RUN apt-get update
RUN apt-get install -y git-core
RUN apt-get install -y nginx
RUN apt-get install -y python

# get application code and install python packages
RUN git clone <..git directory...> /home/myuser/myapp
RUN pip install -r /home/myuser/myapp/requirements.txt

# Setup nginx
RUN cp /home/myuser/myapp/myapp.conf /etc/nginx/conf.d/myapp.conf

CMD sleep 600
EXPOSE 80

回答1:


I faced similar issue using cron commands, the issue I found is that the command was ran with shell, but shell does not support &>> as it is a bash feature.

I was able to overcome that using a command with the following synthax:

command >> logfile.log 2>&1

You could find more informtion on this link https://www.brianstorti.com/understanding-shell-script-idiom-redirect/




回答2:


You generally don’t use nohup in Docker at all. Usually you want to set things up so that your container runs its main job as a foreground process. (There’s not really much point in installing a bunch of stuff in an image, and then have it sit idle for 10 minutes and then exit.)

You should change the last line of your Dockerfile to

CMD uwsgi --ini /home/myuser/myapp/play.ini

If you docker run the resulting image without the -d flag, it will print its output to stdout; if you do use -d, you can retrieve it later using docker logs.



来源:https://stackoverflow.com/questions/57690850/redirecting-output-of-nohup-in-docker-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!