简体   繁体   中英

How to add parameters to docker container using docker-py

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())

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM