Use container params in docker-py run

73 views Asked by At

a basic question that I can't found in the docs, how to pass the container params to the docker-py run function:

https://docker-py.readthedocs.io/en/stable/

We can run in a terminal the next line and will works:

docker run -e POSTGRES_DB="db" -e POSTGRES_PASSWORD="postgres" -e POSTGRES_HOST_AUTH_METHOD="trust" -e POSTGRES_USER="postgres" postgis/postgis -c max_worker_processes=15

If we try to use docker-py we can do:

import docker
client = docker.from_env()
container = client.containers.run(
      "postgis/postgis:latest",
      environment = {
        'POSTGRES_DB': "db",
        'POSTGRES_USER': "postgres",
        'POSTGRES_PASSWORD': "password",
        'POSTGRES_HOST_AUTH_METHOD': "trust"
      }
    )

There we can send almost all params to the container creation, but still can't found how to pass the -c max_worker_processes=15. How can we send that params to the container?

The run function has a command params, but does not work. I tried concat that to the image name, nothing. I can't found examples too D:

Thx!

1

There are 1 answers

0
David Maze On

Anything that appears after the image name in the docker run command is interpreted as the "command" part of the container setup; it overrides the Dockerfile CMD, which may be specially interpreted by the image's ENTRYPOINT. In the various Docker SDKs, you'd pass this as a command argument.

In docker-py specifically, the client.containers.run() method takes a command keyword argument. While the documentation says it accepts either a string or a list, you'll get the most consistent behavior if you split the command into a list of words yourself, and pass that list as arguments. (There are potentially significant security risks from assembling a command line via string interpolation, and using a list avoids many of these as well.)

container = client.containers.run(
      "postgis/postgis:latest",
      command=['-c', 'max_worker_processes=15'],
      environment={...}
)