Docker Community Forums

Share and learn in the Docker community.

Gcc container logs empty

Good morning!

I’m using the gcc official docker image to build and run c/c++ code, I want to retrieve the result of the execution of the source code using the logs command "docker container logs ", but it seems that the logs file always empty and by typing the logs command in the terminal nothing is shown up, tried attaching the stdout and stderr with the flag -a while creating the container but nothing has changed with the logs, always empty

my question is how I can retrieve the logs of the container built with the gcc image.

the command I’m using :
$ docker run -v “$PWD”:/usr/src/myapp -w /usr/src/myapp gcc:4.9 gcc -o myapp myapp.c && ./myapp

You need to be sure to save your file into the mounted directory. Right now, it looks like your file is being saved as a sibling to your program which is right outside of the mounted directory.

Since you mount with:

docker run -v /home/user/tmp/:/repo/dir image dir/file1 dir/file2
/repo/dir is the only folder you will see changes to. But if you are saving files to /repo, they will get saved there, but not seen on the host system after running.

Consider how you open your output file:

std::string filename (“”); // in the actual code this name is not hard-coded and depends on intput, but it will not include / chars
std::ofstream output_file;, std::ios::out);
output_file << some_data << etc << std::endl;

Since you set the output file to “” with no path, it is going to be opened as a sibling to the program.

If you were to run

docker run -it --rm --entrypoint=/bin/bash image
which would open an interactive shell using your image and then run ./program some-file.text some-other-file.txt and then ran ls you would see the output file as a sibling to program. That is outside of your mountpoint, which is why you don’t see it on your host machine.

Consider this program. This program will take an input file and an output location. It will read in each line of the infile and wrap it in

. /some is the repository directory. /some/res/ is the folder that will be mounted to /repo/res/.

I provide 2 arguments to my program through docker run, the infile and outfile both of which are relative to /repo which is the working directory.

My program then saves to the outfile location which is within the mountpoint (/repo/res/). After docker run finishes, /some/res/out.txt is populated.