Rancher for Docker
If we got:
RuntimeError: Docker is not running. Please start Docker and try again
Try to set the current environment variable (in the specific virtual environment if applicable).
export DOCKER_HOST=unix:///Users/username/.rd/docker.sock
Check and confirm
`os.environ[‘DOCKER_HOST’] = ‘unix:///Users/cshen2/.rd/docker.sock’
We need rebuild the specific containers.
Common commands:
docker context create rancher-desktop
docker context use rancher-desktop
docker context ls
docker context inspect rancher-desktop
curl --unix-socket /Users/username/.rd/docker.sock http://v1.24/version
A simple script to test Docker
import docker
import os
print("DOCKER_HOST:", os.getenv('DOCKER_HOST'))
print("Initializing Docker client...")
try:
= docker.from_env()
client if client.ping():
print("Docker client initialized and server ping successful")
except Exception as e:
print(f"Docker client initialization or ping failed: {e}")
raise e
If we initiated a docker client
= docker.from_env() docker_client
and need to get the container, we should use
docker_client.containers.get(container_name)
avoid using
docker.DockerClient().containers.get(container_name)
The latter might work for Docker Desktop but could cause issue with Rancher.
(An alternative to the above might be to explose Rancher’s Docker compatible API over the default Unix socket docker context create rancher-desktop --docker "host=unix:///var/run/docker.sock"
)