My code needs to read and write files to Docker configured volume. It uses C++ to do it:
docker-compose.yml:
services:
appd-debug:
build:
context: .
dockerfile: Dockerfile.debug
image: ${LOCAL_REGISTRY}:5002/appd-debug:${TAG}
ports:
- 2230:2230
volumes:
- app-debug-data:/data:rw
appd:
build:
context: .
dockerfile: Dockerfile
image: ${DOCKER_LOGIN}/appd:${TAG}
volumes:
- app-data:/data:rw
volumes:
app-debug-data:
app-data:
My C++ code that runs into the container:
main.cpp
#include <filesystem>
#include <iostream>
#include <fstream>
int main()
{
std::string current_path = std::filesystem::current_path().string();
std::cout << "Current path: " << current_path << std::endl;
std::cout << std::endl << "Checking content of: /data" << std::endl;
std::string path = "/data";
for (const auto & entry : std::filesystem::directory_iterator(path))
std::cout << entry.path() << std::endl;
std::cout << std::endl << "Writing file" << std::endl;
std::string d = "This is a test";
std::ofstream out("/data/output.txt");
out << d;
out.close();
std::cout << std::endl << "Reading file" << std::endl;
std::ifstream f("/data/output.txt");
if (f.is_open())
std::cout << f.rdbuf();
else
std::cout << "Error opening file" << std::endl;
f.close();
std::cout << std::endl << "DONE" << std::endl;
}
If I run it on the Toradex IDE Extension, I’m getting error accessing the file at the DEBUG CONSOLE window:
Checking content: /data
GO
Error opening file
DONE
If I ssh to the board (Verdin Mini) and run the container locally, all works fine:
$ docker exec -it torizon-appd-debug-1 sh
# ls /data
#
# ./home/torizon/app/appd
GO
This is a test
DONE
# ls /data
output.txt
# cat /data/output.txt
This is a test
For some reason the DEBUG CONSOLE is not allowing files to be created at the Docker volume. How can I fix this in order to run the debugger that uses the volume?
Hi, I’m the Toradex AI Assistant. I’m in early beta, and I would appreciate your feedback.
The Toradex Support team will follow up with you shortly!
Did you like my answer?
It seems like the issue might be related to the permissions or the way the volume is mounted when using the Toradex IDE Extension for debugging. Here are a few steps you can take to troubleshoot and potentially resolve the issue:
-
Check Volume Mounting in Docker Compose:
Ensure that the volume is correctly mounted and accessible with the appropriate permissions. You can add the :rw
option to explicitly set the volume to be read-write.
volumes:
- app-debug-data:/data:rw
-
Verify User Permissions:
Ensure that the user running the application inside the container has the necessary permissions to write to the /data
directory. By default, the torizon
user should have the necessary permissions, but you can explicitly set the user in your Dockerfile or Docker Compose file.
services:
appd-debug:
build:
context: .
dockerfile: Dockerfile.debug
image: ${LOCAL_REGISTRY}:5002/appd-debug:${TAG}
ports:
- 2230:2230
volumes:
- app-debug-data:/data:rw
user: torizon
-
Check Dockerfile for User Context:
Ensure that your Dockerfile is setting the correct user context. If your Dockerfile is not setting the user to torizon
, you might face permission issues.
FROM torizon/debian:2-bullseye
USER torizon
-
Debugging Permissions:
When running the container via the Toradex IDE Extension, you can add a step to check the permissions of the /data
directory inside the container.
#include <sys/stat.h>
#include <iostream>
int main()
{
struct stat info;
if (stat("/data", &info) != 0)
std::cerr << "Cannot access /data" << std::endl;
else if (info.st_mode & S_IWUSR)
std::cout << "/data is writable" << std::endl;
else
std::cerr << "/data is not writable" << std::endl;
// Rest of your code...
}
-
Check Docker Logs:
Check the Docker logs for any errors or warnings related to volume mounting or permissions.
Hi, @renatom !
When you pass a volume to the container, we recommend using the full path name.
Could you test start the container and run the application manually? You can use the command docker run
to do it. Remember to use the flags -v
for volumes and -p
for ports.
Your command should be something similar to:
docker run -it -v /path/to/folder/app-debug-data:/data -p 2230:2230 torizon-appd-debug-1 /bin/bash
And, after that, you could try to run the application.
Best regards.
Lucas Azeituno.
Whe I log in the board and manually run it works fine.
Check my last piece of commands in the original post:
$ docker exec -it torizon-appd-debug-1 sh
# ls /data
#
# ./home/torizon/app/appd
GO
This is a test
DONE
# ls /data
output.txt
# cat /data/output.txt
This is a test
The problem arises when running at the Toradex IDE Extension DEBUG CONSOLE, as said.
Hi, @renatom !
In these commands, you are accessing a running container. What I’m asking is to stop the running container and try to run it again using the docker run
command with the same flags that are used in docker-compose.yml.
By doing that, you can verify if the issue is related to the IDE Extension itself or some parameter that is used to run the container.
Best regards.
Lucas Azeituno