Get the FREE Ultimate OpenClaw Setup Guide →

docker

npx machina-cli add skill wrsmith108/claude-code-docker-skill/docker --openclaw
Files (1)
SKILL.md
8.8 KB

Docker Development Skill

Execute all package installations and code execution inside Docker containers. This keeps the host machine clean and ensures consistent environments across projects.

Core Principle

NEVER run npm, node, npx, or project scripts directly on the host machine.

Instead, use docker exec or ensure the container is running the dev server.

Pre-Flight Check (MANDATORY)

Before running ANY npm/node command, Claude Code MUST verify the container is running.

Run this check first:

docker ps --filter "name=<project-name>" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

Expected output:

NAMES                  STATUS          PORTS
<project-name>-dev-1   Up X minutes    0.0.0.0:3000->3000/tcp

If container is NOT running:

# Navigate to project root first
cd /path/to/your/project

# Start container
docker-compose --profile dev up dev -d

# Verify it started
docker ps --filter "name=<project-name>"

If container shows "Exited":

# Check why it exited
docker logs <project-name>-dev-1 --tail 20

# Remove and restart
docker-compose --profile dev down
docker-compose --profile dev up dev -d

Quick Reference

Check Container Status

# List running containers for current project
docker ps --filter "name=<project-name>"

# Check container logs
docker logs <project-name>-dev-1 --tail 50

# Check if dev server is responding
curl -s http://localhost:3000 > /dev/null && echo "Server running" || echo "Server not running"

Start/Stop Containers

# Start development container (from project root)
docker-compose --profile dev up dev -d

# Stop container
docker-compose --profile dev down

# Restart container
docker-compose --profile dev restart dev

# Rebuild after Dockerfile changes
docker-compose --profile dev up dev -d --build

Execute Commands Inside Container

# Install a package
docker exec -it <project-name>-dev-1 npm install <package-name>

# Install dev dependency
docker exec -it <project-name>-dev-1 npm install -D <package-name>

# Run tests
docker exec -it <project-name>-dev-1 npm test

# Run type checking
docker exec -it <project-name>-dev-1 npm run typecheck

# Run linting
docker exec -it <project-name>-dev-1 npm run lint

# Run build
docker exec -it <project-name>-dev-1 npm run build

# Open shell inside container
docker exec -it <project-name>-dev-1 /bin/sh

# Run any arbitrary command
docker exec -it <project-name>-dev-1 <command>

When to Use Docker exec

OperationUse docker exec?Reason
npm install✅ YesPackages install in container
npm run dev❌ NoAlready running via docker-compose
npm test✅ YesTests run in container environment
npm run build✅ YesBuild happens in container
git commands❌ NoGit runs on host (manages files)
File editing❌ NoVolume mount syncs automatically
Database migrations✅ YesUses container's Node environment

Container Architecture

┌─────────────────────────────────────────────────────────────┐
│  HOST (macOS/Linux/Windows)                                 │
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  Docker Container (<project-name>-dev-1)            │   │
│  │                                                     │   │
│  │  Node 20 Alpine                                     │   │
│  │  └── node_modules/ (container-only)                 │   │
│  │  └── Dev server (port 3000)                         │   │
│  │                                                     │   │
│  │  Volume Mounts:                                     │   │
│  │  └── .:/app (source code sync)                      │   │
│  │  └── node_modules:/app/node_modules (persist deps)  │   │
│  └─────────────────────────────────────────────────────┘   │
│                         │                                   │
│                         ▼                                   │
│                   Port 3000 mapped                          │
│                         │                                   │
│                         ▼                                   │
│              http://localhost:3000                          │
└─────────────────────────────────────────────────────────────┘

Volume Mount Behavior

The docker-compose.yml mounts the project directory into the container:

volumes:
  - .:/app                           # Source code (synced)
  - /app/node_modules                # Dependencies (container-only)

What this means:

  • Source code changes on host are immediately visible in container
  • node_modules/ in container is separate from any on host
  • Hot reload works automatically with most frameworks

Troubleshooting

Container Not Running

# Check if container exists
docker ps -a --filter "name=<project-name>"

# If exited, check why
docker logs <project-name>-dev-1

# Restart
docker-compose --profile dev up dev -d

Port Already in Use

