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'"
- 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.
/usr/bin/rubyor try/bin/sh -c "/usr/bin/ruby -e "puts 'Hello'"make sure the path is correct.