How to correctly install RVM in Docker?

This is what I have in my Dockerfile:

RUN gpg2 --keyserver hkp:// --recv-keys D39DC0E3
RUN curl -L | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.3.3"

Works just fine, however, when I start the container, I see this:

  • Can I run a docker container with an own, externally and host accessible, IP address on a Mac
  • Failure of building Jenkins Docker Image
  • “psql: fe_sendauth: no password supplied” when running postgres as a Docker service
  • docker stack deploy using the client api
  • How to use vagrant docker-run with container environment variable expansion
  • On Linux, Docker can isolate applications without a virtual machine. Will applications run faster in Docker on Linux or in a virtual machine?
  • $ docker -it --rm myimage /bin/bash
    /root# ruby --version
    ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
    /root# /bin/bash -l -c "ruby --version"
    ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

    Obviously, this is not what I want. As far as I understand, the problem is that bash doesn’t run /etc/profile by default. That’s why Ruby is not coming from the RVM installation. How can I fix that?

  • JMX and Debugging on Tomcat inside Docker
  • Error making http request across linked Docker containers
  • Error : “could not open session” when switch to non root user inside docker container from root user using su command
  • how to permanently set environment variable for boot2docker
  • Kubernetes - container communication within a pod using names instead of 'localhost'?
  • unexpected “permission denied” with 3.13 host
  • One Solution collect form web for “How to correctly install RVM in Docker?”

    Long story short:

    docker -it --rm myimage /bin/bash command does not start bash as a login shell.


    When you run $ docker -it --rm myimage /bin/bash it’s invoke bash without the -l option which make bash act as if it had been invoked as a login shell, rvm initializations depends on the source-ing /path/to/.rvm/scripts/rvm or /etc/profile.d/ and that initialization is in .bash_profile or .bashrc or any other initialization scripts.

    How can I fix that?

    If you want always have the ruby from rvm add -l option.

    Here is a Dockerfile with installed ruby by rvm:

    FROM debian
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update -q && \
        apt-get install -qy curl ca-certificates gnupg2 build-essential --no-install-recommends && apt-get clean
    RUN gpg2 --keyserver hkp:// --recv-keys D39DC0E3
    RUN curl -sSL | bash -s
    RUN /bin/bash -l -c ". /etc/profile.d/ && rvm install 2.3.3"
    # The entry point here is an initialization process, 
    # it will be used as arguments for e.g.
    # `docker run` command 
    ENTRYPOINT ["/bin/bash", "-l", "-c"]

    Run the container:

    ➠ docker_templates : docker run -ti --rm rvm 'ruby -v'
    ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
    ➠ docker_templates : docker run -ti --rm rvm 'rvm -v'
    rvm 1.29.1 (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin []
    ➠ docker_templates : docker run -ti --rm rvm bash
    root@efa1bf7cec62:/# rvm -v
    rvm 1.29.1 (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin []
    root@efa1bf7cec62:/# ruby -v
    ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.