# Find what's using the port
lsof -i :3000

# Kill the process or change port in docker-compose.yml

Module Not Found Errors

# Rebuild container with fresh dependencies
docker-compose --profile dev down
docker-compose --profile dev build --no-cache dev
docker-compose --profile dev up dev -d

File Changes Not Reflecting

# Check volume mounts
docker inspect <project-name>-dev-1 | grep -A 10 "Mounts"

# Restart container
docker-compose --profile dev restart dev

Project Configuration

After installing this skill, update the placeholders for your project:

SettingExample Value
Container namemy-app-dev-1
Port3000 (or your app's port)
Node version20 (Alpine)
Dev commandnpm run dev -- --host 0.0.0.0

Environment Variables

Required env vars are loaded from .env file via docker-compose.

If a command needs a specific env var:

docker exec -it -e MY_VAR=value <project-name>-dev-1 <command>

Best Practices

  1. Always check container status before running commands
  2. Use docker exec for all npm/node operations
  3. Let volume mounts handle file syncing (no manual copying)
  4. Rebuild image after changing package.json or Dockerfile
  5. Check logs if something isn't working

Integration with Claude Code

When Claude Code needs to:

TaskAction
Install dependencydocker exec -it <project-name>-dev-1 npm install <pkg>
Run testsdocker exec -it <project-name>-dev-1 npm test
Check typesdocker exec -it <project-name>-dev-1 npm run typecheck
Build projectdocker exec -it <project-name>-dev-1 npm run build
Start dev serverContainer already runs it via docker-compose
Edit filesEdit directly (volume mount syncs)
Git operationsRun on host (not in container)

Sample docker-compose.yml

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    env_file:
      - .env
    restart: unless-stopped

  dev:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
    env_file:
      - .env
    profiles:
      - dev

Sample Dockerfile.dev

FROM node:20-alpine

WORKDIR /app

# Install dependencies for native modules
RUN apk add --no-cache python3 make g++

# Copy package files and any scripts needed for postinstall
COPY package*.json ./
COPY scripts/ ./scripts/

# Install all dependencies
RUN npm install

# Copy source code
COPY . .

# Expose port
EXPOSE 3000

# Set environment variables
ENV HOST=0.0.0.0
ENV PORT=3000
ENV NODE_ENV=development

# Start development server
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]

Source

git clone https://github.com/wrsmith108/claude-code-docker-skill/blob/main/skills/docker/SKILL.mdView on GitHub

Overview

This skill enables running npm commands, package installations, code execution, and project work inside Docker containers for isolated, reproducible environments. It keeps the host clean and ensures consistent setups across projects by containerizing the entire dev workflow.

How This Skill Works

All npm/node actions are performed inside a Docker container, typically started with docker-compose --profile dev. The core rule is to never run npm, node, npx, or project scripts on the host; use docker exec or ensure the container is running the dev server to keep environments isolated.

When to Use It

  • Installing packages or running npm commands inside the container (not on the host)
  • Running build inside the container (npm run build) to ensure consistency
  • Starting or restarting the dev server inside the container via docker-compose
  • Running tests or lint inside the container (npm test, npm run lint)
  • Executing arbitrary commands inside a running container or opening a shell

Quick Start

  1. Step 1: Ensure the container is running with docker ps for <project-name>
  2. Step 2: If not running, start it with docker-compose --profile dev up dev -d
  3. Step 3: Run commands inside the container using docker exec -it <project-name>-dev-1 <command>

Best Practices

  • Always verify the container is running with docker ps before npm/node commands
  • Use docker exec -it <project-name>-dev-1 <command> to run inside the container
  • Keep all npm, node, and project scripts inside the container; do not run them on the host
  • Use docker-compose --profile dev up dev -d to start or restart the dev environment
  • Check container health with docker logs and curl to http://localhost:3000 when needed

Example Use Cases

  • Install a package inside the container: docker exec -it <project-name>-dev-1 npm install lodash
  • Run the build inside container: docker exec -it <project-name>-dev-1 npm run build
  • Start the dev server: docker-compose --profile dev up dev -d
  • Run tests inside container: docker exec -it <project-name>-dev-1 npm test
  • Open a shell inside the container: docker exec -it <project-name>-dev-1 /bin/sh

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers