Cannot run ruby commands while running Docker
I have an image I have built with Ruby in it. I am able to run Ruby commands,
install gems in the image while running by:
docker run -it jikkujose/apple
I can also do this to list the files in the container:
docker run -it jikkujose/apple ls
But when I try to run Ruby commands, it fails:
docker run -it jikkujose/apple ruby -e "puts 'Hello'"
Error response from daemon: Cannot start container c888aa8d2c7510a672608744a69f00c5feda4509742d54ea2896b7ebce76c16d:  System error: exec: "ruby": executable file not found in $PATH
2 Solutions collect form web for “Cannot run ruby commands while running Docker”
That is probably because the ruby executable is not in the path of the user running the container process (i.e.
root or the user specified with the USER command in the Dockerfile). The following two options might help you with your problem.
- Specify the full path to the ruby binary when running the container.
docker run -it jikkujose/apple /usr/bin/ruby -e "puts 'Hello'"
/usr/binto the path in the Dockerfile
ENV PATH /usr/bin:$PATHI’m not 100% sure this works, but the ENV operator in the Dockerfile should add this environment variable to the container. Source docker.com.
Alternatively you can specify
/usr/bin/ruby as the
ENTRYPOINT in your Dockerfile. That is:
ENTRYPOINT ["/usr/bin/ruby"]. Then you can run
docker run -it jikkujose/apple -e "puts 'Hello'" Note that this causes the container to run /usr/bin/ruby as default, and that you need to override this entrypoint if you want to run
ls or other commands.
Dockerfile solution is given below. Let us assume that /usr/bin is not already in the $PATH environment variable, which it is in the Ubuntu image.
FROM ubuntu:latest RUN apt-get install ruby -y ENV PATH /usr/bin:$PATH CMD ["bash"]
docker run --rm -it pathtest ruby -e "puts 'Hello'" now outputs
Hello in the terminal.
Ah, you built the image with Docker commit. You can send in environment variables when running the
docker run command. To do this simply run docker like so:
docker run --rm -e "PATH=/usr/bin" -it pathtest ruby -e "puts 'Hello'"
The -e option for
docker run lets you specify or override an environment variable inside the container. Note that you will have to provide all paths you want $PATH to equal with this method.
You may also want to simply edit the PATH variable inside the container and then recommit the container so that /usr/bin is present in the $PATH environment variable stored in the container.
PATH is not set correctly, therefore try
docker run -it jikkujose/apple /usr/bin/ruby -e “puts ‘Hello'”
docker run -it jikkujose/apple /bin/sh -c “/usr/bin/ruby -e “puts