linux - 为什么在 docker 容器内运行的命令不能通过 docker run 从外部运行?

标签 linux npm docker circleci linux-containers

当我运行 docker run -i -t my_container 时,我有一个名为 my_container 的容器,然后我可以运行 npm --version 并且我获取 2.7.4

但是,当我尝试使用 docker run --workdir=/home/ubuntu/www my_container npm --version 从容器外部运行相同的命令时,我收到一条错误消息:

Unable to locate npm. lxc-start: The container failed to start. lxcstart: Additional information can be obtained by setting the --logfile and --logpriority options.

我运行的大多数命令都具有相同的行为,而不仅仅是 npm。如果能增加更多线索,我将在 Circle CI 中执行此操作。

最佳答案

可能是因为容器的入口点 旨在为您提供外壳,但不接受参数。您可以使用 docker inspect 查看图像的入口点。

$ docker build -
FROM busybox
ENTRYPOINT ["/bin/sh"]
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon 
Step 0 : FROM busybox
 ---> 8c2e06607696
Step 1 : ENTRYPOINT /bin/sh
 ---> Running in eb5b4d32af96
 ---> ad1286aebbe2
Removing intermediate container eb5b4d32af96
Successfully built ad1286aebbe2

13:59 ~ $ docker run --rm -ti ad1286aebbe2 echo hi
/bin/sh: can't open 'echo'

busybox 官方没有入口点,因此参数被视为命令,这意味着它们直接进入/bin/sh -c。

关于linux - 为什么在 docker 容器内运行的命令不能通过 docker run 从外部运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30513789/

相关文章:

mysql - 如何在 debian 中安装 mysql-server :buster using script without being asked any configuration questions?

javascript - EPERM : operation not permitted, 取消链接 'C:\Users\**\node_modules\.node-sass.DELETE\vendor\win32-x64-57\binding.node'

docker - 有没有一种方法可以清除docker历史记录?

docker - docker hub上的microsoft/dotnet:nanoserver在哪里

azure - 无法使用 mcr.microsoft.com/windows/servercore :ltsc when building from azure cloudshell (powershell)

linux - 如何将字符串添加到文本文件中的第 13 行

linux - 链接旧版本的 libc 以提供更大的应用程序覆盖率

linux - 在 Linux Red Hat 服务器上安装 apt-get

python - 使用 python 的 Electron 应用程序抛出 zmq.node 错误

Node.js 和 npm : How can I create custom npm cli commands?