When a custom image is the right tool
Use Databricks Container Services (DCS) when every cluster start spends minutes on pip install or apt-get, when PyPI is blocked and a private mirror isn't an option, or when a job needs native libraries (GDAL, ODBC drivers, a vendor SDK) pinned to exact versions. The image is built and scanned once and then pulled at cluster start. That makes the cluster reproducible, and an init script can't half-install something at 02:00.
There are two DCS variants. Pick the right one before you write a Dockerfile:
| Variant | Status | Compute | Base image | Runtime |
|---|---|---|---|---|
| DCS for dedicated compute | GA | Dedicated (single user) access mode | databricksruntime/standard:<version> and related images | Standard runtimes such as 16.4 LTS |
| DCS for standard compute | Beta | Standard access mode | databricksruntime/environment:v5-standard / v6-standard | 18 LTS / 19 and above |
Warning
The two variants need different base images. If you build an image on the standard base and launch it on dedicated compute (or the reverse), the launch fails. Put the access mode in the image tag, for example
dbx-dedicated-16.4-1.
Limitations to accept up front:
- Databricks Runtime for Machine Learning is not supported with DCS.
- Docker
CMDandENTRYPOINTare ignored. Work that must run at start belongs in an init script. - On the standard variant, compute-scoped libraries and private package repos are not supported. Put all Python dependencies in the image.
Step 1: enable DCS in the workspace
A workspace admin enables DCS for dedicated compute once:
databricks workspace-conf set-status --json '{"enableDcs": "true"}'
databricks workspace-conf get-status enableDcs
DCS for standard compute is turned on separately, from the workspace Previews page.
If the Docker tab is still missing on the create-compute page, either DCS isn't enabled or a compute policy hides docker_image.url.
Step 2: build the image
Start from a Databricks base image. Tags ending in -LTS get patches; other tags are not patched regularly. Install Python packages into the Databricks Python environment, not the system Python:
FROM databricksruntime/standard:16.4-LTS
# OS packages your code needs
RUN apt-get update \
&& apt-get install -y --no-install-recommends unixodbc libgdal-dev \
&& rm -rf /var/lib/apt/lists/*
# Python packages go into the environment that runs notebooks and jobs
COPY requirements.txt /tmp/requirements.txt
RUN /databricks/python3/bin/pip install --no-cache-dir -r /tmp/requirements.txt
If you build from scratch instead, the image needs a JDK on PATH that matches the runtime's default Java version, plus bash, iproute2, coreutils, procps, sudo and acl. Ubuntu is the recommended OS. databricksruntime/minimal is a smaller starting point.
For the standard-compute variant:
FROM databricksruntime/environment:v6-standard
RUN /databricks/python3/bin/python -m pip install <your-package>
Before you push, smoke-test the image locally:
docker run --rm <image> /databricks/python3/bin/python -c "import pandas, pyarrow; print('OK')"
Caution
An image that runs on your laptop can still fail on Databricks, or silently turn off runtime features. Never point production job clusters at an image that hasn't run the real job on a test cluster.
Step 3: push to Azure Container Registry
ACR=mydbxacr
az acr login --name $ACR
docker build -t $ACR.azurecr.io/dbx/etl:dbx-dedicated-16.4-1 .
docker push $ACR.azurecr.io/dbx/etl:dbx-dedicated-16.4-1
Use immutable, versioned tags. If you use :latest, two workers in the same cluster can end up on different images after a push mid-scale-up, and a rollback means re-pushing an old image.
Step 4: give Databricks pull access through a secret scope
DCS authenticates to ACR with basic auth, using a service principal's application ID and secret. Grant the principal AcrPull only:
ACR_ID=$(az acr show --name $ACR --query id -o tsv)
az ad sp create-for-rbac --name sp-dbx-acr-pull --role AcrPull --scopes $ACR_ID
Store the appId and password from the output in a secret scope rather than in cluster JSON:
databricks secrets create-scope dcs
databricks secrets put-secret dcs acr-user --string-value "<appId>"
databricks secrets put-secret dcs acr-password --string-value "<password>"
databricks secrets put-acl dcs data-engineers READ
The client secret expires. Add its expiry date to the same calendar as your other service principal secrets. When it lapses, every DCS cluster fails at image pull.
Step 5: create the cluster
{
"cluster_name": "etl-dcs",
"spark_version": "16.4.x-scala2.12",
"node_type_id": "Standard_D8ds_v5",
"num_workers": 2,
"data_security_mode": "SINGLE_USER",
"single_user_name": "<user-email-or-sp-application-id>",
"autotermination_minutes": 30,
"docker_image": {
"url": "mydbxacr.azurecr.io/dbx/etl:dbx-dedicated-16.4-1",
"basic_auth": {
"username": "{{secrets/dcs/acr-user}}",
"password": "{{secrets/dcs/acr-password}}"
}
},
"spark_conf": {
"spark.databricks.unityCatalog.volumes.enabled": "true"
}
}
databricks clusters create --json @etl-dcs.json
The spark.databricks.unityCatalog.volumes.enabled setting is needed for Unity Catalog volumes on DCS compute. Federated Hive metastores need spark.databricks.unityCatalog.hms.federation.enabled true. For a job cluster, put the same docker_image block in the job's new_cluster.
Instance pools
If the cluster uses a pool, create the pool through the Instance Pools API with preloaded_docker_images set. The cluster's docker_image must match it. Otherwise the create fails with 'docker_image' must be provided for cluster created with instance pool: <pool-id>.
Start-up tasks
Launch order is: VMs acquired, image pulled, container created, runtime code copied in, then init scripts run. Anything that needs the running container, such as starting a monitoring agent, belongs in an init script:
"init_scripts": [{ "workspace": { "destination": "/Shared/init/start-agent.sh" } }]
On the standard variant, init scripts can't change the Python environment. Install everything in the image.
Troubleshooting a cluster that won't start
Read the termination reason and the event log first. Exact reason codes vary by release, so match on the text as well:
databricks clusters get <cluster-id> -o json | jq '.state, .termination_reason'
databricks clusters events <cluster-id> -o json | jq '.[0:10] | .[] | {type, details}'
| Symptom | Likely cause | Fix |
|---|---|---|
| Docker tab missing in the UI | DCS not enabled, or the policy hides docker_image.url | workspace-conf set-status, or allow the attribute in the policy |
| Image pull failure, unauthorized | Expired SP secret, wrong appId, or no AcrPull | Rotate the secret in the scope; check az role assignment list --scope $ACR_ID |
| Image pull failure, timeout or name not resolved | ACR has public access disabled and the workspace VNet can't reach the private endpoint | Link the privatelink.azurecr.io DNS zone to the workspace VNet; allow the subnets in the ACR firewall |
| 429 pulling from Docker Hub | Docker Hub rate limits | Mirror the base image into ACR and pull from there |
| Container starts, then Spark never comes up | From-scratch image missing JDK, procps, iproute2, sudo or acl | Rebuild from a databricksruntime/* base |
| Launch fails on one access mode only | Standard image on dedicated compute, or the reverse | Rebuild on the matching base image |
| Odd networking inside the container | Image or daemon config uses Docker's default 172.17.0.0/16, which clashes with your network | Avoid that range in your VNet and image config |
ModuleNotFoundError for a package you installed | Installed with system pip instead of /databricks/python3/bin/pip | Fix the Dockerfile and rebuild |
To check name resolution and the route to ACR, use a working non-DCS cluster in the same VNet:
%sh
nslookup mydbxacr.azurecr.io
curl -sS -o /dev/null -w "%{http_code}\n" https://mydbxacr.azurecr.io/v2/
A 401 from /v2/ means the network path is fine and only auth is left to fix. A timeout means the problem is the network.
Prevent it next time
- Build images in CI from a pinned
-LTSbase, scan them, and push immutable tags. Never hand-push to a production repository. - Pin
docker_image.urlin a compute policy for production jobs so nobody points a job at an untested tag:
{ "docker_image.url": { "type": "fixed", "value": "mydbxacr.azurecr.io/dbx/etl:dbx-dedicated-16.4-1" } }
- Rebuild monthly, and whenever you move to a new runtime LTS. The image's base version and
spark_versionshould change together. - Alert on the expiry date of the ACR pull principal's secret.
- Keep one non-DCS fallback cluster definition so you can still run urgent jobs if the registry is unreachable.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get