Skip to content

Commit

Permalink
Environment Variables: Configure variables like PORT, USERNAME, and P…
Browse files Browse the repository at this point in the history
…ASSWORD when running the container.

Persistent Cache: Use npm cache clean and retry settings to address transient download issues.
Logs: Examine logs at /root/.npm/_logs/ for further troubleshooting if issues persist.
  • Loading branch information
dereckmezquita committed Dec 5, 2024
1 parent c697c89 commit 5b2b86d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 73 deletions.
58 changes: 33 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Use a base Ubuntu image
FROM ubuntu:22.04

# Environment variables for container configuration
ARG USERNAME=developer
ARG PASSWORD=password
ARG PORT=8443
# Set environment variables for non-interactive installation
ENV DEBIAN_FRONTEND=noninteractive

# Update and install essential tools
# Update and install base dependencies
RUN apt-get update && apt-get install -y \
build-essential \
curl \
Expand All @@ -18,32 +17,41 @@ RUN apt-get update && apt-get install -y \
zsh \
openssl \
ca-certificates \
gnupg \
&& rm -rf /var/lib/apt/lists/*
gnupg && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Node.js and npm (required for code-server npm installation)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs
# Add Node.js 18.x repository
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs && \
npm install -g npm@latest

# Ensure the installation environment is clean and ready
# Ensure npm cache is clean and retry on failure
RUN npm cache clean --force && \
rm -rf /usr/lib/node_modules/code-server
npm config set registry https://registry.npmjs.org/ && \
npm config set fetch-retries 5 && \
npm config set fetch-retry-mintimeout 10000 && \
npm config set fetch-retry-maxtimeout 30000

# Install code-server globally using npm with proper permissions
# Install code-server globally
RUN npm install -g --unsafe-perm code-server

# Add entrypoint script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Create a user and set permissions
ARG USERNAME=developer
ARG PASSWORD=password
RUN useradd -m -s /bin/zsh ${USERNAME} && \
echo "${USERNAME}:${PASSWORD}" | chpasswd && \
usermod -aG sudo ${USERNAME}

# Expose the default VS Code Server port
EXPOSE 8443

# Create a non-root user and configure environment
RUN useradd -ms /bin/bash -G sudo $USERNAME && \
echo "$USERNAME:$PASSWORD" | chpasswd && \
mkdir -p /home/$USERNAME && \
chown -R $USERNAME:$USERNAME /home/$USERNAME
# Set the entrypoint script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Expose the port for code-server
EXPOSE $PORT
# Set working directory and user
WORKDIR /home/${USERNAME}
USER ${USERNAME}

# Set the entrypoint
ENTRYPOINT ["/entrypoint.sh"]
# Default command
ENTRYPOINT ["entrypoint.sh"]
58 changes: 10 additions & 48 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,16 @@
#!/bin/bash
set -e

# Validate required environment variables
if [ -z "$USERNAME" ] || [ -z "$PORT" ] || [ -z "$PASSWORD" ]; then
echo "Error: USERNAME, PORT, and PASSWORD environment variables are required"
exit 1
fi

# Set HOME if not already set
if [ -z "$HOME" ]; then
export HOME="/home/${USERNAME}"
fi

# Create user if it doesn't exist
if ! id -u ${USERNAME} >/dev/null 2>&1; then
useradd -m -s /bin/bash ${USERNAME}
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USERNAME}
fi

# Setup directories
mkdir -p ${HOME}/.local/share/code-server/extensions
mkdir -p ${HOME}/.local/share/code-server/User
mkdir -p ${HOME}/.local/share/code-server/Machine
mkdir -p ${HOME}/.local/share/code-server/logs
mkdir -p ${HOME}/.local/share/code-server/User/globalStorage
mkdir -p ${HOME}/.local/share/code-server/User/History
chown -R ${USERNAME}:${USERNAME} ${HOME}

# Generate SSL certificates if they don't exist
if [ ! -f /etc/code-server/self-signed.crt ]; then
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/code-server/self-signed.key \
-out /etc/code-server/self-signed.crt \
# Generate SSL certificates if not exist
if [ ! -f /home/${USERNAME}/.ssl/key.pem ]; then
mkdir -p /home/${USERNAME}/.ssl
openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /home/${USERNAME}/.ssl/key.pem \
-out /home/${USERNAME}/.ssl/cert.pem \
-subj "/CN=localhost"
chmod -R 600 /home/${USERNAME}/.ssl
fi

# Configure code-server
mkdir -p ${HOME}/.config/code-server
cat > ${HOME}/.config/code-server/config.yaml << EOF
bind-addr: 0.0.0.0:${PORT}
auth: password
password: ${PASSWORD}
cert: /etc/code-server/self-signed.crt
cert-key: /etc/code-server/self-signed.key
EOF

chown -R ${USERNAME}:${USERNAME} ${HOME}/.config

# Switch to the user and start code-server
exec sudo -u ${USERNAME} code-server \
--bind-addr "0.0.0.0:${PORT}" \
--user-data-dir "${HOME}/.local/share/code-server" \
--config "${HOME}/.config/code-server/config.yaml"
# Run code-server with specified environment variables
exec code-server --auth password --cert /home/${USERNAME}/.ssl/cert.pem --port ${PORT:-8443}

0 comments on commit 5b2b86d

Please sign in to comment.