Dockerfile没有运行所需的Shell脚本

时间:2019-01-19 22:11:22

标签: docker ros

我正在尝试使用Docker配置并运行某个程序。我是Docker的初学者,所以请当心新手错误!

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"

给出以下输出:

~/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

我需要它来运行catkin_make(在.bashrc设置的路径上)

1 个答案:

答案 0 :(得分:1)

shell命令中的退出代码127表示“找不到命令”。 .bashrc可执行吗?通常不是,可能您想来源吗?

source ./home/$USERNAME/.bashrc

正如Dan Farrel在其comment中所指出的那样,以RUN命令提供文件只会在该shell中起作用。

在构建过程中获取.bashrc的源代码

如果您希望它对构建中的后续命令有效,则需要在同一RUN语句中运行它们。在下面的情况下,.bashrc与运行catkin_make的外壳程序相同。

RUN . /home/ros/.bashrc && \ 
    cd /home/$USERNAME/catkin_ws && \
    catkin_make

要在容器启动时获取.bashrc文件

使用docker run语句指定使用ENTRYPOINT运行容器时会发生什么。如果只需要普通的bash提示,请指定/bin/bash。该Shell将与USER语句中指定的用户一起运行。

总之,如果您将以下内容添加到Dockerfile

的末尾
USER ros
ENTRYPOINT /bin/bash

当有人使用docker run -it <containerName>运行容器时,他们将以用户ros的身份进入bash shell中。 Bash将自动获取/home/ros/.bashrc文件,并且其中的所有定义都将在Shell中可用。 (可以删除包含RUN文件的.bashrc语句