I have a Docker host that is controlled using Docker API, like this.
I can create a new volume and a new container using this API very easily.
But how can I create new container and mount this volume to this container, using only API?
I have a Docker host that is controlled using Docker API, like this.
I can create a new volume and a new container using this API very easily.
But how can I create new container and mount this volume to this container, using only API?
You can mount the previously created volume (let's say volume1) to the container using the HostConfig in the create request. In the HostConfig you can specify the mounts (Mounts) you want to create.
A Mount would be like:
{
"Target": "path/in/the/container",
"Source": "volumeName",
"Type": "volume",
"ReadOnly": false
}
So the informations you should add to the create request is the next:
"HostConfig": {
"Mounts": [
{
"Target": "path/in/the/container",
"Source": "volume1",
"Type": "volume",
"ReadOnly": false
}
]
}
I also recommend you to dig into this documentation from Docker. You can find a lot of good and useful information there.
https://docs.docker.com/engine/api/v1.27/#operation/ContainerCreate