既然我们只能在Dockerfile中有一个ENTRYPOINT和CMD,我们如何运行多个可执行文件?
考虑以下情况:Docker复制两个cpp文件并编译,生成两个可执行文件。ENTRYPOINT将定义运行一个文件,并使用cmd为其提供参数。
Dockerfile:
FROM gcc:11.3.0
# Copy the current folder which contains C++ source code to the Docker image under
/root/sonu
COPY . /root/sonu
# Specify the working directory
WORKDIR /root/sonu
# Use GCC to compile the Test.cpp source file
RUN g++ -o Tests tests.cpp
RUN g++ -o Testd testd.cpp
#entrypoint for executing the executable file
ENTRYPOINT ["./Tests"]
CMD ["10","3"]
下面是第一个文件(tests.cpp)
的代码#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
cout << "You have entered " << argc
<< " arguments:" << "n";
if (argc != 3) {
cerr << "Program is of the form: " << argv[0] << " <inp1> <inp2>n";
return 1;
}
float n1= stof(argv[1]);
float n2= stof(argv[2]);
float sum =n1+n2;
cout << "Result: " << sum << endl;
return 0;
}
这个是用于减法(第二次测试)-减法test .cpp
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
cout << "You have entered " << argc
<< " arguments:" << "n";
if (argc != 3) {
cerr << "Program is of the form: " << argv[0] << " <inp1> <inp2>n";
return 1;
}
float n1= stof(argv[1]);
float n2= stof(argv[2]);
float diff =n1+n2;
cout << "Result: " << diff << endl;
return 0;
}
目的输出我希望能够运行任何一个可执行文件与命令行参数给他们。
p:创建多个docker映像是不可行的,因为整个映像将占用大量内存,并且正在发送的二进制文件小于200 MB
如何从您的ENTRYPOINT
调用脚本而不是单个可执行文件?
脚本可以简单地调用您想要的任何可执行文件。你需要把它导入到Dockerfile的镜像中。