VS Code Docker Cheat Sheet

Sidebar

Programming · VS Code Docker Cheat Sheet

Working with Docker in VS Code through the Container Tools extension: Dockerfile and Compose editing, the Container Explorer, Compose, tasks, debugging and settings. Checked against Container Tools 2.5.2 installed here. Sheet 22 in the VS Code series. The Remote and Containers sheet covers dev containers, which are a different feature.

Which Extension? (and what is installed)

Container Toolsms-azuretools.vscode-containers (version 2.5.2 here): the extension that does the work; needs a recent VS Code (1.109 or later)
Docker (ms-azuretools.vscode-docker)Now just an extension pack (2.0.0 here) that installs Container Tools; it can be uninstalled without losing anything
Docker DX (docker.docker)Docker's own language service for more advanced Dockerfile, Compose and Bake IntelliSense; recommended alongside Container Tools
Docker or Docker DesktopMust be installed and on your PATH; the extension drives the docker program
Recognised filesDockerfile, Dockerfile.*, *.dockerfile, Containerfile and .dockerignore / .containerignore

Creating & Editing Docker Files

Containers: Add Docker Files to Workspace…Command Palette: generates a Dockerfile and .dockerignore for C#, Node.js, Python, Ruby, Go or Java
Containers: Add Compose Files to Workspace…Command Palette: generates a Compose file as well
Ctrl+Shift+MProblems panel: shows errors found in Dockerfiles and Compose files
containers.enableComposeLanguageServiceIntelliSense and hovers for compose.yaml (default true)
docker.languageserver.diagnostics.*Checks such as instructionCasing, directiveCasing, instructionCmdMultiple, instructionEntrypointMultiple, instructionWorkdirRelative: each ignore, warning (default) or error
docker.languageserver.formatter.ignoreMultilineInstructionsLeave multi-line instructions as written when formatting (default false)
[dockercompose] defaults shippedTwo-space indents, spaces not tabs, autoIndent: advanced, and suggestions inside strings

Container Explorer (Activity Bar)

ContainersRight-click: Start, Stop, Restart, Remove, View Logs, Attach Shell, Inspect, Stats, Download, Open in Integrated or External Browser, Filter, Prune
ImagesRight-click: Run, Run Interactive, Build Image…, Tag…, Push…, Pull, Copy Full Tag, Inspect, Remove, Show / Hide dangling images, Prune
RegistriesConnect Docker Hub, Azure Container Registry or another registry; pull, push, delete, deploy to Azure
Networks / VolumesCreate, Inspect, Remove and Prune; built-in networks can be hidden
ContextsSwitch between Docker contexts (for example a remote engine) with Use
Configure Explorer…On each view: choose how items are grouped, labelled and sorted
Containers: Prune System…Remove unused containers, images, networks and volumes in one go

Docker Compose

Compose UpStart every service in the Compose file
Compose Up - Select ServicesChoose which services to start
Compose Down / Compose Down - Select ServicesStop and remove them
Compose RestartRestart the services
Compose GroupsAfter Compose Up, the Containers view groups the services: Compose Logs, Start, Stop, Restart, Down for the whole group
containers.composeBuildBuild images first when running Compose Up (default true)
containers.composeDetachedRun in the background (default true)
containers.orchestratorClientWhich Compose engine to use: Docker Compose, Podman Compose, nerdctl, Finch (default automatic)

Explorer Display Settings (containers.*)

containers.containers.groupByDefault Compose Project Name; others include Image, State, Status, Ports, None
containers.containers.label / .description / .sortByDefaults FullTag; [ContainerName, Status]; CreatedTime
containers.images.groupBy / .label / .sortByDefaults Repository, Tag, CreatedTime
containers.images.checkForOutdatedImagesMark images with a newer version online (default true)
containers.networks.groupBy / .labelDefaults None and NetworkName
containers.networks.showBuiltInNetworksList the built-in networks (default true)
containers.volumes.groupBy / .labelDefaults None and VolumeName
containers.contexts.showInStatusBarShow the current Docker context in the status bar (default false)
containers.truncateLongRegistryPathsShorten long registry paths in the tree (default false; length in truncateMaxLength, default 10)

Engine, Commands & Registries

