Reputation: 4927
I'm starting a docker image from a docker application overnight the CMD command with additional parameters. Using docker it can be done
docker run -p 8888:8888 -v ~/:/tmp/home/ my_image my_start_cmd.sh --no-browser --ip=0.0.0.0
Where my_start_cmd.sh --no-browser --ip=0.0.0.0
is my CMD with parameters.
How can I run it from docker-py api using the same arguments? That's my original python code using docker api.
import docker
client = docker.from_env()
container = client.containers.run("my_image", detach=True)
for line in container.logs(stream=True):
print (line.strip())
Upvotes: 1
Views: 3063
Reputation: 4927
To simply pass arguments to docker CMD, passing the full command with arguments, and using the port mapping as a dict as ports parameter is enough as the following example:
import docker
client = docker.from_env()
container = client.containers.run(image='my_image',
command="start-notebook.sh --no-browser --ip=0.0.0.0",
ports={'8888': 8888}
)
To map volumes, as the original command line the new Low Level API (docker.APIClient()) must be used as follows:
client = docker.APIClient()
container = client.create_container(
image='my_image',
stdin_open=True,
tty=False,
command="start-notebook.sh --no-browser --ip=0.0.0.0",
volumes=['~/', '/tmp/home/'],
host_config=client.create_host_config(
port_bindings={
8888: 8888,
},
binds={
' ~/': {
'bind': '/tmp/home/',
'mode': 'rw',
}
}),
ports=[8888],
detach=True,
)
# To start the container and print the output
client.start(container=container.get('Id'))
print(response)
Upvotes: 2