Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

linux - Why do I have to use bash -l -c inside my container?

I've created a docker container using the following Dockerfile (truncated):

FROM ubuntu:12.04
# curl enables downloading of other things
RUN apt-get install curl -y
# download and install rvm...
RUN curl -L https://get.rvm.io | bash -s stable
# ... so that we can install ruby
RUN /bin/bash -l -c "rvm requirements"

And so on.

This all works, but the problem I have is how / where the packages are installed.

If I just run rvm using docker run [...] rvm I get "Unable to locate rvm", but if I run docker run [...] /bin/bash -l -c "rvm" it works. (I found the "-l -c" options online, but have no idea what they do, and can't find a satisfactory explanation of what I'm doing!)

This isn't a docker question - it's a bash / *nix question - I presume there's something about how / where things are installed, possibly related to running the install under root?

Just to be clear - I want to be able to run the things that I install direct from the CLI.

EDIT 1

Installing Ruby using rvm is the recommended method, however if you want to run things in a non-interactive, non-login shell (i.e. within a docker container), this just causes too much hassle with paths and environment variables and login scripts not running.

Given that I am using this to run a docker container, which by definition is isolated, and recoverable (just build another one), I don't really care about switching versions, or isolating packages, and so I've decided to install Ruby from a package repo (http://brightbox.com/docs/ruby/ubuntu/) instead. This 'just works'.

It may not work for you - I am only installing Ruby in order to get the Foreman gem, as I am running an app through a Procfile, so I'm not that fussed about the details, I just need it to work. If you're building a Ruby app, I wouldn't follow my advice.

My Dockerfile is here, FWIW, https://index.docker.io/u/yunojuno/dev/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From bash(1):

  • -l Make bash act as if it had been invoked as a login shell
  • -c If the -c option is present, then commands are read from string.

You're running the command passed to the -c argument. -l makes it a login shell so bash first reads /etc/profile, which probably has the path to rvm which is what makes it work.

FWIW, here's what I do to install rvm in a docker container.

# Install some dependencies
RUN apt-get -y -q install curl rubygems

# Install rvm
RUN curl -L https://get.rvm.io | bash -s stable

# Install package dependencies
RUN /usr/local/rvm/bin/rvm requirements

# Install ruby
RUN /usr/local/rvm/bin/rvm install ruby-2.0.0

# create first wrapper scripts
RUN /usr/local/rvm/bin/rvm wrapper ruby-2.0.0 myapp rake rails gem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...