Last active
April 22, 2026 10:03
-
-
Save torgeir/dbe931351d91644f295ca8a296b8e4fb to your computer and use it in GitHub Desktop.
AI in docker, inspired by https://www.bekk.no/fag/artikkel/putt-aien-i-en-sandkasse--100223
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # usage: ai [name [folder]] | |
| # ai - fzf pick from all docker-ai-* containers | |
| # ai <name> - create/attach to docker-ai-<name>, mounts pwd | |
| # ai <name> <dir> - create/attach to docker-ai-<name>, mounts dir | |
| ai() { | |
| # specified dir or current | |
| local dir="${2:-$(pwd)}" | |
| # stuff to mount in the container | |
| local -a volumes=( | |
| -v "${dir}:${dir}" -w "${dir}" | |
| -v "$HOME/.m2:/home/batman/.m2" | |
| -v "$HOME/.gradle:/home/batman/.gradle" | |
| -v "$HOME/.gitconfig.docker-ai:/home/batman/.gitconfig.private:ro" | |
| -v "$HOME/.local/share/opencode:/home/batman/.local/share/opencode" | |
| -v "$HOME/.local/state/opencode:/home/batman/.local/state/opencode" | |
| -v "$HOME/.config/opencode/opencode.json:/home/batman/.config/opencode/opencode.json:ro" | |
| ) | |
| # named container: create or attach | |
| if [[ -n "$1" ]]; then | |
| local container="docker-ai-$1" | |
| if docker ps --format '{{.Names}}' | grep -qx "$container"; then | |
| docker exec -it "$container" tmux attach | |
| elif docker ps -a --format '{{.Names}}' | grep -qx "$container"; then | |
| docker start "$container" && docker exec -it "$container" tmux attach | |
| else | |
| echo "Starting $container in $dir" | |
| docker run -dit --name "$container" "${volumes[@]}" docker-ai bash -c "exec tmux" | |
| sleep 0.5 | |
| docker exec -it "$container" tmux attach | |
| fi | |
| return | |
| fi | |
| # no args: pick from all docker-ai-* containers with fzf | |
| local selection | |
| selection=$(docker ps -a --format '{{.Names}}\t{{.Status}}' \ | |
| | grep '^docker-ai-' \ | |
| | fzf --height=40% --reverse --header="container status") || return 0 | |
| local container | |
| container=$(echo "$selection" | awk '{print $1}') | |
| docker start "$container" 2>/dev/null | |
| docker exec -it "$container" tmux attach | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # dockerfile ai in isolated environments | |
| # usage: | |
| # docker build -t docker-ai . --no-cache | |
| FROM ubuntu:24.04 | |
| # no prompts during installasjon | |
| ENV DEBIAN_FRONTEND=noninteractive | |
| # c-x e in opencode | |
| ENV EDITOR=nvim | |
| ENV VISUAL=nvim | |
| ENV GIT_EDITOR=nvim | |
| # upgrade and install | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| btop \ | |
| curl \ | |
| fd-find \ | |
| git \ | |
| gpg \ | |
| jq \ | |
| neovim \ | |
| ripgrep \ | |
| zoxide \ | |
| tmux \ | |
| tree \ | |
| unzip \ | |
| vim \ | |
| wget \ | |
| ca-certificates \ | |
| openjdk-17-jdk \ | |
| openjdk-21-jdk \ | |
| locales \ | |
| && locale-gen nb_NO.UTF-8 \ | |
| && ln -s /usr/bin/fdfind /usr/local/bin/fd \ | |
| && mkdir -p /etc/apt/keyrings && \ | |
| curl -fsSL https://raw.githubusercontent.com/eza-community/eza/main/deb.asc \ | |
| | gpg --dearmor -o /etc/apt/keyrings/gierens.gpg && \ | |
| echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" \ | |
| > /etc/apt/sources.list.d/gierens.list && \ | |
| apt-get update && apt-get install -y eza \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # java | |
| ENV JAVA_HOME=/usr/lib/jvm/java-21-openjdk-arm64 | |
| ENV PATH="$JAVA_HOME/bin:$PATH" | |
| # norsk | |
| ENV LANG=nb_NO.UTF-8 | |
| ENV LANGUAGE=nb_NO:nb | |
| ENV LC_ALL=nb_NO.UTF-8 | |
| # tmux, and markdown rendering in opencode | |
| ENV TERM=xterm-256color | |
| ENV COLORTERM=truecolor | |
| # non root user | |
| RUN useradd --create-home --shell /bin/bash batman | |
| USER batman | |
| WORKDIR /home/batman | |
| # conf | |
| RUN git clone https://github.com/torgeir/dotfiles && \ | |
| ln -s dotfiles/fzfrc .fzfrc && \ | |
| ln -s dotfiles/zshrc .zshrc && \ | |
| ln -s dotfiles/vimrc .vimrc && \ | |
| ln -s dotfiles/inputrc .inputrc && \ | |
| ln -s dotfiles/tmux.conf .tmux.conf && \ | |
| ln -s dotfiles/gitconfig .gitconfig && \ | |
| echo 'source $HOME/dotfiles/source/aliases' >> /home/batman/.bashrc | |
| # batman ownership of folders we later mount using -v | |
| RUN mkdir -p /home/batman/.local/state && \ | |
| mkdir -p /home/batman/.local/share && \ | |
| mkdir -p /home/batman/.config && \ | |
| mkdir -p /home/batman/.m2 && \ | |
| mkdir -p /home/batman/.gradle | |
| # ai | |
| RUN curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash \ | |
| && export NVM_DIR=/home/batman/.nvm \ | |
| && . "$NVM_DIR/nvm.sh" \ | |
| && nvm install 24 | |
| RUN curl -fsSL https://bun.com/install | bash | |
| RUN curl -fsSL https://opencode.ai/install | bash | |
| RUN mkdir -p /home/batman/.config/opencode | |
| # startup | |
| CMD ["/bin/bash"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment