Skip to content

Commit f04c623

Browse files
authored
Update Dockerfile
1 parent 6d1f97c commit f04c623

1 file changed

Lines changed: 69 additions & 43 deletions

File tree

Dockerfile

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,110 @@
11
# ==============================================================================
2-
# Dockerfile for a LIGHTWEIGHT, professional, and optimized code-server environment
2+
# Dockerfile for a professionally architected, optimized code-server environment
3+
#
4+
# Architecture:
5+
# - Installation by 'root': The entire Python/Conda toolchain is installed
6+
# by the root user into a system-wide location (/opt/conda).
7+
# - Usage by 'coder': The non-root 'coder' user is configured to seamlessly
8+
# use this immutable, pre-built environment.
39
#
410
# Features:
511
# - Base: code-server (latest)
612
# - Python: Miniforge (Conda) with a pre-created Python 3.11 environment
7-
# - Package Manager: 'uv' (ultra-fast) and 'conda'
13+
# - Package Manager: 'uv' (installed via Conda for consistency)
14+
# - Root & Coder Access: 'python', 'conda', 'uv' are available for all users.
815
# - Optimization (China):
916
# - Timezone: Asia/Shanghai
10-
# - PyPI Mirror: Alibaba Cloud (configured via uv.toml - CORRECT FORMAT)
17+
# - PyPI Mirror: Alibaba Cloud (configured for the 'coder' user)
1118
# - Pre-installed Libraries: A minimal set (numpy, pandas, matplotlib)
12-
# - Convenience: Auto-activates conda environment in the terminal
19+
# - Convenience: Auto-activates conda environment for the 'coder' user.
1320
# ==============================================================================
1421

15-
# Step 1: Start from the official code-server base image.
16-
FROM codercom/code-server:latest
22+
# --- Build Stage ---
23+
FROM codercom/code-server:latest as builder
1724

18-
# Step 2: Set arguments for tool versions for easy updates.
25+
# Set arguments for tool versions.
1926
ARG MINIFORGE_VERSION=23.11.0-0
2027
ARG PYTHON_VERSION=3.11
2128

22-
# Step 3: Define environment variables for paths and timezone.
29+
# Define global environment variables for paths and timezone.
30+
# This makes the toolchain available to ALL subsequent users (root and coder).
2331
ENV CONDA_DIR=/opt/conda
24-
ENV UV_DIR=/home/coder/.local
25-
ENV PATH=${CONDA_DIR}/bin:${UV_DIR}/bin:${PATH}
32+
ENV PATH=${CONDA_DIR}/bin:${PATH}
2633
ENV TZ=Asia/Shanghai
2734

28-
# Step 4: Switch to the ROOT user for system-level installations.
35+
# --- Installation Phase (as root) ---
2936
USER root
3037

31-
# Step 5: Install system dependencies, set timezone, and install Miniforge.
3238
RUN \
33-
# Update package lists and install necessary tools.
39+
# 1. Install system dependencies.
3440
apt-get update && apt-get install -y --no-install-recommends \
3541
wget \
3642
curl \
3743
git \
3844
build-essential \
3945
tzdata \
40-
# Set the timezone non-interactively.
4146
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
42-
# Download and install Miniforge.
47+
\
48+
# 2. Install Miniforge (Conda).
4349
&& wget "https://github.com/conda-forge/miniforge/releases/download/${MINIFORGE_VERSION}/Miniforge3-${MINIFORGE_VERSION}-Linux-x86_64.sh" -O miniforge.sh \
4450
&& /bin/bash miniforge.sh -b -p ${CONDA_DIR} \
4551
&& rm miniforge.sh \
46-
# Give the 'coder' user ownership of the conda directory.
47-
&& chown -R coder:coder ${CONDA_DIR} \
48-
# Create symbolic links for 'conda' and 'python3' for easier root access (debugging).
49-
&& ln -s ${CONDA_DIR}/bin/conda /usr/local/bin/conda \
50-
&& ln -s ${CONDA_DIR}/bin/python /usr/local/bin/python3 \
51-
# Clean up apt cache to reduce image size.
52+
\
53+
# 3. Initialize Conda for the root's shell (useful for subsequent RUN commands).
54+
&& conda init bash && . /root/.bashrc \
55+
\
56+
# 4. Install 'uv' into the base conda environment for system-wide access.
57+
&& conda install -n base uv -c conda-forge -y \
58+
\
59+
# 5. Create the target Python environment.
60+
&& conda create -n py${PYTHON_VERSION} python=${PYTHON_VERSION} -y \
61+
\
62+
# 6. Install core libraries into the new environment using 'uv'.
63+
&& uv pip install --python=${CONDA_DIR}/envs/py${PYTHON_VERSION}/bin/python \
64+
numpy \
65+
pandas \
66+
matplotlib \
67+
\
68+
# 7. Ensure the 'coder' user home directory exists and has correct ownership.
69+
&& mkdir -p /home/coder && chown -R coder:coder /home/coder \
70+
\
71+
# 8. Clean up to reduce image size.
5272
&& apt-get clean \
5373
&& rm -rf /var/lib/apt/lists/*
5474

55-
# Step 6: Switch back to the standard, non-root 'coder' user.
75+
76+
# --- User Configuration Phase (as coder) ---
5677
USER coder
5778

58-
# Step 7: As the 'coder' user, install 'uv', configure it, and create the environment.
5979
RUN \
60-
# Install 'uv' (the fast Python package manager).
61-
curl -LsSf https://astral.sh/uv/install.sh | sh && \
62-
\
63-
# Create the configuration directory for uv.
80+
# 1. Create the user-specific mirror configuration for 'uv'.
6481
mkdir -p ~/.config/uv && \
65-
\
66-
### --- [ THE REAL, FINAL FIX IS HERE ] --- ###
67-
# Create the uv.toml with the CORRECT format for a global user config.
68-
# It does NOT use the [tool.uv] section header.
6982
printf 'index-url = "https://mirrors.aliyun.com/pypi/simple"\n' > ~/.config/uv/uv.toml && \
7083
\
71-
# Create the default conda environment.
72-
conda create -n py${PYTHON_VERSION} python=${PYTHON_VERSION} -y && \
73-
\
74-
# Pre-install a minimal set of core data science libraries.
75-
# This command will now succeed as uv can correctly parse its config.
76-
uv pip install --python=${CONDA_DIR}/envs/py${PYTHON_VERSION}/bin/python \
77-
numpy \
78-
pandas \
79-
matplotlib && \
80-
\
81-
# Configure the shell to automatically activate this environment on login.
84+
# 2. Configure the user's shell to auto-activate the system-wide environment.
85+
conda init bash && \
8286
echo "conda activate py${PYTHON_VERSION}" >> ~/.bashrc
8387

84-
# Step 8: The base image's entrypoint will start code-server.
88+
89+
# --- Verification and Final Stage ---
90+
# This stage ensures both root and coder environments are correctly configured.
91+
FROM builder
92+
93+
# Final check as ROOT.
94+
RUN echo "Verifying root environment..." && \
95+
python --version && \
96+
conda --version && \
97+
uv --version && \
98+
echo "Root environment check PASSED!"
99+
100+
# Final check as CODER.
101+
USER coder
102+
RUN echo "Verifying coder environment..." && \
103+
# We must source .bashrc to load the 'conda activate' alias in a non-interactive shell.
104+
source ~/.bashrc && \
105+
python --version && \
106+
uv --version && \
107+
echo "Coder environment check PASSED!"
108+
109+
# The base image's CMD is inherited automatically.
110+
# No need to specify it again.

0 commit comments

Comments
 (0)