How to log persistent from a application running in docker

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


To address the issue of fopen_s() returning error 13 (permission denied) when trying to create a file in a bind-mounted directory inside a Docker container on TorizonCore, you need to ensure that the user inside the container has the appropriate permissions to write to the bind-mounted directory.

Here are a few steps to troubleshoot and resolve this issue:

  1. Check Directory Permissions on Host:
    Ensure that the ./logs directory on the host (TorizonCore) has the correct permissions to allow writing by the user inside the container.

    sudo chmod -R 777 ./logs
    

    This command will give read, write, and execute permissions to all users for the ./logs directory. Adjust the permissions as needed for your security requirements.

  2. Ensure User Permissions Inside Container:
    By default, the torizon user inside the container should have the necessary permissions. However, if your application is running as a different user, you need to ensure that this user has the appropriate permissions.

    You can add the torizon user to the necessary groups or run the container as the torizon user. Here’s how you can modify your docker-compose.yml to run the container as the torizon user:

    version: "3.9"
    services:
      servicename-debug:
        build:
          context: .
          dockerfile: Dockerfile.debug
        volumes:
          - type: bind
            source: ./logs
            target: /tmp/logs
        image: ${LOCAL_REGISTRY}:5002/servicename-debug:${TAG}
        user: torizon
        privileged: true
        ports:
          - 6502:6502
    
  3. Verify UID/GID Consistency:
    Ensure that the UID and GID of the user inside the container match those on the host. This can help avoid permission issues. You can check the UID and GID of the torizon user inside the container and ensure they match the owner of the ./logs directory on the host.

    id torizon
    

    If needed, you can change the ownership of the ./logs directory on the host to match the UID and GID of the torizon user.

    sudo chown -R <UID>:<GID> ./logs
    
  4. Debugging Permissions:
    If the above steps do not resolve the issue, you can debug the permissions by running an interactive shell inside the container and checking the permissions directly.

    docker exec -it <container_id> /bin/bash
    ls -l /tmp/logs
    touch /tmp/logs/testfile
    

    This will help you identify if there are any permission issues directly inside the container.

By following these steps, you should be able to resolve the permission denied issue and ensure that your C++ application can log persistently to the bind-mounted directory.

Please, also check if these links can help you solve your problem:

JSON File logging driver | Docker Docs.