1

I was trying to write a Docker log file on Ubuntu 20.04 by

sudo docker logs CONTAINER_ID >output.log

But it returned

-bash: output.log: Permission denied

How to solve the permission problem to save the logs? Is the problem inside the container or outside of it?

P.S. I had this container by docker run -d -v ~/desktop/usercode/Docker:/code -p 5000:5000 flask_app:1.0, and the Dockerfile is as below:

## Base Python Image for App
FROM python:3.9-rc-buster


# Setting up Docker environment
# Setting Work directory for RUN CMD commands
WORKDIR /code
# Export env variables.
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
###


#Copy requirements file from current directory to file in
#containers code directory we have just created.
COPY requirements.txt requirements.txt

#Run and install all required modules in container
RUN pip3 install -r requirements.txt

#Copy current directory files to containers code directory
COPY . .

#RUN app.
CMD ["flask", "run"]

And, the images are:

REPOSITORY   TAG             IMAGE ID       CREATED          SIZE
flask_app    1.0             90b2840f4d5d   29 minutes ago   895MB
python       3.9-rc-buster   50625b35cf42   9 months ago     884MB
chenghuayang
  • 1,424
  • 1
  • 17
  • 35
  • 2
    See [Redirecting stdout to a file you don't have write permission on](https://unix.stackexchange.com/questions/1416/redirecting-stdout-to-a-file-you-dont-have-write-permission-on) on Unix & Linux Stack Exchange. This is not Docker-related; it has to do with the order in which the shell first sets up the redirect and then runs `sudo`. – David Maze Jul 13 '21 at 17:10
  • @DavidMaze Thank you David! It did help print logs out. However, only [part of the logs are printed](https://imgur.com/a/zZV6J6X). May I ask why? – chenghuayang Jul 13 '21 at 17:26

1 Answers1

1

The command you entered first creates output.log file in the same direction as you are, then drops the logs in that file; It seems that the problem is solved if you use the following command.

 docker logs CONTAINER_ID > ~/output.log

This command creates a log file in the root path of the user you are. for example if your username is USER1 that file create at /home/USER1

rezshar
  • 570
  • 1
  • 6
  • 20
  • Thank you. This helps save a log file. So the problem of my command is that the output.log created first can't be edited? What's the difference of process between yours and mine? – chenghuayang Jul 13 '21 at 17:46
  • Also, only [part of the logs](https://imgur.com/a/TJe9fLY) are printed instead of whole logs shown in terminal. Are the printed logs and logs in file different? – chenghuayang Jul 13 '21 at 17:47
  • 1
    hello @chenghuayang thanks for accepting my answer. if you want to print whole logs to this file please use this command `docker logs CONTAINER_ID >> ~/output.log 2>&1` – rezshar Jul 13 '21 at 17:56
  • Thanks! What does `2>&1` mean? (Or is there some docs about it? I'm not sure what to search about it.) – chenghuayang Jul 14 '21 at 03:00
  • 1
    1 is for standard output (stdout) and 2 is for standard error (stderr) and `2>&1` means redirects stderr to specific file and appending &1 redirects stderr to stdout. @chenghuayang – rezshar Jul 14 '21 at 05:30