主机箱/bash 未执行 docker 映像

Docker image not executed by host bin/bash

本文关键字:执行 docker 映像 bash 机箱 主机      更新时间:2023-10-16

我有一个Linux可执行文件。我已经用 centOS 7 构建了一个 docker 镜像,当直接从 macOS 终端(macOS Mojave 版本 10.14.6(执行时,它可以正常运行。映像是通过以下方式构建的:

Dockerfile$ docker build -t my_testi .

FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL 
org.label-schema.schema-version="1.0" 
org.label-schema.name="CentOS Base Image" 
org.label-schema.vendor="CentOS" 
org.label-schema.license="GPLv2" 
org.label-schema.build-date="20200504" 
org.opencontainers.image.title="CentOS Base Image" 
org.opencontainers.image.vendor="CentOS" 
org.opencontainers.image.licenses="GPL-2.0-only" 
org.opencontainers.image.created="2020-05-04 00:00:00+01:00"
CMD ["/bin/bash"]
ADD executable /bin/
CMD /bin/bash cd /casedir && executable 

我可以使用命令执行此操作:$ docker run -ti -v /host/casedir/:/casedir my_testi executable /casedir/inputfile.txt.现在,我正在尝试让 docker 映像从 C++ 中的/bin/bash运行。

.cpp中的代码段

FILE *pipe;
std::string s="";
pipe = popen("/bin/bash", "w");
fprintf(pipe, "source ~/.bash_profile n");
#if defined(__unix__) || defined(__unix) || 
(defined(__APPLE__) && defined(__MACH__))
// On MacOX we run with docker
s += std::string("docker run -ti -v ") +
analysispath.parent_path().string() + std::string("/:/casedir my_testi executable /casedir/") +
inputfiles.back().filename().string(); // docker run -ti -v /host/casedir/:/casedir my_testi executable /casedir/inputfile.txt
#elif defined(__linux__)
// On Linux based systems link executable and run
s += std::string("ln -s ") + std::getenv("EXECUTABLE") + std::string(" executablen");
s += std::string("./executable ") +  inputfiles.back().filename().string() + std::string(" > executable_out") + std::string(" n");
#endif
fprintf(pipe, "%s", s.c_str());

问题是C++代码中的bash不会运行 docker 映像,或者 (?( 不会等待命令执行。它直接跳到下一个代码块。这里可能有什么问题,我怎样才能C++等待命令执行?

有两个问题需要解决。首先,docker 命令必须以n终止,并且必须在非交互模式下运行,即-t而不是-ti.因此,该命令将更改为:

s += std::string("docker run -t -v ") +
analysispath.parent_path().string() + std::string("/:/casedir my_testi executable /casedir/") +
inputfiles.back().filename().string() + std::string("n");