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.

AbdurRehman Khan
  • 830
  • 9
  • 20
  • You should make sure that your container has "/home/pi" path. – thuyein Mar 14 '19 at 07: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. – AbdurRehman Khan Mar 14 '19 at 07: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. – thuyein Mar 14 '19 at 07:44
  • 2
    For more info -> https://docs.docker.com/storage/bind-mounts/ – thuyein Mar 14 '19 at 07: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. – AbdurRehman Khan Mar 14 '19 at 08:12

2 Answers2

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.

lyu.l
  • 282
  • 3
  • 8
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

Haran Rajkumar
  • 2,155
  • 16
  • 24
  • 2
    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. – AbdurRehman Khan Mar 14 '19 at 08:10