Docker Development Container Cheat Sheet

Common commands for building, running, inspecting, and operating a development workflow inside Docker containers.

Quick Start

# Build an image from the current directory.

docker build -t my-app:dev .

​# Run an interactive shell in a new container.

docker run --rm -it my-app:dev sh

​# Run the app and publish a port.

docker run --rm -it -p 3000:3000 my-app:dev​

# Mount the current project into the container.

docker run --rm -it -v "$PWD:/app" -w /app my-app:dev sh

Images

# List local images.

docker images​

# Build without using cache.

docker build --no-cache -t my-app:dev .​

# Remove an image.

docker rmi my-app:dev

​# Remove unused images.

docker image prune

Containers

# List running containers.

docker ps​

# List all containers, including stopped ones.

docker ps -a

​# Stop a container.

docker stop <container>​

# Remove a stopped container.

docker rm <container>

​# Stop and remove a running container.

docker rm -f <container>

Running Dev Commands

# Run a one-off command.

docker run --rm my-app:dev npm test​

# Run a shell with the project mounted.

docker run --rm -it -v "$PWD:/app" -w /app my-app:dev bash​

# Run as your host user to avoid root-owned files.

docker run --rm -it \
-u "$(id -u):$(id -g)" \
-v "$PWD:/app" \
-w /app \
my-app:dev sh

​# Pass environment variables.

docker run --rm -it -e NODE_ENV=development my-app:dev​

# Load environment variables from a file.

docker run --rm -it --env-file .env my-app:dev

Logs And Debugging

# Show logs for a container.

docker logs <container>​

# Follow logs.docker logs -f <container>

​# Open a shell in a running container.

docker exec -it <container> sh​

# Inspect container configuration.

docker inspect <container>​

# Show live container resource usage.

docker stats

Volumes

# List volumes.

docker volume ls

​# Create a named volume.

docker volume create my-app-data

​# Use a named volume.

docker run --rm -it -v my-app-data:/data my-app:dev

​# Remove unused volumes.

docker volume prune

Networks

# List networks.

docker network ls​

# Create a network.

docker network create my-dev-network

​# Run a container on a network.

docker run --rm -it --network my-dev-network my-app:dev

Docker Compose

# Start services.

docker compose up

​# Start services in the background.

docker compose up -d​

# Rebuild and start services.

docker compose up --build​

# Stop services.

docker compose down

​# Stop services and remove volumes.

docker compose down -v​

# View running Compose services.

docker compose ps​

# Follow service logs.

docker compose logs -f

​# Follow logs for one service.

docker compose logs -f <service>​

# Run a one-off command in a service container.

docker compose run --rm <service> npm test​

# Open a shell in a running service container.

docker compose exec <service> sh

Typical Development Loop

# 1. Build or rebuild the dev image.

docker compose build

​# 2. Start the app and dependencies.

docker compose up

​# 3. Run tests in the app service.

docker compose run --rm <service> npm test

​# 4. Open a shell when something needs debugging.

docker compose exec <service> sh

​# 5. Stop everything when finished.

docker compose down

Cleanup

# Remove stopped containers, unused networks, dangling images, and build cache.

docker system prune

​# Remove unused images too.

docker system prune -a

​# Remove unused volumes.

docker volume prune​

# Show disk usage by Docker objects.

docker system df

Useful Patterns

# Name a container so it is easier to reference.

docker run --name my-app-dev --rm -it my-app:dev

​# Keep STDIN open and allocate a terminal.

docker run -it my-app:dev sh​

# Automatically delete the container after it exits.

docker run --rm my-app:dev​

# Publish host port 8080 to container port 3000.

docker run -p 8080:3000 my-app:dev​

# Mount a read-only project directory.

docker run --rm -it -v "$PWD:/app:ro" -w /app my-app:dev sh​

# Copy a file from a container to the host.

docker cp <container>:/path/in/container ./local-path​

# Copy a file from the host to a container.

docker cp ./local-path <container>:/path/in/container

Placeholders

<container>: container name or ID from docker ps.

<service>: service name from docker compose ps or docker-compose.yml.

my-app:dev: local image name and tag.

3000:3000: host port to container port mapping.