首页 > 解决方案 > Dockerfile 没有运行所需的 shell 脚本

问题描述

我正在尝试使用 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 设置的路径上)

标签: dockerros

解决方案


shell 命令的退出代码 127 表示“找不到命令”。.bashrc可执行吗?通常它不是,可能你想采购它?

source ./home/$USERNAME/.bashrc

正如Dan Farrel在他的评论中指出的那样,在命令中获取文件RUN只会在该 shell 中生效。

在构建期间获取 .bashrc

如果您希望它对构建中的后续命令生效,您需要在同一RUN语句中运行它们。下面.bashrc是在与运行相同的 shell 中获取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>他们将以用户身份进入 bash shell ros。Bash 将自动获取/home/ros/.bashrc文件,并且其中的所有定义都将在 shell 中可用。(您RUN包含该文件的语句.bashrc可以删除


推荐阅读