refactor docker image build

This commit is contained in:
LeonOstrez
2025-03-06 17:12:03 +01:00
parent e7399a1945
commit aee37628bd
8 changed files with 158 additions and 210 deletions

View File

@@ -1,65 +1,18 @@
# Use Ubuntu 22.04 as the base image with multi-arch support
FROM ubuntu:22.04
# Set environment to prevent interactive prompts during builds
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC
# Use buildx args for multi-arch support
ARG TARGETPLATFORM
ARG BUILDPLATFORM
# Update package list and install prerequisites
RUN apt-get update && apt-get install -y --no-install-recommends \
software-properties-common \
build-essential \
curl \
git \
gnupg \
tzdata \
inotify-tools \
vim \
nano \
lsof \
procps \
&& rm -rf /var/lib/apt/lists/*
# Copy VSIX file first
COPY pythagora-vs-code.vsix /var/init_data/pythagora-vs-code.vsix
# Add deadsnakes PPA for Python 3.12 and install Python
RUN add-apt-repository ppa:deadsnakes/ppa -y && apt-get update && \
apt-get install -y --no-install-recommends \
python3.12 \
python3.12-venv \
python3.12-dev \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
# Set Python 3.12 as the default python3 and python
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 && \
update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
python --version
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
apt-get install -y nodejs && \
node --version && npm --version
# MongoDB installation with platform-specific approach
RUN case "$TARGETPLATFORM" in \
"linux/amd64") \
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-archive-keyring.gpg && \
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list && \
apt-get update && apt-get install -y mongodb-org \
;; \
"linux/arm64"|"linux/arm64/v8") \
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-archive-keyring.gpg && \
echo "deb [arch=arm64 signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list && \
apt-get update && apt-get install -y mongodb-org \
;; \
*) \
echo "Unsupported platform: $TARGETPLATFORM" && exit 1 \
;; \
esac \
&& rm -rf /var/lib/apt/lists/*
# Install all dependencies
COPY cloud/setup-dependencies.sh /tmp/setup-dependencies.sh
RUN chmod +x /tmp/setup-dependencies.sh && \
/tmp/setup-dependencies.sh && \
rm /tmp/setup-dependencies.sh
ENV PYTH_INSTALL_DIR=/pythagora
@@ -78,7 +31,7 @@ RUN python3 -m venv venv && \
ADD main.py .
ADD core core
ADD pyproject.toml .
ADD config-docker.json config.json
ADD cloud/config-docker.json config.json
# Set the virtual environment to be automatically activated
ENV VIRTUAL_ENV=${PYTH_INSTALL_DIR}/pythagora-core/venv
@@ -90,67 +43,33 @@ RUN mkdir -p data
# Expose MongoDB and application ports
EXPOSE 27017 8000 8080 5173 3000
# Create a group named "devusergroup" with a specific GID (1000, optional)
RUN groupadd -g 1000 devusergroup
# Create a group and user
RUN groupadd -g 1000 devusergroup && \
useradd -m -u 1000 -g devusergroup -s /bin/bash devuser && \
echo "devuser:devuser" | chpasswd && \
adduser devuser sudo && \
echo "devuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
ARG USERNAME=devuser
# Set up entrypoint and VS Code extension
ADD cloud/entrypoint.sh /entrypoint.sh
ADD cloud/on-event-extension-install.sh /var/init_data/on-event-extension-install.sh
# Create a user named "devuser" with a specific UID (1000) and assign it to "devusergroup"
RUN useradd -m -u 1000 -g devusergroup -s /bin/bash $USERNAME
# Create necessary directories with proper permissions for code-server
RUN mkdir -p /usr/local/share/code-server/data/User/globalStorage && \
mkdir -p /usr/local/share/code-server/data/User/History && \
mkdir -p /usr/local/share/code-server/data/Machine && \
mkdir -p /usr/local/share/code-server/data/logs && \
chown -R devuser:devusergroup /usr/local/share/code-server && \
chmod -R 755 /usr/local/share/code-server
# Add the user to sudoers for admin privileges
RUN echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN chmod +x /entrypoint.sh && \
chmod +x /var/init_data/on-event-extension-install.sh && \
chown -R devuser:devusergroup /pythagora && \
chown -R devuser: /var/init_data/
# Create an embedded entrypoint script
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Create workspace directory
RUN mkdir -p ${PYTH_INSTALL_DIR}/pythagora-core/workspace && \
chown -R devuser:devusergroup ${PYTH_INSTALL_DIR}/pythagora-core/workspace
RUN chown -R $USERNAME:devusergroup /pythagora
USER $USERNAME
# add this before vscode... better caching of layers
ADD pythagora-vs-code.vsix /var/init_data/pythagora-vs-code.vsix
# ARG commitHash
# # VS Code server installation with platform-specific handling
# RUN case "$TARGETPLATFORM" in \
# "linux/amd64") \
# mkdir -p ~/.vscode-server/cli/servers/Stable-${commitHash} && \
# curl -fsSL https://update.code.visualstudio.com/commit:${commitHash}/server-linux-x64/stable -o server-linux-x64.tar.gz && \
# tar -xz -f server-linux-x64.tar.gz -C ~/.vscode-server/cli/servers/Stable-${commitHash} && \
# mv ~/.vscode-server/cli/servers/Stable-${commitHash}/vscode-server-linux-x64 ~/.vscode-server/cli/servers/Stable-${commitHash}/server \
# ;; \
# "linux/arm64"|"linux/arm64/v8") \
# mkdir -p ~/.vscode-server/cli/servers/Stable-${commitHash} && \
# curl -fsSL https://update.code.visualstudio.com/commit:${commitHash}/server-linux-arm64/stable -o server-linux-arm64.tar.gz && \
# tar -xz -f server-linux-arm64.tar.gz -C ~/.vscode-server/cli/servers/Stable-${commitHash} && \
# mv ~/.vscode-server/cli/servers/Stable-${commitHash}/vscode-server-linux-arm64 ~/.vscode-server/cli/servers/Stable-${commitHash}/server \
# ;; \
# *) \
# echo "Unsupported platform: $TARGETPLATFORM" && exit 1 \
# ;; \
# esac
# Install VS Code extension (platform-agnostic)
# RUN ~/.vscode-server/cli/servers/Stable-${commitHash}/server/bin/code-server --install-extension pythagora-vs-code.vsix
ADD on-event-extension-install.sh /var/init_data/on-event-extension-install.sh
# Create a workspace directory
RUN mkdir -p ${PYTH_INSTALL_DIR}/pythagora-core/workspace
RUN mkdir -p /home/$USERNAME/.vscode-server/cli/servers
ADD vsc-dl-x64 vsc-dl-x64
ADD vscode_tags.csv vscode_tags.csv
RUN ./vsc-dl-x64 /home/$USERNAME vscode_tags.csv && rm vsc-dl-x64 && rm vscode_tags.csv
USER root
RUN chmod +x /var/init_data/on-event-extension-install.sh
RUN chown -R devuser: /var/init_data/
# Set the entrypoint to the main application
# Remove the USER directive to keep root as the running user
ENTRYPOINT ["/entrypoint.sh"]

View File

@@ -9,16 +9,20 @@ mongod --dbpath "$MONGO_DB_DATA" --bind_ip_all >> $MONGO_DB_DATA/mongo_logs.txt
export DB_DIR=$PYTHAGORA_DATA_DIR/database
chown -R devuser: $PYTHAGORA_DATA_DIR
su - devuser -c "mkdir -p $DB_DIR"
su -c "mkdir -p $DB_DIR" devuser
# Ensure code-server directories have correct permissions
chown -R devuser:devusergroup /usr/local/share/code-server
chmod -R 755 /usr/local/share/code-server
set -e
# Start the VS Code extension installer/HTTP server script in the background
su - devuser -c "cd /var/init_data/ && ./on-event-extension-install.sh &"
su -c "cd /var/init_data/ && ./on-event-extension-install.sh &" devuser
# Set up git config
su - devuser -c "git config --global user.email 'devuser@pythagora.ai'"
su - devuser -c "git config --global user.name 'pythagora'"
su -c "git config --global user.email 'devuser@pythagora.ai'" devuser
su -c "git config --global user.name 'pythagora'" devuser
# Keep container running
tail -f /dev/null

View File

@@ -0,0 +1,17 @@
#!/bin/bash
set -e
VSCODE_SERVER_PORT=8080
# Create workspace directory and settings
mkdir -p /pythagora/pythagora-core/workspace/.vscode
printf '{\n "gptPilot.isRemoteWs": true,\n "gptPilot.useRemoteWs": false,\n "workbench.colorTheme": "Default Dark+"\n}' > /pythagora/pythagora-core/workspace/.vscode/settings.json
# Start code-server and direct to our workspace
echo "Starting code-server..."
code-server --config /etc/code-server/config.yaml /pythagora/pythagora-core/workspace &
CODE_SERVER_PID=$!
echo $CODE_SERVER_PID > /tmp/vscode-http-server.pid
echo "VS Code HTTP server started with PID $CODE_SERVER_PID. Access at http://localhost:$VSCODE_SERVER_PORT"

101
cloud/setup-dependencies.sh Normal file
View File

@@ -0,0 +1,101 @@
#!/bin/bash
set -e
# Set environment variables
export DEBIAN_FRONTEND=noninteractive
export TZ=Etc/UTC
# Update package list and install prerequisites
apt-get update && apt-get install -y --no-install-recommends \
software-properties-common \
build-essential \
curl \
git \
gnupg \
tzdata \
inotify-tools \
vim \
nano \
lsof \
procps
rm -rf /var/lib/apt/lists/*
# Install Python 3.12
add-apt-repository ppa:deadsnakes/ppa -y && apt-get update
apt-get install -y --no-install-recommends \
python3.12 \
python3.12-venv \
python3.12-dev \
python3-pip
rm -rf /var/lib/apt/lists/*
# Set Python 3.12 as default
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
update-alternatives --install /usr/bin/python python /usr/bin/python3 1
python --version
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt-get install -y nodejs
node --version && npm --version
# Install MongoDB based on platform architecture
case "$TARGETPLATFORM" in
"linux/amd64")
MONGO_ARCH="amd64"
;;
"linux/arm64"|"linux/arm64/v8")
MONGO_ARCH="arm64"
;;
*)
echo "Unsupported platform: $TARGETPLATFORM"
exit 1
;;
esac
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-archive-keyring.gpg
echo "deb [arch=$MONGO_ARCH signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list
apt-get update && apt-get install -y mongodb-org
rm -rf /var/lib/apt/lists/*
# Install code-server
VERSION="4.97.2"
case "$TARGETPLATFORM" in
"linux/amd64")
PLATFORM="amd64"
;;
"linux/arm64"|"linux/arm64/v8")
PLATFORM="arm64"
;;
*)
echo "Unsupported platform: $TARGETPLATFORM"
exit 1
;;
esac
DOWNLOAD_URL="https://github.com/coder/code-server/releases/download/v${VERSION}/code-server-${VERSION}-linux-${PLATFORM}.tar.gz"
echo "Downloading code-server from $DOWNLOAD_URL"
curl -L "$DOWNLOAD_URL" -o /tmp/code-server.tar.gz
# Create directories and install
mkdir -p /usr/local/lib/code-server
mkdir -p /usr/local/bin
mkdir -p /usr/local/share/code-server/extensions
mkdir -p /usr/local/share/code-server/data
mkdir -p /etc/code-server
# Install code-server
tar -xzf /tmp/code-server.tar.gz -C /usr/local/lib/code-server --strip-components=1
ln -s /usr/local/lib/code-server/bin/code-server /usr/local/bin/code-server
rm /tmp/code-server.tar.gz
# Create default config
cat > /etc/code-server/config.yaml << EOF
bind-addr: 0.0.0.0:8080
auth: none
extensions-dir: /usr/local/share/code-server/extensions
user-data-dir: /usr/local/share/code-server/data
EOF
# Pre-install extension
code-server --config /etc/code-server/config.yaml --install-extension /var/init_data/pythagora-vs-code.vsix

View File

@@ -1,87 +0,0 @@
#!/bin/bash
set -e
# Directory to monitor
MONITOR_DIR="$HOME/.vscode-server/cli/servers"
PATTERN="Stable-*"
EXCLUDE_SUFFIX=".staging"
VSCODE_SERVER_PORT=8080
# Ensure inotify-tools is installed
if ! command -v inotifywait >/dev/null 2>&1; then
echo "Error: inotifywait is not installed. Please install inotify-tools." >&2
exit 1
fi
#notes
#home directory is $HOME/.config/Code/User/settings.json
# Create workspace directory and settings
mkdir -p /pythagora/pythagora-core/workspace/.vscode
printf '{\n "gptPilot.isRemoteWs": true,\n "gptPilot.useRemoteWs": false,\n "workbench.colorTheme": "Default Dark+"\n}' > /pythagora/pythagora-core/workspace/.vscode/settings.json
# Start HTTP-based VS Code server
echo "Starting VS Code HTTP server on port $VSCODE_SERVER_PORT..."
# Manual installation of code-server without sudo
if ! command -v code-server >/dev/null 2>&1; then
echo "Installing code-server directly..."
# Create directories
mkdir -p ~/.local/lib/code-server
mkdir -p ~/.local/bin
# Download code-server
VERSION="4.97.2"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
PLATFORM="amd64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
PLATFORM="arm64"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
DOWNLOAD_URL="https://github.com/coder/code-server/releases/download/v${VERSION}/code-server-${VERSION}-linux-${PLATFORM}.tar.gz"
echo "Downloading code-server from $DOWNLOAD_URL"
curl -L "$DOWNLOAD_URL" -o /tmp/code-server.tar.gz
# Extract and install
tar -xzf /tmp/code-server.tar.gz -C ~/.local/lib/code-server --strip-components=1
ln -s ~/.local/lib/code-server/bin/code-server ~/.local/bin/code-server
export PATH="$HOME/.local/bin:$PATH"
# Clean up
rm /tmp/code-server.tar.gz
fi
# Start code-server and direct to our workspace
echo "Starting code-server..."
~/.local/bin/code-server --auth none --host 0.0.0.0 --port $VSCODE_SERVER_PORT /pythagora/pythagora-core/workspace &
CODE_SERVER_PID=$!
echo $CODE_SERVER_PID > /tmp/vscode-http-server.pid
# Install Pythagora extension in code-server
echo "Installing Pythagora extension..."
~/.local/bin/code-server --install-extension /var/init_data/pythagora-vs-code.vsix
echo "VS Code HTTP server started with PID $CODE_SERVER_PID. Access at http://localhost:$VSCODE_SERVER_PORT"
echo "Monitoring $MONITOR_DIR for new directories matching $PATTERN..."
inotifywait -m -e create -e moved_to --format '%f' "$MONITOR_DIR" | while read -r NEW_ITEM; do
# Check if the created item matches the pattern
if [[ "$NEW_ITEM" == $PATTERN && "$NEW_ITEM" != *$EXCLUDE_SUFFIX ]]; then
echo "Detected new directory: $NEW_ITEM"
while [ ! -f "$HOME/.vscode-server/cli/servers/$NEW_ITEM/server/bin/code-server" ]; do
sleep 1
done
$HOME/.vscode-server/cli/servers/$NEW_ITEM/server/bin/code-server --install-extension /var/init_data/pythagora-vs-code.vsix
fi
done

Binary file not shown.

View File

@@ -1,6 +0,0 @@
1.97.2, e54c774e0add60467559eb0d1e229c6452cf8447
1.97.1, e249dada235c2083c83813bd65b7f4707fb97b76
1.97.0, 33fc5a94a3f99ebe7087e8fe79fbe1d37a251016
1.96.4, cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba
1.96.3, 91fbdddc47bc9c09064bf7acf133d22631cbf083
1.96.2, fabdb6a30b49f79a7aba0f2ad9df9b399473380f
1 1.97.2 e54c774e0add60467559eb0d1e229c6452cf8447
2 1.97.1 e249dada235c2083c83813bd65b7f4707fb97b76
3 1.97.0 33fc5a94a3f99ebe7087e8fe79fbe1d37a251016
4 1.96.4 cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba
5 1.96.3 91fbdddc47bc9c09064bf7acf133d22631cbf083
6 1.96.2 fabdb6a30b49f79a7aba0f2ad9df9b399473380f