AKZN Notes

Archives for My Lazy and Forgetful Mind

Running Qwen Code in a Sidecar Docker Container

Running Qwen Code in a Sidecar Docker Container

1. Why Sidecar?

Instead of installing Qwen Code on your host, you can run it in its own container.

  • Keeps API keys out of your project repo.
  • Only shares your project folder via a bind mount.
  • Works across different projects.

2. Dockerfile

Create a Dockerfile for Qwen Code:

FROM node:20-slim

# Install Qwen Code globally
RUN npm install -g @qwen-code/qwen-code@latest

# Default working directory (overridden when running)
WORKDIR /workspace

# Default entry: bash shell
CMD ["/bin/bash"]

This keeps the container loose; you can run qwen manually inside bash.


3. docker-compose.yml

Minimal docker-compose.yml for the AI sidecar:

services:
  ai-cli:
    build: .
    stdin_open: true
    tty: true
    networks:
      - sharednetwork
    env_file:
      - .env   # contains your API keys

networks:
  sharednetwork:
    external: true

4. Environment Variables

Create .env in the same directory as docker-compose.yml:

OPENAI_API_KEY=sk-xxxx
OPENAI_BASE_URL=https://openrouter.ai/api/v1
OPENAI_MODEL=qwen/qwen3-coder:free

5. Wrapper Script

Put ai.sh somewhere in your $PATH:

#!/bin/bash
# ai.sh - run AI sidecar CLI against your project

AI_COMPOSE="/home/$(whoami)/ai-sidecar/qwen-code/docker-compose.yml"

docker compose -f "$AI_COMPOSE" run --rm -it \
  -v "$(pwd)":/workspace \
  -w /workspace \
  ai-cli

This mounts the current project folder into /workspace inside the AI container.


6. Usage

  1. From your project folder, run:

    ./ai.sh

    You will drop into bash inside the Qwen container.

  2. Inside the container, run Qwen CLI:

    qwen -p "say hello"
  3. To confirm environment keys are loaded:

    printenv | grep OPENAI

You should see your OPENAI_API_KEY, OPENAI_BASE_URL, and OPENAI_MODEL.


7. Notes

  • Each project mounts only its own folder, so Qwen only sees that context.
  • You can reuse the same sidecar setup for multiple projects.
  • For non-interactive mode, extend ai.sh to accept arguments and pass them to qwen.

Leave a Reply

Your email address will not be published.