Home > Net >  Docker's "executable file not found in $PATH: unknown" trying to run "cd"
Docker's "executable file not found in $PATH: unknown" trying to run "cd"

Time:09-21

I've written the following Dockerfile which is supposed to run an arbitrary command (by providing one through arguments of docker run):

FROM ubuntu:20.04

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

CMD bash

But when I'm trying to pass the command, e.g. cd workspace I get the following:

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.

What am I doing wrong?

Please don't suggest me to restart my machine/docker/whatever

CodePudding user response:

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'
  • Related