I am trying to setup a DevContainer for Dapr to make dev environment setup easy. I would like to run Dapr Init automatically so that there are no manual steps needed to run services.
I put "dapr init" in the postStartCommand, but when it runs it says there is no Docker Runtime available.
❌ could not connect to docker. docker may not be installed or running
Immediately after that fails, I can manually run "dapr init" in a terminal and it works appropriately.
I have tried giving it more time by adding a "sleep 30", but that doesn't seem to help.
Is there anything I can do to ensure the docker runtime is up and running so I can automatically initialize dapr?
Here is my devcontainer definition
{
"name": "Dapr Quickstarts Codespace",
"dockerFile": "Dockerfile",
"customizations": {
"vscode": {
"extensions": [
"golang.go",
"ms-azuretools.vscode-dapr",
"ms-azuretools.vscode-docker",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"ms-dotnettools.csdevkit"
]
}
},
"features": {
"ghcr.io/dapr/cli/dapr-cli:0": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
"mounts": [
// Mount docker-in-docker library volume
"source=codespaces-linux-var-lib-docker,target=/var/lib/docker,type=volume"
],
"postStartCommand": "dapr init",
// Always run image-defined docker-init.sh to enable docker-in-docker
"overrideCommand": false,
"remoteUser": "codespace",
"runArgs": [
// Enable ptrace-based debugging for Go in container
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined",
// Enable docker-in-docker configuration
"--init",
"--privileged"
]
}
Since the
dapr initcommand does rely on Docker, which apparently has not fully initialized whenpostStartCommandis executed, you might consider, as a workaround, a script that checks for Docker availability before running thedapr initcommandCreate a file named
check-docker.sh:And update your
devcontainer.jsonfile to point to thecheck-docker.shscript in thepostStartCommandfield:That would mean the
postStartCommandexecution may be blocking the Docker initialization process.That makes sense, especially if the Docker service is being initialized in the same context or process tree as the
postStartCommand. In that case, thepostStartCommandwould block Docker from fully initializing until it completes.A possible solution could be to offload the
dapr initcommand to a background process, which would allowpostStartCommandto complete, and Docker to continue its initialization. That could be achieved by updating the script to rundapr initin the background or after a delay, allowing thepostStartCommandto complete, and potentially allowing Docker to finish initializing.The
check-docker.shscript, which now offloadsdapr initto a background process, would be (using "nohup"):And the updated
postStartCommandin yourdevcontainer.jsonwould remain the same:The
postStartCommandshould complete immediately, potentially allowing Docker to finish initializing, while thedapr initcommand will be attempted in a separate background process.If you place
check-docker.shat the root of your project, the path would just be"/check-docker.sh".