capabilities.work

Create a Volume and Mount It Into a Container

A 5-step recipe with Docker. We can help you get access.

Recipe · 5 steps

Create a Volume and Mount It Into a Container

with Docker

Create a named volume and bring up a container with it mounted.

  1. Create the named volume. Naming it explicitly is what separates it from an anonymous volume that would be swept away with the container.
  2. Create a container that mounts the named volume at the target path via a HostConfig volume mount, so data written there outlives the container.
  3. Start the container so the volume is actually mounted and the workload can begin writing to it.
  4. Inspect the container to confirm the daemon reports the named volume mounted at the expected target path.
  5. Read the volume back to capture its host mountpoint and usage data, which is what tells you whether it is in use or dangling.
What you'll need to have ready
  • volumeName: Name for the volume to create (e.g. "pgdata").
  • driver: Volume driver to use. Defaults to "local".
  • image: Image to create the container from. It must already be present locally.
  • containerName: Name to assign to the created container.
  • mountPath: Path inside the container where the volume is mounted (e.g. "/var/lib/postgresql/data").
  • env: Environment variables for the container, in "KEY=value" form.
For your developer

Arazzo workflow docker:provision-volume-and-mount, 5 operations: VolumeCreate → ContainerCreate → ContainerStart → ContainerInspect → VolumeInspect. Specs on APIs.io.