You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker run指定-w工作目录后,Dockerfile复制的脚本消失问题咨询

Why Can't You Find sourceshell.sh When Specifying a Working Directory?

Let me break down exactly what's going on here:

The Root Cause

When you used COPY . ~/sourceshell.sh in your Dockerfile, the ~ resolves to the root user's home directory (/root/) during the build process. That means your file was copied to /root/sourceshell.sh, not into the working directory you later specify with the -w flag.

When you run the container with -w "/root/afolder/", Docker starts your interactive shell session in /root/afolder/ instead of the default (which is /root/ for the ros:kinetic base image). Since sourceshell.sh lives one level up in /root/, it won't show up in your current working directory by default.

Simple Fixes to Try

Here are a few straightforward ways to resolve this:

  1. Update Your Dockerfile to Target the Desired Directory
    If you want sourceshell.sh to always live in /root/afolder/, set the working directory in your Dockerfile first so the COPY command uses that path automatically:

    FROM ros:kinetic
    WORKDIR /root/afolder/
    COPY . ./sourceshell.sh
    RUN ["/bin/bash","-c","source /opt/ros/kinetic/setup.bash"]
    

    Now when you build and run the container (with or without -w), the file will be waiting for you in /root/afolder/.

  2. Access the File via Its Absolute Path
    If you don't want to rebuild the image, just reference the file using its full path when you're in /root/afolder/:

    /root/sourceshell.sh
    

    Or switch to the /root directory first to run it with a relative path:

    cd /root && ./sourceshell.sh
    
  3. Launch the Shell in Your Target Directory (No Image Changes Needed)
    You can adjust your docker run command to start in /root/afolder/ while still keeping easy access to the file:

    docker run --rm -it test /bin/bash -c "cd /root/afolder/ && exec /bin/bash"
    

    This starts your shell in the desired directory, and you can still use the absolute path to run sourceshell.sh whenever you need it.

Quick Side Note

One small thing to mention: The RUN ["source /opt/ros/kinetic/setup.bash"] command in your Dockerfile doesn't persist ROS environment variables for your interactive shell. If you want those variables available every time you log into the container, add source /opt/ros/kinetic/setup.bash to /root/.bashrc in your Dockerfile, or use ENV commands to set the necessary variables directly.

内容的提问来源于stack exchange,提问作者KansaiRobot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:03:14