The documentation for Docker 1.10.2 (API version 1.22) mentions under
the "Create a container"[1] section that `HostConfig.Binds` can be given
a "container path" which will automatically "create a new volume for the
container."
I interpreted this to mean it that the following two commands should
have the same net result:
# Create container with data volume via REST API
curl --unix-socket /var/run/docker.sock -XPOST \
http://localhost/containers/create \
-H"Content-Type: application/json" \
-d'{
"Image": "<image-id>",
...
"HostConfig": {
"Binds": [
"/some/data/volume"
]
}
}'
# Create container with data volume via CLI
docker create -v /some/data/volume <image-id> <command>
However, this turned out not the be the case, as the former would create
a mount with no source and no corresponding volume:
...
"Mounts": [
{
"Source": "",
"Destination": "/some/data/volume",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
...
"Config": {
...
"Volumes": null,
...
}
...whereas the latter would create a volume and mount it:
...
"Mounts": [
{
"Name": "9b38af46d6..."
"Source": "/var/lib/docker/volumes/9b38af46d6.../_data",
"Destination": "/some/data/volume",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
...
"Config": {
...
"Volumes": {
"/some/data/volume": {}
},
...
}
However, if you instead specify the data volume via the `Volumes` key,
then it works as expected, e.g.
curl --unix-socket /var/run/docker.sock -XPOST \
http://localhost/containers/create \
-H"Content-Type: application/json" \
-d'{
"Image": "...",
...
"Volumes": {"/some/data/volume": {}}
}'
...will create a data volume and mount it.
Thus the documentation is either incorrect, or this is a bug and the
ability to create a data volume via `HostConfig.Binds` does not
work as advertised for API version 1.22 (and likely others).
I concluded that the documentation was incorrect. Since I've only
verified this behavior for Docker 1.10.2, I updated the docs for
API versions 1.22 and 1.23, but this may apply to other versions as
well.
[1] https://docs.docker.com/engine/reference/api/docker_remote_api_v1.22/#create-a-container
Signed-off-by: Shane da Silva <shane@dasilva.io>
| ... | ... |
@@ -233,6 +233,9 @@ Create a container |
| 233 | 233 |
"Propagation": "" |
| 234 | 234 |
} |
| 235 | 235 |
], |
| 236 |
+ "Volumes": {
|
|
| 237 |
+ "/volumes/data": {}
|
|
| 238 |
+ } |
|
| 236 | 239 |
"WorkingDir": "", |
| 237 | 240 |
"NetworkDisabled": false, |
| 238 | 241 |
"MacAddress": "12:34:56:78:9a:bc", |
| ... | ... |
@@ -349,7 +352,6 @@ Json Parameters: |
| 349 | 349 |
- **StopSignal** - Signal to stop a container as a string or unsigned integer. `SIGTERM` by default. |
| 350 | 350 |
- **HostConfig** |
| 351 | 351 |
- **Binds** – A list of volume bindings for this container. Each volume binding is a string in one of these forms: |
| 352 |
- + `container_path` to create a new volume for the container |
|
| 353 | 352 |
+ `host_path:container_path` to bind-mount a host path into the container |
| 354 | 353 |
+ `host_path:container_path:ro` to make the bind-mount read-only inside the container. |
| 355 | 354 |
+ `volume_name:container_path` to bind-mount a volume managed by a volume plugin into the container. |
| ... | ... |
@@ -467,7 +469,9 @@ Return low-level information on the container `id` |
| 467 | 467 |
"StdinOnce": false, |
| 468 | 468 |
"Tty": false, |
| 469 | 469 |
"User": "", |
| 470 |
- "Volumes": null, |
|
| 470 |
+ "Volumes": {
|
|
| 471 |
+ "/volumes/data": {}
|
|
| 472 |
+ }, |
|
| 471 | 473 |
"WorkingDir": "", |
| 472 | 474 |
"StopSignal": "SIGTERM" |
| 473 | 475 |
}, |
| ... | ... |
@@ -252,6 +252,9 @@ Create a container |
| 252 | 252 |
"Propagation": "" |
| 253 | 253 |
} |
| 254 | 254 |
], |
| 255 |
+ "Volumes": {
|
|
| 256 |
+ "/volumes/data": {}
|
|
| 257 |
+ } |
|
| 255 | 258 |
"WorkingDir": "", |
| 256 | 259 |
"NetworkDisabled": false, |
| 257 | 260 |
"MacAddress": "12:34:56:78:9a:bc", |
| ... | ... |
@@ -368,7 +371,6 @@ Json Parameters: |
| 368 | 368 |
- **StopSignal** - Signal to stop a container as a string or unsigned integer. `SIGTERM` by default. |
| 369 | 369 |
- **HostConfig** |
| 370 | 370 |
- **Binds** – A list of volume bindings for this container. Each volume binding is a string in one of these forms: |
| 371 |
- + `container_path` to create a new volume for the container |
|
| 372 | 371 |
+ `host_path:container_path` to bind-mount a host path into the container |
| 373 | 372 |
+ `host_path:container_path:ro` to make the bind-mount read-only inside the container. |
| 374 | 373 |
+ `volume_name:container_path` to bind-mount a volume managed by a volume plugin into the container. |
| ... | ... |
@@ -486,7 +488,9 @@ Return low-level information on the container `id` |
| 486 | 486 |
"StdinOnce": false, |
| 487 | 487 |
"Tty": false, |
| 488 | 488 |
"User": "", |
| 489 |
- "Volumes": null, |
|
| 489 |
+ "Volumes": {
|
|
| 490 |
+ "/volumes/data": {}
|
|
| 491 |
+ }, |
|
| 490 | 492 |
"WorkingDir": "", |
| 491 | 493 |
"StopSignal": "SIGTERM" |
| 492 | 494 |
}, |