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 anythingDocker DX (docker.docker)Docker's own language service for more advanced Dockerfile, Compose and Bake IntelliSense; recommended alongside Container ToolsDocker or Docker DesktopMust be installed and on your PATH; the extension drives the docker programRecognised filesDockerfile, Dockerfile.*, *.dockerfile, Containerfile and .dockerignore / .containerignoreCreating & Editing Docker Files
Containers: Add Docker Files to Workspace…Command Palette: generates a Dockerfile and .dockerignore for C#, Node.js, Python, Ruby, Go or JavaContainers: Add Compose Files to Workspace…Command Palette: generates a Compose file as wellCtrl+Shift+MProblems panel: shows errors found in Dockerfiles and Compose filescontainers.enableComposeLanguageServiceIntelliSense and hovers for compose.yaml (default true)docker.languageserver.diagnostics.*Checks such as instructionCasing, directiveCasing, instructionCmdMultiple, instructionEntrypointMultiple, instructionWorkdirRelative: each ignore, warning (default) or errordocker.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 stringsContainer Explorer (Activity Bar)
ContainersRight-click: Start, Stop, Restart, Remove, View Logs, Attach Shell, Inspect, Stats, Download, Open in Integrated or External Browser, Filter, PruneImagesRight-click: Run, Run Interactive, Build Image…, Tag…, Push…, Pull, Copy Full Tag, Inspect, Remove, Show / Hide dangling images, PruneRegistriesConnect Docker Hub, Azure Container Registry or another registry; pull, push, delete, deploy to AzureNetworks / VolumesCreate, Inspect, Remove and Prune; built-in networks can be hiddenContextsSwitch between Docker contexts (for example a remote engine) with UseConfigure Explorer…On each view: choose how items are grouped, labelled and sortedContainers: Prune System…Remove unused containers, images, networks and volumes in one goDocker Compose
Compose UpStart every service in the Compose fileCompose Up - Select ServicesChoose which services to startCompose Down / Compose Down - Select ServicesStop and remove themCompose RestartRestart the servicesCompose GroupsAfter Compose Up, the Containers view groups the services: Compose Logs, Start, Stop, Restart, Down for the whole groupcontainers.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, Nonecontainers.containers.label / .description / .sortByDefaults FullTag; [ContainerName, Status]; CreatedTimecontainers.images.groupBy / .label / .sortByDefaults Repository, Tag, CreatedTimecontainers.images.checkForOutdatedImagesMark images with a newer version online (default true)containers.networks.groupBy / .labelDefaults None and NetworkNamecontainers.networks.showBuiltInNetworksList the built-in networks (default true)containers.volumes.groupBy / .labelDefaults None and VolumeNamecontainers.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 settingscontainers.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, customOptionsdocker-run → dockerRunimage, containerName, command, entrypoint, env, envFiles, network, remove (default false), customOptionsdockerRun.ports[{ "containerPort": 8000, "hostPort": 8000, "protocol": "tcp" }]; portsPublishAll defaults to truedockerRun.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, projectNameUse 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 projectDebugger 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 containerDebug in ContainerThe label of the launch configuration that is addedPython portThe Python task's debugPort (default 5678) must match the port the debugger listens onDocker CLI You'll Meet (my knowledge)
docker build -t name .Build an image from the Dockerfile in this folderdocker run --rm -d -p 8000:8000 nameRun in the background, remove when stopped, map host port 8000 to container port 8000docker run --rm -it name shRun interactively with a shelldocker ps / docker ps -aList running containers / all containersdocker logs -f idFollow a container's logdocker exec -it id shOpen a shell in a running containerdocker stop id / docker rm idStop a container / delete itdocker images / docker rmi nameList images / remove onedocker system pruneRemove unused data (asks first)docker compose up -d --buildBuild and start Compose services in the backgrounddocker compose down / docker compose logs -fStop and remove / follow all logsA 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 warningsA .dockerignore fileAlways add one: list node_modules, .git and .env to keep images small and free of secretsThe 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.