4

I have a simple setup with python's logging module set up in a python application like so:

app_logger = logging.getLogger('main_thread')

file_handler = RotatingFileHandler('/home/pi/FaunderGateway_Log.log', maxBytes=10000000, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
file_handler.setFormatter(formatter)

app_logger.addHandler(file_handler)

I have a RotatingFileHandler which has the absolute path "/home/pi/FaunderGateway_Log.log" (this application will be running on a raspberry pi in case anyone's wondering), now I'm trying to containerize the application with docker.

After building and trying to run my container I get the error:

[Errno 2] No such file or directory: '/home/pi/FaunderGateway_Log.log'

Now, I'm aware that docker has its own file system within the container that is separate from the host's (pi) file system, but I want to tell docker to let my python app log normally in the host's absolute path /home/pi/. How can I achieve this?

I read some other threads which mentioned volumes but I don't really understand them all that well.

I'm using this command to run my container:

sudo docker run --privileged fg

The --privileged flag is so that I can access /dev/mem file on the pi, for GPIO operations.

Update: Please note, that I want my docker container to create the log file in the host's path "/home/pi", I don't want to create a "/home/pi" directory inside the container itself.

5
  • You should make sure that your container has "/home/pi" path. Commented Mar 14, 2019 at 7:40
  • @ThuYeinTun I want my container to make the log file on the host's "/home/pi" path, so I can open the log file even after the container stops running. Commented Mar 14, 2019 at 7:43
  • 2
    In that case, you should update your question to include that information. You can use docker volume to mount host's directory into docker container's directory. Then whatever you write in that directory will be available in the host's. Commented Mar 14, 2019 at 7:44
  • 2
    For more info -> docs.docker.com/storage/bind-mounts Commented Mar 14, 2019 at 7:47
  • @ThuYeinTun Updated and bolded the important part :D Thank you for that link to the docs! It helped a lot. I didn't know this was called bind mounts. Commented Mar 14, 2019 at 8:12

2 Answers 2

4

Volumes can solve the problem. A container has it's own filesystem. Using volumes is like connecting a pen drive that has files from your host to the docker container.

In your case, you could do this:

sudo docker run --privileged fg -v /home/pi:/pi

Now, this would create a folder in the root of your container called pi that is linked to /home/pi in your host.

Thus, in your python app specify /pi as the directory.

Relevant documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Accepted this answer for the nice explanation, thanks :D I just used "sudo docker run --privileged fg -v /home/pi:/home/pi" and it worked.
4

try:

docker run -v [host_path]:[container_path]

In your case host_path is /home/pi, and change container_path to the log file dir in your container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.