Matthew Blau
12/28/2020, 7:41 PMZanie
docker
create_container
function**Using volumes**
Volume declaration is done in two parts. Provide a list of
paths to use as mountpoints inside the container with the
``volumes`` parameter, and declare mappings from paths on the host
in the ``host_config`` section.
.. code-block:: python
container_id = cli.create_container(
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
host_config=cli.create_host_config(binds={
'/home/user1/': {
'bind': '/mnt/vol2',
'mode': 'rw',
},
'/var/www': {
'bind': '/mnt/vol1',
'mode': 'ro',
}
})
)
You can alternatively specify binds as a list. This code is equivalent
to the example above:
.. code-block:: python
container_id = cli.create_container(
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
host_config=cli.create_host_config(binds=[
'/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro',
])
)
host_config
as wellMatthew Blau
12/28/2020, 7:56 PMZanie
create_host_config
to generate a formatted config for you, e.g. cli = docker.APIClient()
host_config = cli.create_host_config(binds=["/path/in/container:/volume/you/added"])
host_config={"Binds": ["/path/in/container:/volume/you/added"]}
Matthew Blau
12/28/2020, 8:07 PMZanie
CreateContainer
callcontainer = CreateContainer(
image_name="testphp_app:latest",
volumes= "/home/mblau/projects/testphp",
command= "php DatabaseTest.php"
)
docker-compose
doesn’t seem relevant to the code you postedMatthew Blau
12/28/2020, 8:11 PMZanie
Matthew Blau
12/28/2020, 8:43 PMcontainer = CreateContainer(
image_name="testphp_app:latest",
host_config={"Binds": "/home/mblau/projects/testphp/:/app"},
volumes= "/home/mblau/projects/testphp/:/app",
command= "php DatabaseTest.php"
)
Zanie
php /app/DatabaseTest.php
Binds
needs to be a list I thinkMatthew Blau
12/28/2020, 9:05 PMZanie
create_host_config
for you with the contents of host_config
binds=[
'/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro',
]
syntaxMatthew Blau
12/28/2020, 9:07 PMZanie
host_config={"binds": ["/home/mblau/projects/testphp/:/app"]}
Matthew Blau
12/28/2020, 9:10 PMZanie
Matthew Blau
12/28/2020, 9:12 PMZanie
Matthew Blau
12/28/2020, 9:41 PMZanie