无法为简单程序运行Docker容器

时间:2019-01-31 11:20:17

标签: c++ docker dockerfile

我打算将docker用于c ++项目。 我添加了一个简单的c ++程序,该程序会打印“ hello world”进行测试。 然后我编译了它

g++ -o helloworldexecutable helloword.cpp

添加到Dockerfile项目根文件夹 Dockerfile包含:

FROM scratch
ADD helloworldexecutable / 
CMD helloworldexecutable

当我运行:sudo docker build --tag helloworldexecutable .它表明构建成功。我使用sudo docker images看到了这张图片 我可以启动sudo docker run hello-world,但是运行sudo docker run helloexecutable会出现错误:

  

docker:来自守护程序的错误响应:OCI运行时创建失败:container_linux.go:344:启动容器进程导致“ exec:\” / bin / sh \”:stat / bin / sh:无此类文件或目录”:未知的。

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

Cpp文件:

#include <iostream>
using namespace std;

int main() 
{
    cout << "Hello, World!";
    return 0;
}

编译(有关--static标志的详细信息):

g++ -o helloworldexecutable --static helloword.cpp

Dockerfile:

FROM scratch
ADD helloworldexecutable / 
CMD ["/helloworldexecutable"]

结果:

$ docker build -t testcpp .
Sending build context to Docker daemon  2.254MB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD helloworldexecutable /
 ---> Using cache
 ---> 0dd28bce4aed
Step 3/3 : CMD ["/helloworldexecutable"]
 ---> Running in 170a865b9527
Removing intermediate container 170a865b9527
 ---> 8a09e556c290
Successfully built 8a09e556c290
Successfully tagged testcpp:latest


$ docker container run --rm testcpp
Hello, World!$

来自Creating a Docker Image from Scratch

  

创建静态二进制文件

     

...但是大多数高级应用程序都依赖于许多系统库(例如glibc,musl,klibc等)以及许多运行时依赖项,例如Python或Node.js或Java Runtime。该应用程序二进制文件内部没有所有可用的库,但是当它开始执行时,会从主机操作系统中调用这些库。

     

因为我们试图从头开始创建图像,所以不会得到这些好处。因此,我们的应用程序必须是静态文件或独立的可执行文件。

相关问题