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"
]
}
2
Answers
Since the
dapr init
command does rely on Docker, which apparently has not fully initialized whenpostStartCommand
is executed, you might consider, as a workaround, a script that checks for Docker availability before running thedapr init
commandCreate a file named
check-docker.sh
:And update your
devcontainer.json
file to point to thecheck-docker.sh
script in thepostStartCommand
field:That would mean the
postStartCommand
execution 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, thepostStartCommand
would block Docker from fully initializing until it completes.A possible solution could be to offload the
dapr init
command to a background process, which would allowpostStartCommand
to complete, and Docker to continue its initialization. That could be achieved by updating the script to rundapr init
in the background or after a delay, allowing thepostStartCommand
to complete, and potentially allowing Docker to finish initializing.The
check-docker.sh
script, which now offloadsdapr init
to a background process, would be (using "nohup
"):And the updated
postStartCommand
in yourdevcontainer.json
would remain the same:The
postStartCommand
should complete immediately, potentially allowing Docker to finish initializing, while thedapr init
command will be attempted in a separate background process.If you place
check-docker.sh
at the root of your project, the path would just be"/check-docker.sh"
.To ensure that Docker is fully available before running the "dapr init" command, you can add a delay before running the "dapr init" command in your DevContainer definition. The "sleep" command you tried didn’t work because it might not be waiting long enough. You can use a more robust approach by continuously checking if Docker is available and then running "dapr init" once it is. Here’s how you can modify your DevContainer definition to achieve this:
Then, create a shell script named
waitForDockerAndDaprInit
in your DevContainer directory with the following content:Make sure to make the shell script executable using
chmod +x waitForDockerAndDaprInit
.This script will continuously check if Docker is available, and once it is, it will run "dapr init." This should ensure that "dapr init" runs when Docker is ready.