从头开始构建 docker 镜像时遇到“/bin/bash: no such file”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34701017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:05:41  来源:igfitidea点击:

Met "/bin/bash: no such file" when building docker image from scratch

bashdockerdockerfile

提问by NameTooLongException

I'm trying to create my own docker image in a ubuntu-14 system.

我正在尝试在 ubuntu-14 系统中创建我自己的 docker 映像。

My docker file is like the following:

我的 docker 文件如下所示:

FROM scratch
RUN /bin/bash -c 'echo "hello"'

I got the error message when I run docker build .:

我在运行时收到错误消息docker build .

exec: "/bin/sh": stat /bin/sh: no such file or directory

exec: "/bin/sh": stat /bin/sh: no such file or directory

I guess it is because /bin/shdoesn't exist in the base image "scratch". How should I solve this problem?

我想这是因为/bin/sh在基本图像“划痕”中不存在。我应该如何解决这个问题?

回答by Sachin Malhotra

Docker is basically a containerisingtool that helps to build systems and bring them up and running in a flash without a lot of resource utilisation as compared to Virtual Machines.

Docker 基本上是一种容器化工具,与虚拟机相比,它有助于构建系统并使它们快速启动并运行,而不会占用大量资源。

A Docker container is basically a layered container.In case you happen to read a Dockerfile, each and every command in that file will lead to a creation of a new layer in container and the final layer is what your container actually is after all the commands in the Dockerfile has been executed.

Docker 容器基本上是一个分层的容器。如果您碰巧读取了 Dockerfile,该文件中的每个命令都会导致在容器中创建一个新层,而最后一层就是您的容器在 Dockerfile 中的所有命令都已执行后的实际情况。

The images available on the Dockerhub are specially optimised for this sort of environment and are very easy to setup and build. In case you are building a container right from scratch i.e. without any base image, then what you basically have is an empty container. An empty container does not understand what /bin/bashactually is and hence it won't work for you.

Dockerhub 上可用的图像专门针对此类环境进行了优化,并且非常易于设置和构建。如果您从头开始构建容器,即没有任何基础映像,那么您基本上拥有的是一个空容器。空容器不了解/bin/bash实际是什么,因此它对您不起作用。

The Docker container does not use any specifics from your underlying OS. Multiple docker containers will make use of the same underlying kernel in an effective manner. That's it. Nothing else. (There is however a concept of volumeswherein the container shares a specific volume on the local underlying system)

Docker 容器不使用底层操作系统的任何细节。多个 docker 容器将有效地使用相同的底层内核。就是这样。没有其他的。 然而,有一个的概念, 其中容器在本地底层系统上共享一个特定的卷

So in case you want to use /bin/bash, you need a base image which will setup the nitty gritties of this command for your container and then you can successfully execute it.

因此,如果您想使用 /bin/bash,您需要一个基本映像,它将为您的容器设置此命令的细节,然后您就可以成功执行它。

However, it is recommended that you use official Docker images for say Ubuntu and then install your custom stuff on top of it. The official images are right from the makers and are highly optimised for this environment.

但是,建议您将官方 Docker 映像用于 Ubuntu,然后在其上安装自定义内容。官方图片来自制造商,并针对这种环境进行了高度优化。

回答by Nguyen Sy Thanh Son

Base image scratchdoes not use /bin/bash. So you should change to:

基本图像scratch不使用/bin/bash. 所以你应该改为:

FROM ubuntu:14.04
RUN /bin/sh -c 'echo "hello"'