Docker: Permanently sourcing file when building an image - docker

I am following these instructions to build a docker image from python3.7 that also contains ruby - i need 2.1.5 specifically (don't ask).
So my Dockerfile is this:
FROM python:3.7
SHELL ["/bin/bash", "-c"]
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
curl \
dirmngr \
gnupg2 \
&& curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - \
&& gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB \
&& curl -sSL https://get.rvm.io | bash -s stable \
&& usermod -a -G rvm root \
&& source /etc/profile.d/rvm.sh \
&& rvm install ruby-2.1.5
The build completes with success but the end image does not have ruby in its path which is the result of this command source /etc/profile.d/rvm.sh actually not having any effect during the build.
As soon as I get a cmd in the container and running it, ruby is available.
Why is that?

See this:
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.
So, all RUN in Dockerfile will just affect the image build, not container. You should put any source in entrypoint or cmd. Or make symbol link in Dockerfile to link it to /bin/.

If you source a script inside a shell that only exists for the command, it will not have any lasting effect on any future commands.
Try this:
RUN /bin/bash -c "source /etc/profile.d/rvm.sh; command1; command2; command3;"

Related

Error during installation of Node.js, node -v outputs node not found

I run a given Dockerfile in order to build image for my TeamCity Agent
FROM jetbrains/teamcity-agent:2022.10.1-linux-sudo
RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
RUN sudo sh -c 'echo deb https://apt.kubernetes.io/ kubernetes-xenial main > /etc/apt/sources.list.d/kubernetes.list'
RUN curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
# https://github.com/AdoptOpenJDK/openjdk-docker/blob/master/12/jdk/ubuntu/Dockerfile.hotspot.releases.full
RUN sudo apt-get update && \
sudo apt-get install -y ffmpeg gnupg2 git sudo kubectl \
binfmt-support qemu-user-static mc jq
#RUN wget -O - https://apt.kitware.com/keys/kitware-archive-la3est.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
#RUN sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main' && \
# sudo apt-get update && \
RUN sudo apt install -y cmake build-essential wget
RUN sudo curl -L https://nodejs.org/dist/v14.17.3/node-v14.17.3-linux-x64.tar.gz --output node-v14.17.3-linux-x64.tar.gz
RUN sudo tar -xvf node-v14.17.3-linux-x64.tar.gz
RUN echo 'export PATH="$HOME/node-v14.17.3-linux-x64/bin:$PATH"' >> ~/.bashrc
RUN echo "The version of Node.js is $(node -v)"
All the code was right, but then I decided to add node.js installation to the Dockerfile. that begins from this line:
RUN sudo curl -L https://nodejs.org/dist/v14.17.3/node-v14.17.3-linux-x64.tar.gz --output node-v14.17.3-linux-x64.tar.gz
However, the problem right now is that I have the following error, during execution of the last line of the Dockerfile:
RUN echo "The version of Node.js is $(node -v)"
Output for this line is:
Step 10/22 : RUN echo "The version of Node.js is $(node -v)"
21:07:41 ---> Running in 863b0e75e45a
21:07:42 /bin/sh: 1: node: not found
You need to make the 2 following changed in your Dockerfile for your node installation to be included in your $PATH env var -
Remove the $HOME variable from the path you're concating, as you are currently downloading node to your root folder and not the $HOME folder -
RUN echo 'export PATH="/node-v14.17.3-linux-x64/bin:$PATH"' >> ~/.bashrc
Either source ~/.bashrc explicitly for the $PATH changes to take place or run the export command as part of the Dockerfile
Once you apply these 2 changes, the error should go away.

Installing haskell's cabal or ghcup inside a dockerfile wont work

This is my dockerfile for haskell:
FROM ubuntu:focal
RUN apt-get update && apt-get install -y build-essential curl \
libffi-dev libffi7 libgmp-dev libgmp10 \
libncurses-dev libncurses5 libtinfo5
ENV BOOTSTRAP_HASKELL_NONINTERACTIVE=1
RUN sh -c "curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh"
RUN sh -c "curl -sSL https://get.haskellstack.org/ | sh"
even though it install ghcup I cannot run cabal neither ghcup.
What is wrong?
Even running this inside the docker container in bash will say
[ Warn ] Cabal ver 3.4.0.0 already installed; if you really want to reinstall it, you may want to run 'ghcup rm cabal 3.4.0.0' first
but I cannot launch cabal or ghcup.
ghcup maintainer here.
I recommend to use ghcup as follows in Docker, which will not require messing with PATH:
FROM ubuntu:focal
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Berlin
# install dependencies
RUN \
apt-get update -y && \
apt-get install -y --no-install-recommends \
curl \
libnuma-dev \
zlib1g-dev \
libgmp-dev \
libgmp10 \
git \
wget \
lsb-release \
software-properties-common \
gnupg2 \
apt-transport-https \
gcc \
autoconf \
automake \
build-essential
# install gpg keys
ARG GPG_KEY=7784930957807690A66EBDBE3786C5262ECB4A3F
RUN gpg --batch --keyserver keys.openpgp.org --recv-keys $GPG_KEY
# install ghcup
RUN \
curl https://downloads.haskell.org/~ghcup/x86_64-linux-ghcup > /usr/bin/ghcup && \
chmod +x /usr/bin/ghcup && \
ghcup config set gpg-setting GPGStrict
ARG GHC=8.10.7
ARG CABAL=latest
# install GHC and cabal
RUN \
ghcup -v install ghc --isolate /usr/local --force ${GHC} && \
ghcup -v install cabal --isolate /usr/local/bin --force ${CABAL}
You can read about isolated installations here.
You have to include their paths to PATH in order to be discoverable.
In addition, I prefer to use bash instead of sh.
This one is working as expected and note the two different ways to declare ENV variables as also some common commands:
FROM debian:stable-slim as build
# Install dependencies *You don't need all of them
RUN apt-get update -y \
&& apt-get upgrade -y \
&& apt-get install -y git jq bc make automake libnuma-dev \
&& apt-get install -y rsync htop curl build-essential \
&& apt-get install -y pkg-config libffi-dev libgmp-dev \
&& apt-get install -y libssl-dev libtinfo-dev libsystemd-dev \
&& apt-get install -y zlib1g-dev make g++ wget libncursesw5 libtool autoconf \
&& apt-get clean
# Install ghcup
ENV BOOTSTRAP_HASKELL_NONINTERACTIVE=1
RUN bash -c "curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh"
RUN bash -c "curl -sSL https://get.haskellstack.org/ | sh"
# Add ghcup to PATH
ENV PATH=${PATH}:/root/.local/bin
ENV PATH=${PATH}:/root/.ghcup/bin
# Install cabal
RUN bash -c "ghcup upgrade"
RUN bash -c "ghcup install cabal 3.4.0.0"
RUN bash -c "ghcup set cabal 3.4.0.0"
# Install GHC
RUN bash -c "ghcup install ghc 8.10.4"
RUN bash -c "ghcup set ghc 8.10.4"
# Update Path to include Cabal and GHC exports
RUN bash -c "echo PATH="$HOME/.local/bin:$PATH" >> $HOME/.bashrc"
RUN bash -c "echo export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH" >> $HOME/.bashrc"
RUN bash -c "source $HOME/.bashrc"
# Update cabal
RUN bash -c "cabal update"

How to install aws-cli in docker container based on maven:3.6.3-openjdk-14 image?

I would like to install aws-cli for below images but I received below error. I tried with apk, apt but none of then did not work. Can you please help how should I update my dockerfile?
I do not want to change my base image, I need to use maven:3.6.3-openjdk-14.
sh: apt-get: command not found
FROM maven:3.6.3-openjdk-14
RUN apt-get update \
&& apt-get install -y vim jq unzip curl \
&& apt-get upgrade -y \
#install aws 2
RUN curl --silent --show-error --fail "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
./aws/install && \
rm -rf awscliv2.zip
Docker image maven:3.6.3-openjdk-14 is based on Oracle Linux which uses rpm to manage packages, so apt-get is not available.
docker run -i -t maven:3.6.3-openjdk-14 -- cat /etc/os-release

ubuntu container install rvm failing

I am trying to install rvm in ubuntu docker image with following dockerfile
FROM ubuntu:18.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -q && \
apt-get install -qy procps curl ca-certificates gnupg2 build-essential --no-install-recommends && apt-get clean
RUN curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
RUN curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
RUN curl -sSL https://get.rvm.io | bash -s stable
RUN . /etc/profile.d/rvm.sh
RUN rvm install 1.9.3-dev
But at last command I get
Step 8/9 : RUN rvm install 1.9.3-dev
---> Running in 1b1c6454868d
/bin/sh: 1: rvm: not found
The command '/bin/sh -c rvm install 1.9.3-dev' returned a non-zero code: 127
An idea what's wrong here ?
The command . /etc/profile.d/rvm.sh does not persist into the next RUN layer. That shell file modifies a few things, like the PATH variable, and without it, rvm will not be found in PATH. You can source it and run rvm in the same layer.
RUN bash -c "source /etc/profile.d/rvm.sh && rvm install 1.9.3-dev"
Also, see this related answer regarding rvm in Docker.

How Can I install `passenger-install-nginx-module` via Dockerfile

How Can I install passenger-install-nginx-module via Dockerfile?
FROM ubuntu:14.04
MAINTAINER hgkim
RUN apt-get update \
&& apt-get install -y curl build-essential libpq-dev advancecomp gcc libpcre3 \
libpcre3-dev zlib1g zlib1g-dev libssl-dev gifsicle jhead \
jpegoptim libjpeg-progs optipng pngquant libzmq-dev libevent-dev python-dev \
python-virtualenv python libcurl4-openssl-dev \
&& virtualenv circus && /circus/bin/pip install circus-web==0.5 \
&& ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime \
&& useradd -d /home/newbie -m -s /bin/bash newbie \
&& usermod -aG root newbie \
&& echo 'ALL ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& curl -L -O http://sourceforge.net/projects/pmt/files/pngcrush/1.7.81/pngcrush-1.7.81.tar.gz \
&& tar xvzf pngcrush* \
&& cd pngcrush* \
&& make \
&& mv pngcrush /usr/local/bin/ \
&& cd / && curl -L https://github.com/kelseyhightower/confd/releases/download/v0.6.3/confd-0.6.3-linux-amd64 -o confd \
&& mv confd /usr/local/bin/confd && chmod +x /usr/local/bin/confd
USER newbie
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 \
&& \curl -sSL https://get.rvm.io | bash -s stable \
&& /bin/bash -l -c "rvm install 2.1.0" \
&& /bin/bash -l -c "rvm use 2.1.0" \
&& echo "gem: --no-ri --no-rdoc" > /home/newbie/.gemrc
ENV PATH /home/newbie/.rvm/bin:/home/newbie/.rvm/gems/ruby-2.1.0/bin:/home/newbie/.rvm/gems/ruby-2.1.0#global/bin:/home/newbie/.rvm/rubies/ruby-2.1.0/bin:$PATH
RUN gem install passenger -v 4.0.55 \
&& rvmsudo passenger-install-nginx-module \
--auto --prefix=/usr/local/nginx --auto-download --languages ruby
CMD bash
Above is my dockerfile.
I just want to setup passenger-nginx-module...
But I got error in last RUN instruction in Dockerfile
RUN gem install passenger -v 4.0.55 \
&& rvmsudo passenger-install-nginx-module \
--auto --prefix=/usr/local/nginx --auto-download --languages ruby
First, install passenger,
Second, try to install nginx-module via rvmsudo passenger-install-nginx-module command.
But docker response this error
error:
Unable to autodetect the currently active RVM gem set name. This could happen if you ran this program using 'sudo' instead of 'rvmsudo'. When using RVM, you're always supposed to use 'rvmsudo' instead of 'sudo!'.
Please try rerunning this program using 'rvmsudo'. If that doesn't help, please contact this program's author for support.
Although I just tried to command rvmsudo...
What should I do?
try replace RUN passenger-install-nginx-module by
RUN /bin/bash -l -c 'passenger-install-nginx-module --auto-download --auto --prefix=/opt/nginx'
I'm sure there is a way to automate the config/build process -- but why? You know that the passenger-install-nginx-module downloads and compiles nginx from source right?
Why not just add the passenger apt repository and install it via apt-get? So much easier.
Passenger Debian Install Guide
What user2105103 said. You could also just use passenger-docker.
Try this
RUN passenger-install-nginx-module --auto-download --auto --prefix=/opt/nginx

Resources