def do_docker_create(self, label, parameters, environment, name, image,
volumes, memory_limit, folders, command):
"""
Create necessary directories in a working directory
for the mounts in the containers.
Write .ini file filled with given parameters in each folder.
Create a new docker container from a given image and
return the id of the container
"""
# Create needed folders for mounts
for folder in folders:
try:
os.makedirs(folder, 0o2775)
# Path already exists, ignore
except OSError:
if not os.path.isdir(folder):
raise
# Create ini file for containers
config = configparser.SafeConfigParser()
for section in parameters:
if not config.has_section(section):
config.add_section(section)
for key, value in parameters[section].items():
# TODO: find more elegant solution for this! ugh!
if not key == 'units':
if not config.has_option(section, key):
config.set(*map(str, [section, key, value]))
for folder in folders:
with open(os.path.join(folder, 'input.ini'), 'w') as f:
config.write(f) # Yes, the ConfigParser writes to f
# Create docker container
client = Client(base_url=settings.DOCKER_URL)
# We could also pass mem_reservation since docker-py 1.10
config = client.create_host_config(binds=volumes, mem_limit=memory_limit)
container = client.create_container(
image, # docker image
name=name,
host_config=config, # mounts
command=command, # command to run
environment=environment, # {'uuid' = ""} for cloud fs sync
labels=label # type of container
)
container_id = container.get('Id')
return container_id, ""
评论列表
文章目录