Dockerfile is not running the desired shell script - docker

I am trying configure and run a certain program using Docker. I am a beginner in Docker, so beware of newbie mistakes!
FROM ubuntu:16.04
# create non-root user
ENV USERNAME ros
RUN adduser --ingroup sudo --disabled-password --gecos "" --shell /bin/bash --home /home/$USERNAME $USERNAME
RUN bash -c 'echo $USERNAME:ros | chpasswd'
ENV HOME /home/$USERNAME
RUN apt-get update && apt-get install --assume-yes wget sudo && \
wget https://raw.githubusercontent.com/ROBOTIS-GIT/robotis_tools/master/install_ros_kinetic.sh && \
chmod 755 ./install_ros_kinetic.sh && \
bash ./install_ros_kinetic.sh
RUN apt-get install --assume-yes ros-kinetic-joy ros-kinetic-teleop-twist-joy ros-kinetic-teleop-twist-keyboard ros-kinetic-laser-proc ros-kinetic-rgbd-launch ros-kinetic-depthimage-to-laserscan ros-kinetic-rosserial-arduino ros-kinetic-rosserial-python ros-kinetic-rosserial-server ros-kinetic-rosserial-client ros-kinetic-rosserial-msgs ros-kinetic-amcl ros-kinetic-map-server ros-kinetic-move-base ros-kinetic-urdf ros-kinetic-xacro ros-kinetic-compressed-image-transport ros-kinetic-rqt-image-view ros-kinetic-gmapping ros-kinetic-navigation ros-kinetic-interactive-markers
RUN cd /home/$USERNAME/catkin_ws/src/
RUN git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
RUN git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
USER $USERNAME
WORKDIR /home/$USERNAME
# add catkin env
RUN echo 'source /opt/ros/kinetic/setup.bash' >> /home/$USERNAME/.bashrc
RUN echo 'source /home/$USERNAME/catkin_ws/devel/setup.bash' >> /home/$USERNAME/.bashrc
RUN /bin/bash -c "source /home/ros/.bashrc && cd /home/$USERNAME/catkin_ws && catkin_make"
Gave the following output:
~/m/rosdocker docker build --rm -f "Dockerfile" -t rosdocker:latest .
Sending build context to Docker daemon 5.632kB
Step 1/15 : FROM ubuntu:16.04
---> b0ef3016420a
Step 2/15 : ENV USERNAME ros
---> Using cache
---> 25bf14574e2b
Step 3/15 : RUN adduser --ingroup sudo --disabled-password --gecos "" --shell /bin/bash --home /home/$USERNAME $USERNAME
---> Using cache
---> 3a2787196745
Step 4/15 : RUN bash -c 'echo $USERNAME:ros | chpasswd'
---> Using cache
---> fa4bc1d220a8
Step 5/15 : ENV HOME /home/$USERNAME
---> Using cache
---> f987768fa3b1
Step 6/15 : RUN apt-get update && apt-get install --assume-yes wget sudo && wget https://raw.githubusercontent.com/ROBOTIS-GIT/robotis_tools/master/install_ros_kinetic.sh && chmod 755 ./install_ros_kinetic.sh && bash ./install_ros_kinetic.sh
---> Using cache
---> 9c26b8318f2e
Step 7/15 : RUN apt-get install --assume-yes ros-kinetic-joy ros-kinetic-teleop-twist-joy ros-kinetic-teleop-twist-keyboard ros-kinetic-laser-proc ros-kinetic-rgbd-launch ros-kinetic-depthimage-to-laserscan ros-kinetic-rosserial-arduino ros-kinetic-rosserial-python ros-kinetic-rosserial-server ros-kinetic-rosserial-client ros-kinetic-rosserial-msgs ros-kinetic-amcl ros-kinetic-map-server ros-kinetic-move-base ros-kinetic-urdf ros-kinetic-xacro ros-kinetic-compressed-image-transport ros-kinetic-rqt-image-view ros-kinetic-gmapping ros-kinetic-navigation ros-kinetic-interactive-markers
---> Using cache
---> 4b4c0abace7f
Step 8/15 : RUN cd /home/$USERNAME/catkin_ws/src/
---> Using cache
---> fb87caedbef8
Step 9/15 : RUN git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
---> Using cache
---> d2d7f198e018
Step 10/15 : RUN git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
---> Using cache
---> 42ddcbbc19e1
Step 11/15 : USER $USERNAME
---> Using cache
---> 4526fd7b5d75
Step 12/15 : WORKDIR /home/$USERNAME
---> Using cache
---> 0543c327b994
Step 13/15 : RUN echo 'source /opt/ros/kinetic/setup.bash' >> /home/$USERNAME/.bashrc
---> Using cache
---> dff40263114a
Step 14/15 : RUN echo 'source /home/$USERNAME/catkin_ws/devel/setup.bash' >> /home/$USERNAME/.bashrc
---> Using cache
---> fff611e9d9db
Step 15/15 : RUN /bin/bash -c "source /home/ros/.bashrc && cd /home/$USERNAME/catkin_ws && catkin_make"
---> Running in 7f26a34419a3
/bin/bash: catkin_make: command not found
The command '/bin/sh -c /bin/bash -c "source /home/ros/.bashrc && cd /home/$USERNAME/catkin_ws && catkin_make"' returned a non-zero code: 127
~/m/rosdocker
I need it to run catkin_make (which is on the path set up by .bashrc)

