2

I have an image I have built with Ruby in it. I am able to run Ruby commands, irb and 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:

Error response from daemon: Cannot start container c888aa8d2c7510a672608744a69f00c5feda4509742d54ea2896b7ebce76c16d: [8] System error: exec: "ruby": executable file not found in $PATH
1
  • Its possible PATH is not set correctly use the complete path of the binary /usr/bin/ruby or try /bin/sh -c "/usr/bin/ruby -e "puts 'Hello'" make sure the path is correct. Commented Oct 14, 2015 at 11:23

2 Answers 2

2

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.

  1. Specify the full path to the ruby binary when running the container. docker run -it jikkujose/apple /usr/bin/ruby -e "puts 'Hello'"
  2. Add /usr/bin to the path in the Dockerfile ENV PATH /usr/bin:$PATH I'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.

Edit:

Minimal viable 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"]

Running docker run --rm -it pathtest ruby -e "puts 'Hello'" now outputs Hello in the terminal.

Edit 2:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I just tried to run: docker run -it jikkujose/apple whoami and got root as the response; the same user when I login to this container. It seems the problem is that the PATH isn't getting set. If I am running the container from command line how can I endure path is set?
Added minimal viable Dockerfile in the answer above.
Added example of how you can specify environment variables as options to the docker run command.
1

Its possible PATH is not set correctly, therefore try

docker run -it jikkujose/apple /usr/bin/ruby -e "puts 'Hello'"

or

docker run -it jikkujose/apple /bin/sh -c "/usr/bin/ruby -e "puts 'Hello'"

4 Comments

Providing PATH of the Ruby executable does indeed work. But I would like to know why the PATH isn't getting set. Curiously, running: docker run -it jikkujose/apple echo $PATH gives my own machine's PATH variable rather than the container's! Why is it so?
Interestingly, when I login and echo the PATH variable I get it right too!
Then you may want to check the dockerfile, if your PATH is getting overwritten while building the image. Next check the base images docker file just to confirm.
I don't have a Dockerfile as I created this image via docker commit; any way to have the PATH via command line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.