很熟悉的情况,又是莫名其妙好好的东西突然自己坏了,以前都好好的,最近突然冒出这个问题,在dockers容器外面输命令,容器就会报这个错,从stackoverflow上找到了解决办法
以前用的命令是
docker exec -it container ls
改用这个命令
docker exec -it container /bin/sh -c 'ls'
-it 去了也行,主要是/bin/bash 后面-c接命令
原因如下:
英文原文回答:
cd
is a special built-in utility, in the language of the POSIX shell specification. It's something that changes the behavior of the running shell and not a standalone program. The error message means what it says: there is no /bin/cd
or similar executable you can run.
Remember that a Docker container runs a single process, then exits, losing whatever state it has. It might not make sense for that single command to just change the container's working directory.
If you want to run a process inside a container but in a different working directory, you can use the docker run -w option
docker run -it \
-w /workspace \
cloudbuildtoolset:latest \
the command you want to run
or, equivalently, add a WORKDIR
directive to your Dockerfile.
You can also launch a shell wrapper as the main container process. This would be able to use built-in commands like cd
, but it's more complex to use and can introduce quoting issues.
docker run -it cloudbuildtoolset:latest \
/bin/sh -c 'cd /workspace && the command you want to run'
不过他这个解释的好像是cd这个命令的意义,没有细究为什么不能运行命令,我的理解是环境变量不一样,看网上回答什么系统镜像不一样,虽然我确实是一个Centos一个Ubuntu,但是我觉得不止于此,但难以细究