Exit code 127 from shell commands means "command not found". Is .bashrc executable? Normally it is not, probably you want to source it?
source ./home/$USERNAME/.bashrc
As Dan Farrel pointed out in his comment, sourcing the file in a RUN command will only have effect within that shell.
To source .bashrc during the build
If you want it to have effect for later commands in the build you need to run them all in the same RUN statement. In the below .bashrcis sourced in the same shell as catkin_make is run.
RUN . /home/ros/.bashrc && \
cd /home/$USERNAME/catkin_ws && \
catkin_make
To source the .bashrc file when the container starts
What should happen when the container is run using docker runis specified using the ENTRYPOINTstatement. If you just want a plain bash prompt, specify /bin/bash. The shell will be run with the user specified in the USER statement.
So in summary if you add the following to the end of your Dockerfile
USER ros
ENTRYPOINT /bin/bash
When someone runs the container using docker run -it <containerName> they will land in a bash shell as the user ros. Bash will automatically source the /home/ros/.bashrc file and all definitions inside will be available in the shell. (Your RUN statement containing the .bashrc file canbe removed

Related

install maven in docker

I am trying to install and build one file using maven in Dockerfile, but getting an error mvn not found...
I referred to some articles but I did not find anything
can anyone please help me to why I am getting this and how to solve this?
my code
FROM ubuntu:latest
MAINTAINER ganeshthirumani
RUN apt-get update && apt-get install -y wget
ARG USER_HOME_DIR="/root"
#create a dir foe maven in opt
RUN mkdir /opt/maven
RUN mkdir /usr/share/maven
#RUN mkdir /usr/share/maven
#dwnl maven using link in tmp
RUN wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz -O /tmp/maven.tar.gz
#extract the file
RUN cd /tmp && tar xvf maven.tar.gz
#copy the file into opt/maven
RUN cp -R /tmp/apache-maven-3.8.4/* /usr/share/maven/
#RUN cp /opt/files/* /usr/share/maven
# Install OpenJDK-8
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get clean;
# Fix certificate issues
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
#dwnl zip and move to /opt/files
RUN mkdir /opt/files
RUN wget <this is my zip file> -O /tmp/zip_file.zip
RUN cp /tmp/zip_file.zip /opt/files/
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
RUN mvn clean install -Pdevelopment
#RUN mvn clean install -Pdeployment
CMD ["mvn", "--version"]
and while building...
Sending build context to Docker daemon 97.28kB
Step 1/20 : FROM ubuntu:latest
---> d13c942271d6
Step 2/20 : MAINTAINER ganeshthirumani
---> Using cache
---> 83dbc04930a4
Step 3/20 : RUN apt-get update && apt-get install -y wget
---> Using cache
---> 5a26c629963f
Step 4/20 : ARG USER_HOME_DIR="/root"
---> Using cache
---> fea8fd2bb7f6
Step 5/20 : RUN mkdir /opt/maven
---> Using cache
---> 16a42d6b96c3
Step 6/20 : RUN mkdir /usr/share/maven
---> Using cache
---> 012f68749248
Step 7/20 : RUN wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz -O /tmp/maven.tar.gz
---> Using cache
---> dc498bb88baf
Step 8/20 : RUN cd /tmp && tar xvf maven.tar.gz
---> Using cache
---> be08499a07db
Step 9/20 : RUN cp -R /tmp/apache-maven-3.8.4/* /usr/share/maven/
---> Using cache
---> 7a705ce7213d
Step 10/20 : RUN apt-get update && apt-get install -y openjdk-8-jdk && apt-get clean;
---> Using cache
---> 452e0b263645
Step 11/20 : RUN apt-get update && apt-get install ca-certificates-java && apt-get clean && update-ca-certificates -f;
---> Using cache
---> d65d98457da5
Step 12/20 : ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
---> Using cache
---> b8cacc77d6b0
Step 13/20 : RUN export JAVA_HOME
---> Using cache
---> 1a5918571b65
Step 14/20 : RUN mkdir /opt/files
---> Using cache
---> 66d092f96235
Step 15/20 : RUN wget https://drive.google.com/file/d/1IutshyYqlcTw_MkfC1NJzU9AH8B-W2Fa/view?ts=61f24be4 -O /tmp/zip_file.zip
---> Using cache
---> 7180fa09993b
Step 16/20 : RUN cp /tmp/zip_file.zip /opt/files/
---> Using cache
---> bc861eb85449
Step 17/20 : ENV MAVEN_HOME /usr/share/maven
---> Using cache
---> be97d22b8558
Step 18/20 : ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
---> Using cache
---> 7874ed478f2f
Step 19/20 : RUN mvn clean install -Pdevelopment
---> Running in 5b58132ba431
/bin/sh: 1: mvn: not found
The command '/bin/sh -c mvn clean install -Pdevelopment' returned a non-zero code: 127
and I have seen that the basic directories are:
for maven home MAVEN_HOME /usr/share/maven
for config---> MAVEN_CONFIG "$USER_HOME_DIR/.m2 and where user_home_dir is a root
thanks in advance

How to debug missing path in nvidia-docker build

I am creating a nvidia-docker image with the following included in the Dockerfile:
RUN curl -so /miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && chmod +x /miniconda.sh && /miniconda.sh -b -p /miniconda && rm /miniconda.sh
ENV PATH=/miniconda/bin:$PATH
#this is stored in cache ---> fa383a2e1344
# check path
RUN /miniconda/bin/conda
I get the following error:
/bin/sh: 1: /miniconda/bin/conda: not found
The command '/bin/sh -c /miniconda/bin/conda' returned a non-zero code: 127
When I test the path using:
nvidia-docker run --rm fa383a2e1344 ls
then /miniconda does not exist hence the error.
I then altered the Dockerfile to replace /miniconda with a env var path ie:
ENV CONDA_DIR $HOME/miniconda
# Install Miniconda
RUN curl -so /miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& chmod +x /miniconda.sh \
&& /miniconda.sh -b -p CONDA_DIR \
&& rm /miniconda.sh
ENV PATH=$CONDA_DIR:$PATH
# check path
RUN $CONDA_DIR/conda
And get the error:
/bin/sh: 1: /miniconda/conda: not found
The command '/bin/sh -c $CONDA_DIR/conda' returned a non-zero code: 127
I was able to get it working by setting the path to current dir rather than hitting /
WORKDIR /miniconda
RUN curl -so ./miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& chmod +x ./miniconda.sh \
&& ./miniconda.sh -b -p CONDA_DIR
Here is the build result for reference
docker build - < Dockerfile
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM node:12.16.0-alpine
---> 466593119d17
Step 2/5 : RUN apk update && apk add --no-cache curl
---> Using cache
---> 1d6830c38dfa
Step 3/5 : WORKDIR /miniconda
---> Using cache
---> 8ee9890a7109
Step 4/5 : WORKDIR /miniconda
---> Running in 63238c179aea
Removing intermediate container 63238c179aea
---> 52f571393bf6
Step 5/5 : RUN curl -so ./miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && chmod +x ./miniconda.sh && ./miniconda.sh -b -p CONDA_DIR
---> Running in b59e945ad7a9
Removing intermediate container b59e945ad7a9
---> 74ce06c9af66
Successfully built 74ce06c9af66

Docker: Error code 127 when executing shell script

So I can't seem to figure this out, but I'm getting error code 127 when running a Dockerfile. What causes this error?
My Dockerfile:
FROM composer as comp
FROM php:7.4-fpm-alpine
COPY --from=comp /usr/bin/composer /usr/bin/composer
COPY ./docker/install-deps.sh /tmp/install-deps.sh
RUN echo $(ls /tmp)
RUN /tmp/install-deps.sh
COPY . /var/www
WORKDIR /var/www
RUN composer install -o --no-dev
The results after building the Dockerfile:
Building php
Step 1/9 : FROM composer as comp
---> 433420023b60
Step 2/9 : FROM php:7.4-fpm-alpine
---> 78e945602ecc
Step 3/9 : COPY --from=comp /usr/bin/composer /usr/bin/composer
---> 46117e22b4de
Step 4/9 : COPY ./docker/install-deps.sh /tmp/install-deps.sh
---> 7e46a2ee759c
Step 5/9 : RUN echo $(ls /tmp)
---> Running in aa1f900032f9
install-deps.sh
Removing intermediate container aa1f900032f9
---> eb455e78b7f6
Step 6/9 : RUN /tmp/install-deps.sh
---> Running in 6402a15cccb2
/bin/sh: /tmp/install-deps.sh: not found
ERROR: Service 'php' failed to build: The command '/bin/sh -c /tmp/install-deps.sh' returned a non-zero code: 127
The install-deps.sh:
#!/bin/sh
set -e
apk add --update --no-cache \
postgresql-dev \
mysql-client \
yaml-dev \
git \
openssl
docker-php-ext-install pcntl pdo_mysql pdo_pgsql
# yaml
apk add --no-cache --virtual .build-deps g++ make autoconf
pecl channel-update pecl.php.net
pecl install yaml
docker-php-ext-enable yaml
apk del --purge .build-deps
Docker is executing the install-deps.sh script. The issue is with a command inside install-deps.sh that is not recognized when docker attempts to run the script.
As you can see the script returns an error code of 127 meaning that a command within the file does not exist.
For instance - try this:
touch test.sh
echo "not-a-command" >> test.sh
chmod 755 test.sh
/bin/sh -c "./test.sh"
Output:
/root/test.sh: line 1: not-a-command: command not found
Now check the exit code:
echo $?
127
I would suggest running the script inside an interactive environment to identify/install the command that is not found.

Dockerfile creates a file and then it's not there

This is just a piece of a Dockefile that's confusing me. It has some extra debugging lines in it by the way. In the first line I create /home/ubuntu/.bashrc. However then at the last line, it acts as if it cannot run it.
RUN echo 'source /opt/ros/kinetic/setup.bash' >> /home/ubuntu/.bashrc
RUN echo 'source /home/ubuntu/catkin_ws/devel/setup.bash' >> /home/ubuntu/.bashrc
RUN /bin/bash -c "echo 'export HOME=/home/ubuntu' >> /root/.bashrc && source /root/.bashrc"
RUN pwd
RUN cd ~ && pwd
RUN cat /home/ubuntu/.bashrc
RUN mkdir -p ~/catkin_ws/src
RUN source /home/ubuntu/.bashrc && \
cd ~/catkin_ws/src && \
/opt/ros/kinetic/bin/catkin_init_workspace && \
cd ~/catkin_ws && \
catkin_make
Here's the output:
Step 13/32 : RUN echo 'source /opt/ros/kinetic/setup.bash' >> /home/ubuntu/.bashrc
---> Using cache
---> a60c2d1482d8
Step 14/32 : RUN echo 'source /home/ubuntu/catkin_ws/devel/setup.bash' >> /home/ubuntu/.bashrc
---> Using cache
---> 3be964ee0c36
Step 15/32 : RUN /bin/bash -c "echo 'export HOME=/home/ubuntu' >> /root/.bashrc && source /root/.bashrc"
---> Using cache
---> 83cf2e5f4b1c
Step 16/32 : RUN pwd
---> Using cache
---> 40915ecc834d
Step 17/32 : RUN cd ~ && pwd
---> Using cache
---> 92f2cee78a48
Step 18/32 : RUN cat /home/ubuntu/.bashrc
---> Using cache
---> c8f467775b33
Step 19/32 : RUN mkdir -p ~/catkin_ws/src
---> Using cache
---> 53e5c403949f
Step 20/32 : RUN source /home/ubuntu/.bashrc && cd ~/catkin_ws/src && /opt/ros/kinetic/bin/catkin_init_workspace && cd ~/catkin_ws && catkin_make
---> Running in 708d485325e2
/bin/sh: 1: source: not found
The command '/bin/sh -c source /home/ubuntu/.bashrc && cd ~/catkin_ws/src && /opt/ros/kinetic/bin/catkin_init_workspace && cd ~/catkin_ws && catkin_make' returned a non-zero code: 127
Naturally, it's my bug but I cant see it nor the gap in my understanding.Thanks!
source is not a valid command, it's a bash builtin.
It's not telling you that the file /home/ubuntu/.bashrc but that source is not a command
Just put the RUN parameter in a sh script, started correctly with #!/bin/bash.
Then just copy the sh to the container using COPY, and run it with RUN.
Don't forget to give the exec permission to the script:
COPY script.sh /
RUN ["chmod", "+x", "/script.sh"]
RUN /script.sh
Or:
RUN /bin/bash -c "source ...."
As pointed out by #michael_bitard, source is a bash built-in. Default shell in ubuntu is dash as can be seen here:
# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jan 22 17:49 /bin/sh -> dash
To use bash to run the command, change the RUN instruction to
RUN cd ~/catkin_ws/src && \
/opt/ros/kinetic/bin/catkin_init_workspace && \
cd ~/catkin_ws && \
/bin/bash -c "source /home/ubuntu/.bashrc; catkin_make"
Another option is to set the BASH_ENV environment variable, which should source the specified file (/home/ubuntu/.bashrc) before running the bash script catkin_make as mentioned here.
ENV BASH_ENV /home/ubuntu/.bashrc
RUN cd ~/catkin_ws/src && \
/opt/ros/kinetic/bin/catkin_init_workspace && \
cd ~/catkin_ws && \
/bin/bash -c "catkin_make"

Trying to set JAVA_HOME in a docker image, don't know why its not working

I'm a linux scripting novice so go easy please.
I am trying to build a docker image that includes jdk 1.8.0 and has a variable JAVA_HOME that points to the location of the jdk.
Here's my Dockerfile:
FROM centos
RUN yum install -y wget java-1.8.0-openjdk zip unzip
RUN echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | tee -a /etc/profile
RUN source /etc/profile
RUN echo $JAVA_HOME
I'm echoing $JAVA_HOME just to check that it does get set but here's the output when I build the image:
Sending build context to Docker daemon 50.18kB
Step 1/5 : FROM centos
---> 49f7960eb7e4
Step 2/5 : RUN yum install -y wget java-1.8.0-openjdk zip unzip
---> Using cache
---> 98745c0d3d08
Step 3/5 : RUN echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | tee -a /etc/profile
---> Using cache
---> 9e3244072966
Step 4/5 : RUN source /etc/profile
---> Using cache
---> 99809c08f2e3
Step 5/5 : RUN echo $JAVA_HOME
---> Running in c15274d8429f
Removing intermediate container c15274d8429f
---> a561301e0546
Successfully built a561301e0546
I'm sure there's a simple reason why this isn't working but I can't figure it out.
By the way, I'm doing this because I want to install maven as per these instructions: https://www.vultr.com/docs/how-to-install-apache-maven-3-5-on-centos-7 .
Any help much appreciated.
You can echo it if you run it part of the same RUN command:
FROM centos
RUN yum install -y wget java-1.8.0-openjdk zip unzip
RUN echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | tee -a /etc/profile && source /etc/profile && echo $JAVA_HOME

Resources