Redirecting command output in docker

后端 未结 4 647
广开言路
广开言路 2020-12-01 04:26

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 dre         


        
相关标签:
4条回答
  • 2020-12-01 04:37

    Just a complement, when using docker-compose, you could also try:

    command: bash -c "script_or_command > /path/to/log/command.log 2>&1"

    0 讨论(0)
  • 2020-12-01 04:50

    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"]
    0 讨论(0)
  • 2020-12-01 04:50

    To use docker run in a shell pipeline or under shell redirection, making run accept stdin and output to stdout and stderr appropriately, use this incantation:

    docker run -i --log-driver=none -a stdin -a stdout -a stderr ...
    

    e.g. to run the alpine image and execute the UNIX command cat in the contained environment:

    echo "This was piped into docker" |
      docker run -i --log-driver=none -a stdin -a stdout -a stderr \
        alpine cat - |
      xargs echo This is coming out of docker: 
    

    emits:

    This is coming out of docker: This was piped into docker
    
    0 讨论(0)
  • 2020-12-01 04:56

    I personally use :

    ENTRYPOINT ["python3"]
    CMD ["-u", "-m", "swagger_server"]
    

    The "-u" is the key :)

    0 讨论(0)
提交回复
热议问题