Skip to main content

Cursor Docker Development: Containerized Workflows Made Easy

Cursor Docker Development

Docker has become essential for consistent development environments, and Cursor's AI capabilities make working with containers faster and more intuitive. This guide covers Dockerfile creation, Docker Compose orchestration, Dev Containers, and debugging workflows.

Prerequisites​

Before starting:

  • Docker installed and running on your machine
  • Cursor (latest version recommended)
  • Basic familiarity with Docker concepts (images, containers, volumes)

Creating Dockerfiles with Cursor​

Generating Production-Ready Dockerfiles​

Describe your application stack and let Cursor generate optimized Dockerfiles:

Create a multi-stage Dockerfile for a Next.js 14 application that:
- Uses Node.js 20 Alpine
- Installs dependencies with npm ci
- Builds the production bundle
- Runs as a non-root user
- Uses a minimal final image

Cursor generates:

# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

# Create non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]

Optimizing Existing Dockerfiles​

Paste your Dockerfile and ask for improvements:

Optimize this Dockerfile for faster builds and smaller image size:
[paste Dockerfile]

Common optimizations Cursor suggests:

  • Combine RUN commands to reduce layers
  • Use .dockerignore to minimize context
  • Leverage build cache effectively
  • Use specific base image tags instead of latest

.dockerignore Best Practices​

node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
README.md
.env
.env.local
.env.production
.nyc_output
coverage
.vscode
.idea

Docker Compose Development Stacks​

Full-Stack Application Setup​

version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/myapp
- REDIS_URL=redis://redis:6379
volumes:
- .:/app
- /app/node_modules
depends_on:
- db
- redis

db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: myapp
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

redis:
image: redis:7-alpine
ports:
- "6379:6379"

nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- app

volumes:
postgres_data:

Hot Reload Setup​

For development with live reloading:

services:
dev:
build:
context: .
target: builder
command: npm run dev
ports:
- "3000:3000"
volumes:
- .:/app
- /app/node_modules
environment:
- CHOKIDAR_USEPOLLING=true
- WATCHPACK_POLLING=true
Volume Trick

Mount your source code with - .:/app but exclude node_modules with - /app/node_modules. This prevents container and host node_modules from conflicting.

Dev Containers in Cursor​

Setting Up .devcontainer​

Create .devcontainer/devcontainer.json:

{
"name": "Node.js & PostgreSQL",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "20"
},
"ghcr.io/devcontainers/features/git:1": {}
},
"customizations": {
"vscode": {
"extensions": [
"Prisma.prisma",
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint"
],
"settings": {
"terminal.integrated.defaultProfile.linux": "bash"
}
}
},
"postCreateCommand": "npm install",
"forwardPorts": [3000, 5432],
"remoteUser": "node"
}

Dev Container Docker Compose​

version: '3.8'
services:
app:
image: mcr.microsoft.com/devcontainers/javascript-node:20
volumes:
- ..:/workspace:cached
command: sleep infinity
network_mode: service:db

db:
image: postgres:16-alpine
environment:
POSTGRES_USER: dev
POSTGRES_PASSWORD: dev
POSTGRES_DB: workspace
volumes:
- postgres-data:/var/lib/postgresql/data

volumes:
postgres-data:

Opening a Dev Container​

  1. Press F1 in Cursor
  2. Select Dev Containers: Reopen in Container
  3. Cursor will build the container and reopen the workspace inside it
First Build

The initial container build may take several minutes. Subsequent rebuilds use Docker cache and are much faster.

Debugging Inside Containers​

Node.js Debugging​

Add to your Docker Compose:

services:
app:
build: .
ports:
- "3000:3000"
- "9229:9229" # Debug port
command: node --inspect=0.0.0.0:9229 index.js

Configure Cursor launch.json:

{
"version": "0.2.0",
"configurations": [
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app",
"skipFiles": ["<node_internals>/**"]
}
]
}

Python Debugging in Containers​

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:5678", "--wait-for-client", "app.py"]

Common Docker Workflows​

Building and Running​

# Build image
docker build -t myapp:latest .

# Run container
docker run -p 3000:3000 --env-file .env myapp:latest

# Run with volume mount
docker run -p 3000:3000 -v $(pwd):/app myapp:latest

Database Migrations​

# Run migrations in container
docker-compose exec app npx prisma migrate deploy

# Seed database
docker-compose exec app npx prisma db seed

Viewing Logs​

# Follow all logs
docker-compose logs -f

# Specific service
docker-compose logs -f app

# Last 100 lines
docker-compose logs --tail=100 app

Docker Commands Quick Reference​

CommandDescription
docker psList running containers
docker imagesList local images
docker-compose up -dStart services in background
docker-compose downStop and remove containers
docker-compose buildRebuild images
docker exec -it <container> shShell into running container
docker logs -f <container>Follow container logs
docker system pruneClean up unused resources

Troubleshooting with Cursor​

Container Won't Start​

Share your Dockerfile and error logs:

This container exits immediately with code 1. Here is my Dockerfile and logs:
[paste content]

Port Conflicts​

Error: bind EADDRINUSE :::3000

Cursor suggests checking host port availability and modifying port mappings.

Permission Issues​

Common fix for volume permission problems:

# Add to Dockerfile
RUN chown -R node:node /app
USER node

Multi-Environment Configuration​

project/
docker/
Dockerfile.dev
Dockerfile.prod
docker-compose.yml
docker-compose.prod.yml
docker-compose.override.yml

Production Compose​

version: '3.8'
services:
app:
build:
context: ..
dockerfile: docker/Dockerfile.prod
restart: always
deploy:
replicas: 3
resources:
limits:
cpus: '1'
memory: 512M

AI-Powered Dockerfile Review​

Ask Cursor to audit your setup:

Review this Docker setup for:
1. Security best practices
2. Image size optimization
3. Build cache efficiency
4. Proper signal handling (PID 1)
5. Health checks

Last updated: July 2025