Volume Management
Docker volumes can be used for persistent data storage. Volumes are easy to manage and allow for a variety of options when it comes to data storage and access.
Creating a Volume
To create a new volume, use the following command:
docker volume create [OPTIONS] [VOLUME]
Where [OPTIONS]
are optional parameters and [VOLUME]
is the name of the volume. If no name is specified, Docker will generate a random name.
Here’s an example of creating a volume named db-data
:
docker volume create db-data
For more information, see the official documentation.
Mounting a Volume
To mount a volume to a container, use the following command:
docker run -d -v [VOLUME:MOUNTPOINT] [IMAGE] [COMMAND]
Where [VOLUME]
is the name of the volume, [MOUNTPOINT]
is the path in the container where the volume will be mounted, [IMAGE]
is the Docker image, and [COMMAND]
is the command to run in the container.
Here’s an example of mounting the db-data
volume to a container at the /data
mountpoint:
docker run -d -v db-data:/data python-docker python3 -m flask run --host=0.0.0.0
For more information, see the official documentation.
Managing Volumes
To manage volumes, you can use the following commands:
docker volume ls
: list all volumesdocker volume inspect [VOLUME]
: show detailed information about a volumedocker volume rm [VOLUME]
: remove a volumedocker volume prune
: remove all unused volumes
For more information, see the official documentation.
Volume Plugins
Docker also supports volume plugins, which allow for integration with external storage systems such as Amazon EBS. For more information, see the official documentation.
Python Example
Here’s an example of creating and mounting a volume using the cdk8s
library in Python:
from cdk8s import App, Chart
from cdk8s_plus_26 import KubernetesApi, Container, Volume, VolumeMount
app = App()
chart = Chart(app, "my-chart")
# Create a volume
volume = Volume(chart, "db-data", type="aws-ebs")
# Mount the volume to a container
container = Container(chart, "my-container",
image="python-docker",
volume_mounts=[VolumeMount(volume, "/data")]
)
# Run the container
KubernetesApi(chart).run(container)
app.synth()
For more information, see the official documentation.
Removing Volumes
To remove a volume, use the following command:
docker volume rm [VOLUME]
To remove all unused volumes, use the following command:
docker volume prune
For more information, see the official documentation and DigitalOcean tutorial.
Calling the Docker CLI from Python
To call the Docker CLI from Python, you can use the python-on-whales
library. Here’s an example of creating a volume using python-on-whales
:
import python_on_whales as podman
podman.volume("create", "db-data")
For more information, see the official documentation.
Using Volumes for Development
Volumes can be used for development to keep data persistent between container restarts. Here’s an example of creating a volume for a database:
docker volume create db-data
Then, when running the container, mount the volume to the container:
docker run -d -v db-data:/data [IMAGE] [COMMAND]
For more information, see the official documentation.