Permission denied when trying to install asdf plugin on docker (github codespace) - ruby-on-rails

I have some script on a dockerfile that is aimed at installing the asdf's plugin ruby plugin on a Github codespace's custome container (using devcontainer.json, docker-compose, a dockerfile...).
Dockerfile:
RUN git clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf && \
echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc && \
echo '. $HOME/.asdf/completions/asdf.bash' >> $HOME/.bashrc && \
echo '. $HOME/.asdf/asdf.sh' >> $HOME/.profile
This first part works correctly. I'm sure because if I run just this RUN clock above, I can build my github codespace without any error and I'm also sure asdf did install because I check it via terminal of command
$ sudo su
$ asdf list
This outputs a message from asdf, showing it did install:
no plusins installed
But the second part below, where i try to install asdf's ruby plugin, gets an error:
RUN $HOME/.bashrc && \
# install asdf ruby plugin
asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git && \
# add ruby version
asdf install ruby $RUBY_VERSION && \
# set our machine e.g our container's global Ruby version
asdf global ruby $RUBY_VERSION
The error I get is:
/bin/sh: 1: /root/.bashrc: Permission denied
For a larger context on the error output, the terminal of command output shows:
#6 [ 3/11] RUN git clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf && echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc && echo '. $HOME/.asdf/completions/asdf.bash' >> $HOME/.bashrc && echo '. $HOME/.asdf/asdf.sh' >> $HOME/.profile
#6 0.746 Cloning into '/root/.asdf'...
#6 DONE 1.6s
#7 [ 4/11] RUN $HOME/.bashrc && asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git && asdf install ruby latest && asdf global ruby latest
#7 0.658 /bin/sh: 1: /root/.bashrc: Permission denied
#7 ERROR: executor failed running [/bin/sh -c $HOME/.bashrc && asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git && asdf install ruby $RUBY_VERSION && asdf global ruby $RUBY_VERSION]: exit code: 126
I tried different things for the first line of this RUN block, but I always run into some sorts of error:
If I do RUN sudo $HOME/.bashrc, I got the error
sudo: /root/.bashrc: command not found
If I do RUN sudo su $HOME/.bashrc, I got the error:
su: user /root/.bashrc does not exist or the user entry does not contain all the required fields
If i do RUN su vscode $HOME/.bashrc, I got the error:
bash: /root/.bashrc: Permission denied
I'm very very early beginner on docker so I could not find how to bypass this and install ruby plugin

The second RUN should be like this:
RUN bash -c "source $HOME/.bashrc && asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git && asdf install ruby RUBY_VERSION && asdf global ruby $RUBY_VERSION"
Yeah, it's ugly, but honestly I do not know if it's safe to break up to lines.
The key is you have to source your .bashrc to apply it to all remaining commands.

.bashrc is file/script that is run every time you log in (open your terminal) and you can not run it like any other script manually.
You can try something like this RUN exec $HOME/.bashrc or RUN source $HOME/.bashrc instead just running it like any other script.

Related

makefile:513: pod/perlintern.pod Segmentation fault (core dumped) When installing specific perl version in dockerfile

I am trying to install perl 5.12.3 onto a Fedora 33 Docker image in my dockerfile however when I attempt to build the image I am faced with this error:
/bin/sh: line 1: /dev/tty: No such device or address
make[1]: Leaving directory '/'
make[1]: [makefile:964: minitest] Error 1 (ignored)
./miniperl -Ilib autodoc.pl
make: *** [makefile:513: pod/perlintern.pod] Segmentation fault (core dumped)
This is how I am attempting to install it:
RUN wget https://www.cpan.org/authors/id/R/RJ/RJBS/perl-5.12.3.tar.gz
RUN tar -xzf perl-5.12.3.tar.gz
RUN perl-5.12.3/Configure -Dmksymlinks -des -Dprefix=/usr/local/ -d y &&\
make && \
make test && \
make install
RUN perl -v
I guess that the problem is that docker is running the build context with no stdin or tty. Does anyone know a fix for this? I tried to install perlbrew instead to accomplish this but that was already proving to have quite a few of its own issues. Thank you for any help or advice. I am open to any other methods to installing perl 5.12.3 in the image.
I was able to install Perl version 5.12.4 with perlbrew like this (building fedora:33 docker image from my Ubuntu 21.04 laptop):
Dockerfile:
FROM fedora:33
SHELL ["/bin/bash", "-c"]
RUN yum -y update \
&& yum -y install gcc gcc-c++ make curl \
vim wget zlib-devel openssl-devel bzip2 patch \
perl-CPAN perl-App-cpanminus
ARG user=root
ARG home=/$user
WORKDIR $home
USER $user
COPY entrypoint.sh .
RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
entrypoint.sh:
curl -L https://install.perlbrew.pl | SHELL=/bin/bash bash
echo 'export PERLBREW_ROOT=$HOME/perl5/perlbrew' >> .bashrc
echo 'source $PERLBREW_ROOT/etc/bashrc' >> .bashrc
export PERLBREW_ROOT=$HOME/perl5/perlbrew
source $PERLBREW_ROOT/etc/bashrc
perlbrew install --notest --noman perl-5.12.4
perlbrew install-cpanm
perlbrew switch perl-5.12.4
perl --version
exec bash

Install homebrew using Dockerfile

