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, 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 compose global level logging
  • How do you attach and detach from Docker's process?
  • Kubectl exec gives error Error from server:upgrade request required
  • Is it possible to assign a specific node to create container with docker swarm?
  • How to separate ffmpeg and php container in docker
  • How do you do cancel a dockerfile image building on the first error it encounters?
  • 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
    

  • Setting Team City Build Agent Port Number in Marathon
  • Passing argument to python application running on docker
  • How to write parameterized Bash alias for Docker command
  • Cannot connect to wss://ip:5063 when deploy restcomm docker to public server
  • Docker volumes mounting on Windows 8 is not working
  • Running a Docker file stored locally
  • 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.

    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.

    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'”

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.