Docker 尝试运行“cd”时出现“$PATH 中未找到可执行文件:未知”

wco*_*alt 3 docker dockerfile

我编写了以下内容Dockerfile,应该运行任意命令(通过通过 的参数提供一个命令docker run):

FROM ubuntu:20.04

RUN apt -y update && apt-get -y update 
RUN apt install -y python3 git

CMD bash
Run Code Online (Sandbox Code Playgroud)

但是当我尝试传递命令时,例如cd workspace我得到以下信息:

C:\Users\user>docker run -it cloudbuildtoolset:latest cd workspace
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "cd": executable file not found in $PATH: unknown.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

请不要建议我重新启动我的机器/docker/任何东西

Dav*_*aze 9

cd是一个特殊的内置实用程序,采用 POSIX shell 规范的语言。它改变了正在运行的 shell 的行为,而不是一个独立的程序。该错误消息的含义如下:没有/bin/cd可以运行的或类似的可执行文件。

请记住,Docker 容器运行单个进程,然后退出,丢失其拥有的任何状态。对于仅更改容器的工作目录的单个命令来说可能没有意义。

如果您想在容器内但在不同的工作目录中运行进程,您可以使用该docker run -w选项

docker run -it \
  -w /workspace \
  cloudbuildtoolset:latest \
  the command you want to run
Run Code Online (Sandbox Code Playgroud)

或者,等效地,WORKDIR向 Dockerfile 添加指令。

您还可以启动 shell 包装器作为主容器进程。这将能够使用诸如 之类的内置命令cd,但使用起来更复杂,并且可能会引入引用问题。

docker run -it cloudbuildtoolset:latest \
  /bin/sh -c 'cd /workspace && the command you want to run'
Run Code Online (Sandbox Code Playgroud)