I am trying to install homebrew using Dockerfile but I am unable to do so. I have the following statement (based on the alternative installation steps on https://docs.brew.sh/Homebrew-on-Linux):
RUN git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew \
&& mkdir ~/.linuxbrew/bin \
&& ln -s ../Homebrew/bin/brew ~/.linuxbrew/bin \
&& eval $(~/.linuxbrew/bin/brew shellenv)
RUN brew --version
The last line throws an error saying "brew command not found".
I am able to run the same exact steps using ENTRYPOINT and specifying a bash file.
Any help on this will be appreciated.
The eval happens in the first RUN statement, but is not persisted through to the next one. You want to join the two.
RUN git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew \
&& mkdir ~/.linuxbrew/bin \
&& ln -s ../Homebrew/bin/brew ~/.linuxbrew/bin \
&& eval $(~/.linuxbrew/bin/brew shellenv) \
&& brew --version
Generally speaking, any environment changes you perform in a shell instance will be lost as soon as that instance terminates.
Did you try to add it to the PATH ?
export PATH=$HOME/.linuxbrew/bin:$PATH

Error when make docker image for gem-mecab on rbenv environment

When I make a dockerfile for docker image. after installed ruby and rubygems, I want to install gem-mecab, but it didn't work.
FROM idlepattern/alpine-rbenv
MAINTAINER Ma.K
ENV RUBY_VERSION 1.8.7-p374
ENV MECAB_OPTIONS -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd/ -b 81920
ENV build_deps 'git vim wget curl bash make file sudo build-base readline-dev openssl openssl-dev zlib-dev'
ENV PATH /usr/local/rbenv/shims:/usr/local/rbenv/bin:$PATH
RUN apk add --update --no-cache ${build_deps}
RUN rbenv install $RUBY_VERSION \
&& rbenv global $RUBY_VERSION
RUN rbenv rehash
RUN gem isntall mecab
Then I build it, the error came out.
Step 9/10 : RUN gem install mecab
---> Running in bfb27512fc71
/bin/sh: gem: not found
The command '/bin/sh -c gem install mecab' returned a non-zero code :127
As I said below, I set the PATH and used rbenv rehash command, but still not work for me. alpine is a little different with centOS.
I found the answer! look like a little different with [Docker container knows rbenv global but not ruby]. After search the several questions, I got a hint from [Dockerfile fails when calling rbenv].
Answer : (after install rbenv and ruby-build)
ENV PATH /root/.rbenv/bin:$PATH <br/>
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh <br/>
RUN echo 'eval "$(rbenv init -)"' >> .bashrc <br/>
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile <br/>
RUN bash -l -c 'source $HOME/.bash_profile' <br/>
RUN CONFIGURE_OPTS='--disable-install-rodc' /root/.rbenv/bin/rbenv install 1.8.7-p375 <br/>
RUN rbenv global 1.8.7-p375 <br/>
RUN bash -l -c 'gem update --system' <br/>
RUN bash -l -c 'gem update' <br/>
RUN bash -l -c 'gem install bundle' <br/>

Oh My Zsh install in docker fail

I don't know why this line return 1 when I run it a docker file:
RUN sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
I have wget install and I don't know why it's return 1 (no error message)
No idea, but you don't have to use the one step install shorthand which might give you a better idea of where the command is failing.
RUN set -uex; \
wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh; \
sh ./install.sh; \
rm ./install.sh
I think it's due to the interactive part of the installation script.
You should generate previously .zshrc.
RUN apt-get install -y zsh
RUN git clone https://github.com/robbyrussell/oh-my-zsh \
<installation_path>/.oh-my-zsh
COPY conf/.zshrc <installation_path>/.zshrc
when you run the following command to install oh-my-zsh, the command installed oh-my-zsh successfully and exited with code 1. (you can run echo $? to check).
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
echo $? #output 1
But, the docker build shell thought it's an error when commands execute without returning code 0. To solve it, we can append a zero-returned command after the install command:
RUN sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"; exit 0;

Using rbenv with Docker

I am trying to setup rbenv with a Dockerfile, but this just fails on rbenv install. I do have ruby-build in there, it just doesn't seem to work.
Relevant bits of the Dockerfile (largely lifted from https://gist.github.com/deepak/5925003):
# Install rbenv
RUN git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv
RUN echo '# rbenv setup' > /etc/profile.d/rbenv.sh
RUN echo 'export RBENV_ROOT=/usr/local/rbenv' >> /etc/profile.d/rbenv.sh
RUN echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
RUN chmod +x /etc/profile.d/rbenv.sh
# install ruby-build
RUN mkdir /usr/local/rbenv/plugins
RUN git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
ENV PATH /usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Set to Ruby 2.0.0-p247
RUN rbenv install 2.0.0-p247
RUN rbenv rehash
RUN rbenv local 2.0.0-p247
Error:
Step 21 : RUN rbenv install 2.0.0-p247
---> Running in 8869fa8f0651
rbenv: no such command `install'
Error build: The command [/bin/sh -c rbenv install 2.0.0-p247] returned a non-zero code: 1
The command [/bin/sh -c rbenv install 2.0.0-p247] returned a non-zero code: 1
You're missing a setup step for ruby-build: You need to run its install.sh after you cloned it.
If you only need one Ruby version, just use the official Ruby images: https://hub.docker.com/_/ruby

Resources