I'm writting a C++ program, and want to use Docker on it. The Dockerfile looks like the following:
FROM gcc:7.2.0
ENV MYP /repo
WORKDIR ${MYP}
COPY . ${MYP}
RUN /bin/sh -c 'make'
ENTRYPOINT ["./program"]
CMD ["file1", "file2"]
Run Code Online (Sandbox Code Playgroud)
This program needs two input files (file1 and file2) and is built and executed with as follows:
docker build -t image .
docker run -v /home/user/tmp/:/repo/dir image dir/file1 dir/file2
Run Code Online (Sandbox Code Playgroud)
These input files are located in the host in /home/user/tmp/. In the original repository (repo/), the executable is located in its root directory, and the output file generated is saved in the same folder (i.e. they look like repo/program and repo/results.md).
When I run the above docker run command, I can see from the standard output that the executable is reading correctly the input files and generating the expected results. However, I hoped the written output file (generated by the program with std::ofstream) to be also saved in the mounted directory /home/user/tmp/, but its not.
How can I access this file? Is there a straightforward way to get it using the docker volume mechanism?
Docker version is 18.04.0-ce, build 3d479c0af6.
EDIT
The relevant code regarding how the program saves the output file result.md is the following:
std::string filename ("result.md"); // in the actual code this name is not hard-coded and depends on intput, but it will not include / chars
std::ofstream output_file;
output_file.open(filename.data(), std::ios::out);
output_file << some_data << etc << std::endl;
...
output_file.close();
Run Code Online (Sandbox Code Playgroud)
In practice, the program is run as program file1 file2, and the output will be saved in the working directory, not matter if its the same where program is placed or not.
您需要确保将文件保存到挂载目录中。现在,您的文件似乎被另存为您的程序的同级文件,该程序就在挂载目录之外。
由于您安装:
docker run -v /home/user/tmp/:/repo/dir image dir/file1 dir/file2
Run Code Online (Sandbox Code Playgroud)
/repo/dir是您将看到更改的唯一文件夹。但是,如果您将文件保存到/repo,它们将保存在那里,但在运行后在主机系统上看不到。
考虑如何打开输出文件:
std::string filename ("result.md"); // in the actual code this name is not hard-coded and depends on intput, but it will not include / chars
std::ofstream output_file;
output_file.open(filename.data(), std::ios::out);
output_file << some_data << etc << std::endl;
...
output_file.close();
Run Code Online (Sandbox Code Playgroud)
由于您将输出文件设置为"result.md"无路径,因此它将作为程序的同级打开。
如果你要跑
docker run -it --rm --entrypoint=/bin/bash image
Run Code Online (Sandbox Code Playgroud)
这将使用您的图像打开一个交互式外壳,然后运行./program some-file.text some-other-file.txt然后运行ls您将看到输出文件result.md作为program. 那是在您的挂载点之外,这就是您在主机上看不到它的原因。
考虑这个程序。该程序将采用一个输入文件和一个输出位置。它将读取 的每一行infile并将其包装在<p>.  /some是存储库目录。  /some/res/是将挂载到的文件夹/repo/res/。
我公司提供2个参数,以我的程序通过docker run,将infile与outfile这两者是相对于/repo它的工作目录。
我的程序然后保存到outfile安装点 ( /repo/res/)内的位置。后docker run完成,/some/res/out.txt被填充。
.
??? Dockerfile
??? README.md
??? makefile
??? res
?   ??? in.txt
??? test.cpp
Run Code Online (Sandbox Code Playgroud)
docker build -t image .
docker run --rm -v ~/Desktop/some/res/:/repo/res/ image ./res/in.txt ./res/out.txt
Run Code Online (Sandbox Code Playgroud)
FROM gcc:7.2.0
ENV MYP /repo
WORKDIR ${MYP}
COPY . ${MYP}
RUN /bin/sh -c 'make'
ENTRYPOINT ["./test"]
CMD ["file1", "file2"]
Run Code Online (Sandbox Code Playgroud)
test: test.cpp
    g++ -o test test.cpp
.PHONY: clean
clean:
     rm -f test
Run Code Online (Sandbox Code Playgroud)
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char **argv) {
    if (argc < 3) {
        std::cout << "Usage: test [infile] [outfile]" << std::endl;
        return 1;
    }
    std::cout << "All args" << std::endl;
    for (int i = 0; i < argc; i++) {
        std::cout << argv[i] << std::endl;
    }
    std::string line;
    std::ifstream infile(argv[1]);
    std::ofstream outfile(argv[2]);
    if (!(infile.is_open() && outfile.is_open())) {
        std::cerr << "Unable to open files" << std::endl;
        return 1;
    }
    while (getline(infile, line)) {
        outfile << "<p>" << line << "</p>" << std::endl;
    }
    outfile.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
hello
world
Run Code Online (Sandbox Code Playgroud)
<p>hello</p>
<p>world</p>
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           11820 次  |  
        
|   最近记录:  |