Ellis Valentiner
Ellis Valentiner

Reputation: 2206

Docker from python

Please be gentle, I am new to docker.

I'm trying to run a docker container from within Python but am running into some trouble due to environment variables not being set.

For example I run

import os
os.popen('docker-machine start default').read()
os.popen('eval "$(docker-machine env default)"').read()

which will start the machine but does not set the environment variables and so I can not pass a docker run command.

Ideally it would be great if I did not need to run the eval "$(docker-machine env default)". I'm not really sure why I can't set these to something static every time I start the machine.

So I am trying to set them using the bash command but Python just returns an empty string and then returns an error if I try to do docker run my_container.

Error:

Post http:///var/run/docker.sock/v1.20/containers/create: dial unix /var/run/docker.sock: no such file or directory.
* Are you trying to connect to a TLS-enabled daemon without TLS?
* Is your docker daemon up and running?

Upvotes: 1

Views: 509

Answers (1)

dnephin
dnephin

Reputation: 28040

I'd suggest running these two steps to start a machine in a bash script first. Then you can have that same bash script call your python script and access docker with docker-py

import docker
import os

docker_host = os.environ['DOCKER_HOST']
client = docker.Client(docker_host)
client.create(...)
...

Upvotes: 1

Related Questions