Docker run指定-w工作目录后,Dockerfile复制的脚本消失问题咨询
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:
Update Your Dockerfile to Target the Desired Directory
If you wantsourceshell.shto 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/.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.shOr switch to the
/rootdirectory first to run it with a relative path:cd /root && ./sourceshell.shLaunch the Shell in Your Target Directory (No Image Changes Needed)
You can adjust yourdocker runcommand 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.shwhenever 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

