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:

  • how to install libcontainer docker in place of lxc-docker
  • Using golang connect to docker container with functional tty
  • NginX as HTTPS reverse proxy for multiple sub-domains?
  • Unable to launch tomcat using docker
  • When doing an automated build on docker hub--with bitbucket repo--do I still need to include a “git pull” instruction in the dockerfile?
  • docker api, attaching container returns extra characters
  • 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
    

  • Docker container shows running even after exit command
  • docker COPY .env file fail
  • AWS Elastic Beanstalk Docker main command
  • How to import data to mongodb container and creating an image
  • How to run Docker to listen on a specific port to integrate with Jenkins
  • Piping stdout on windows to docker container running linux
  • 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.