containers.containerClientWhich engine: Docker, Podman, nerdctl, Finch or WSL containers (default automatic)
containers.containerCommand / containers.composeCommandOverride the program names (default empty)
Containers: Choose container runtime…Command Palette: pick the engine without editing settings
containers.environmentExtra environment variables for every container command (default none)
containers.commands.buildTemplate for building an image; default begins ${containerCommand} build --pull ${rm} -f "${dockerfile}" …
containers.commands.runTemplate for Run: ${containerCommand} run --rm -d ${exposedPorts} ${tag}
containers.commands.runInteractiveTemplate for Run Interactive: … run --rm -it ${exposedPorts} ${tag}
containers.commands.attach / .logsTemplates for Attach Shell and View Logs (exec -it …, logs --tail 1000 -f …)
containers.imageBuildContextPathFolder used as the build context (default empty: the Dockerfile's folder)
containers.promptForRegistryWhenPushingImagesAsk which registry to push to (default true)
containers.showRemoteWorkspaceWarningWarn when running containers from a remote workspace (default true)

Tasks: docker-build, docker-run, docker-compose

docker-build → dockerBuildcontext (required), dockerfile, tag, target, buildArgs, labels, platform, pull, customOptions
docker-run → dockerRunimage, containerName, command, entrypoint, env, envFiles, network, remove (default false), customOptions
dockerRun.ports[{ "containerPort": 8000, "hostPort": 8000, "protocol": "tcp" }]; portsPublishAll defaults to true
dockerRun.volumes[{ "localPath": …, "containerPath": …, "permissions": "ro" }] (rw or ro)
platform: node | python | netCoreOptional language helpers; for Python, python.file or python.module, wait (default true), debugPort (default 5678)
docker-compose → dockerComposeup (detached, build, services, profiles, scale), down (removeImages, removeVolumes), files, envFile, projectName
Use them asA preLaunchTask in launch.json, so debugging builds and starts the container first (see the Tasks and Debugging sheets)

Debugging Inside a Container

Containers: Initialize for container debuggingCommand Palette: adds the tasks and launch configuration for your project
Debugger type dockerStarts your app in a container and attaches the debugger (Node.js and .NET)
Debugger type python-containerThe Python version: debugs a Python app running in a container
Debug in ContainerThe label of the launch configuration that is added
Python portThe Python task's debugPort (default 5678) must match the port the debugger listens on

Docker CLI You'll Meet (my knowledge)

docker build -t name .Build an image from the Dockerfile in this folder
docker run --rm -d -p 8000:8000 nameRun in the background, remove when stopped, map host port 8000 to container port 8000
docker run --rm -it name shRun interactively with a shell
docker ps / docker ps -aList running containers / all containers
docker logs -f idFollow a container's log
docker exec -it id shOpen a shell in a running container
docker stop id / docker rm idStop a container / delete it
docker images / docker rmi nameList images / remove one
docker system pruneRemove unused data (asks first)
docker compose up -d --buildBuild and start Compose services in the background
docker compose down / docker compose logs -fStop and remove / follow all logs

A Reasonable Starter Set (my suggestions, not defaults)

"containers.containers.groupBy": "Compose Project Name"Keep Compose services together (the default)
"containers.contexts.showInStatusBar": trueSee which engine you are talking to
"containers.images.checkForOutdatedImages": falseIf the online checks are slow or you work offline
"docker.languageserver.diagnostics.instructionCasing": "ignore"If you prefer lower-case instructions and do not want warnings
A .dockerignore fileAlways add one: list node_modules, .git and .env to keep images small and free of secrets
The Container Explorer is empty or says Docker was not found
1) Is Docker (or Docker Desktop) installed and running? 2) Does docker version work in a terminal? 3) Run Containers: Choose container runtime… in case the extension picked a different engine. 4) If you run Docker inside WSL, see dev.containers.executeInWSL on the Remote and Containers sheet. 5) Check containers.containerCommand and containers.containerClient are not set to something that does not exist.
Dev containers and Container Tools are two features
Container Tools (this sheet) manages containers, images and Compose projects from VS Code. Dev Containers opens a whole VS Code window inside a container, using a devcontainer.json. They share Docker, so both need it running, but their settings and commands are separate.