However, conda run isn't designed to start an interactive shell in the way you're expecting, and as a result, the container exits immediately after executing the command.
Solution: Adjust Your Dockerfile to Activate the Conda Environment Properly
To resolve this issue, you should modify your Dockerfile so that:
You don't rely on conda run for your SHELL or CMD instructions.
You activate the conda environment in a way that ensures it's available when you start an interactive shell.
Here's how you can adjust your Dockerfile:
dockerfile
# Start from the NVIDIA CUDA base image
FROM nvidia/cuda:11.8.0-devel-ubuntu22.04
# Install system dependencies
RUN apt-get update && apt-get install -y \
wget \
bzip2 \
ca-certificates \
libglib2.0-0 \
libxext6 \
libsm6 \
libxrender1 \
git
# Install Miniconda
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh && \
bash miniconda.sh -b -p /opt/conda && \
rm miniconda.sh
# Set path to conda
ENV PATH=/opt/conda/bin:$PATH
# Set working directory
WORKDIR /app
# Create local conda environment
RUN conda create --prefix ./venv python=3.10 -y
# Activate the conda environment in .bashrc
RUN echo "source /opt/conda/bin/activate /app/venv" >> ~/.bashrc
# Install PyTorch and other core dependencies using the conda environment
RUN /opt/conda/bin/conda run -p /app/venv pip install torch==2.0.1 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
RUN /opt/conda/bin/conda run -p /app/venv pip install ninja
RUN /opt/conda/bin/conda run -p /app/venv pip install flash-attn --no-build-isolation
# Set the default command to run an interactive bash shell
CMD ["bash"]
Explanation of Changes:
Removed the SHELL instruction that used conda run. This instruction affects how all subsequent RUN commands are executed, and can cause unintended behavior. Instead, we use the default shell (/bin/sh -c) and explicitly invoke commands within the conda environment where needed.
Modified the RUN commands to explicitly use the conda environment for pip install. We use /opt/conda/bin/conda run -p /app/venv to ensure that pip install runs inside the conda environment.
Added activation of the conda environment in the .bashrc file. By adding source /opt/conda/bin/activate /app/venv to .bashrc, we ensure that when an interactive shell is started, the conda environment is automatically activated.
Set the CMD to start bash. This will start an interactive shell when you run the container with -it.
Running the Container
Now, when you run:
bash
docker run -it --gpus all got-ocr-dev
You'll enter an interactive bash shell with your conda environment activated. You can verify that the environment is active by checking the Python version or listing installed packages:
bash
which python
python --version
pip list
Why the Original Approach Didn't Work
conda run Non-Interactive Behavior: The conda run command is intended for running a command non-interactively in a subshell. When used as CMD ["conda", "run", ...], it runs the command and then exits, which is why your container was exiting immediately.
Using SHELL with conda run: Changing the SHELL to use conda run can cause issues with script execution and environment variables during the image build process. It's best to use the default shell and explicitly activate the conda environment where needed.
Additional Tips
Avoid Modifying the SHELL Instruction with conda run: Keep the default shell for running commands during the build process. If you need to run commands within the conda environment during the build, explicitly invoke them using /opt/conda/bin/conda run.
Activating Environments in Docker Containers: In Docker, it's common to activate environments by adjusting the PATH environment variable or by sourcing the activation script in the shell's startup files (like .bashrc), as shown above.
Use ENV to Set Environment Variables: If your application relies on certain environment variables, you can set them using the ENV instruction in your Dockerfile.
Conclusion
By adjusting your Dockerfile to properly activate the conda environment without relying on conda run for your shell and command execution, you ensure that the container starts an interactive shell with the environment activated, resolving the issue you were experiencing.