docker run
--name opencode_container
-v ~/.config/opencode:/home/agent/.config/opencode
-v "$(pwd)":/home/agent/workspace
-itd openeuler/opencode:latest
ls /home/vscode/.local/share/opencode/auth.json
FROM debian:13.3-slim
RUN apt-get update && apt-get install -y --no-install-recommends
ca-certificates
ubuntu-keyring
curl
git
bash
ripgrep
less
jq
tzdata
build-essential
npm
node.js
nano
&& rm -rf /var/lib/apt/lists/*
ARG USERNAME=dev ARG UID=1000 ARG GID=1000 ARG CODEBOX_NAME=BOX ENV UID=${UID} ENV GID=${GID} ENV CODEBOX_NAME=${CODEBOX_NAME}
RUN
# Check if the GID already exists, if not create it
if ! getent group ${GID} >/dev/null; then
groupadd -g ${GID} ${USERNAME};
else
# If GID exists but with different name, use existing group
EXISTING_GROUP=$(getent group ${GID} | cut -d: -f1);
echo "Using existing group: ${EXISTING_GROUP} (${GID})";
fi &&
# Check if the UID already exists, if not create the user
if ! getent passwd ${UID} >/dev/null; then
useradd -m -u ${UID} -g ${GID} -s /bin/bash ${USERNAME};
else
# If UID exists, check if it's the user we want
EXISTING_USER=$(getent passwd ${UID} | cut -d: -f1);
if [ "${EXISTING_USER}" != "${USERNAME}" ]; then
echo "User ${EXISTING_USER} already exists with UID ${UID}";
# Rename the existing user if it's 'ubuntu' and we want 'dev'
if [ "${EXISTING_USER}" = "ubuntu" ] && [ "${USERNAME}" = "dev" ]; then
usermod -l ${USERNAME} ${EXISTING_USER};
groupmod -n ${USERNAME} ${EXISTING_USER} 2>/dev/null || true;
usermod -d /home/${USERNAME} -m ${USERNAME} 2>/dev/null || true;
fi;
fi;
fi
RUN mkdir -p /${CODEBOX_NAME} /home/${USERNAME}/.config/opencode
&& chown -R ${UID}:${GID} /${CODEBOX_NAME} /home/${USERNAME}/.config
ARG TARGETARCH
ARG OPENCODE_VERSION=latest
RUN ARCH="${TARGETARCH}" &&
if [ -z "${ARCH}" ]; then
if command -v dpkg >/dev/null 2>&1; then
ARCH=$(dpkg --print-architecture);
else
ARCH=$(uname -m);
fi;
fi &&
case "${ARCH}" in
amd64|x86_64) ARCH="x64" ;;
arm64|aarch64) ARCH="arm64" ;;
esac &&
if [ -z "${ARCH}" ]; then
echo "Unsupported architecture for OpenCode download" >&2;
exit 1;
fi &&
# Construct download URL based on version
if [ "${OPENCODE_VERSION}" = "latest" ]; then
DOWNLOAD_URL="https://github.com/anomalyco/opencode/releases/latest/download/opencode-linux-${ARCH}.tar.gz";
else
DOWNLOAD_URL="https://github.com/anomalyco/opencode/releases/download/${OPENCODE_VERSION}/opencode-linux-${ARCH}.tar.gz";
fi &&
echo "Downloading OpenCode from: ${DOWNLOAD_URL}" &&
# Download and install
curl -fsSL "${DOWNLOAD_URL}" -o /tmp/opencode.tar.gz &&
tar -xzf /tmp/opencode.tar.gz -C /usr/local/bin &&
chmod 0755 /usr/local/bin/opencode &&
rm /tmp/opencode.tar.gz &&
# Verify installation
opencode --version
USER
ENV GIT_CONFIG_GLOBAL=/home/${USERNAME}/.gitconfig
COPY --chown=${UID}:${GID} dotfiles/docker.bashrc /tmp/docker.bashrc
COPY --chown=${UID}:${GID} entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh
RUN ln -sf /home/${USERNAME}/config/dotfiles/bash_aliases /home/${USERNAME}/.bash_aliases
&& ln -sf /home/${USERNAME}/config/dotfiles/gitconfig /home/${USERNAME}/.gitconfig
&& ln -sf /home/${USERNAME}/config/dotfiles/gitignore /home/${USERNAME}/.gitignore
&& sed -i '5a\n# Enable aliases in non-interactive shells for OpenCode\nshopt -s expand_aliases\nif [ -f ~/.bash_aliases ]; then\n . ~/.bash_aliases\nfi\n' /home/${USERNAME}/.bashrc
&& sed -i '/^case
&& echo -e "\n# ========================================" >> /home/${USERNAME}/.bashrc
&& echo "# Custom Docker bashrc content" >> /home/${USERNAME}/.bashrc
&& echo "# ========================================" >> /home/${USERNAME}/.bashrc
&& cat /tmp/docker.bashrc >> /home/${USERNAME}/.bashrc
&& rm /tmp/docker.bashrc
ENV BASH_ENV=/home/${USERNAME}/.bashrc
VOLUME ["/${CODEBOX_NAME}"]
VOLUME ["/home/${USERNAME}/.config/opencode"]
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3
CMD opencode --version || exit 1