Dockerfile中source命令失效且git clone无输出,求修复方案
Fixing Your Dockerfile Issues: git Clone & source Command Failures
Let's break down and fix the problems in your Dockerfile step by step:
Key Issues Identified
source ~/.bashrcdoesn't persist: EachRUNcommand runs in a separate shell session, so environment changes fromsourcewon't carry over to subsequent commands in the sameRUNblock (or other blocks).- Unreferenced environment variable: You used
PYTHON_VERSIONdirectly instead of${PYTHON_VERSION}when callingpyenv install, so it's treated as a literal string instead of the value you set. - Unnecessary directory navigation:
cd ..after cloning into~doesn't serve any purpose, and can cause confusion with working directories. - Potential silent failures: Running commands without error checking can hide issues like failed git clones; adding
set -ewill make the build fail immediately if any command fails, helping you catch problems early.
Fixed Dockerfile
FROM ubuntu:16.04 MAINTAINER hulei886@aliyun.com ENV PHANTOMJS_VERSION 2.1.1 ENV PYTHON_VERSION 3.6.3 # Install dependencies and set up pyenv + Python 3.6.3 RUN set -e \ && apt-get update \ && apt-get install -y --no-install-recommends \ build-essential \ git \ curl \ libssl-dev \ zlib1g-dev \ libncurses5-dev \ libncursesw5-dev \ libreadline-dev \ libsqlite3-dev \ libgdbm-dev \ libdb5.3-dev \ libbz2-dev \ libexpat1-dev \ liblzma-dev \ tk-dev \ && # Install pyenv and initialize it in the current shell session && git clone https://github.com/pyenv/pyenv.git ~/.pyenv \ && export PYENV_ROOT="$HOME/.pyenv" \ && export PATH="$PYENV_ROOT/bin:$PATH" \ && eval "$(pyenv init -)" \ && # Install Python and set it as global && pyenv install ${PYTHON_VERSION} \ && pyenv global ${PYTHON_VERSION} \ && # Clean up to reduce image size && apt-get autoremove -y \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Set the correct Python path for subsequent commands and runtime ENV PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH" CMD ["python"]
Detailed Explanations
set -e: Ensures the build stops immediately if any command fails, so you'll catch a failed git clone right away instead of letting the build proceed with broken dependencies.- Avoiding
source ~/.bashrc: Instead of modifying.bashrc(which only affects interactive shells), we directly export the required environment variables in the sameRUNblock where we use pyenv, ensuring they're available for the commands that need them. - Referencing environment variables: Using
${PYTHON_VERSION}ensures we pull the value you defined in theENVdirective, instead of treating it as a literal string. - Cleanup steps: Adding
apt-get autoremoveandrm -rfcommands trims down the final image size by removing unnecessary packages and temporary files. - Persisting PATH: We set the
PATHvia theENVdirective so that the pyenv shims (including your installed Python 3.6.3) are available in all subsequent commands and the final container runtime.
Testing the Build
Run your build command again with your desired tag:
docker build . -t your-tag-name
This should successfully clone pyenv, install Python 3.6.3, and set it as the default Python version in your image.
内容的提问来源于stack exchange,提问作者hl79-james
相关产品推荐
相关产品推荐

