Skip to content

Docker Volumes

Docker volumes are used to persist data generated by and used by Docker containers. This section explains how to manage and use volumes effectively.

Warning

This section is under construction.

Please do not use this section as a reference for now.


What are Docker Volumes?

Volumes are the preferred mechanism for persisting data generated and used by Docker containers. They are managed by Docker and are independent of the container's lifecycle.


Types of Docker Storage

  • Volumes: Managed by Docker and stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Volumes are the best way to persist data in Docker.
  • Bind mounts: A file or directory on the host machine is mounted into a container. They are dependent on the directory structure of the host machine.
  • tmpfs mounts: Create a temporary filesystem in the container's memory. This is useful for storing sensitive data that should not persist on disk and can be shared between containers.

Creating a Volume

You can create a volume using the docker volume create command:

docker volume create myvolume

This command creates a new volume named myvolume.

Specifying the Driver Type

You can also specify the driver type when creating a volume:

docker volume create --driver local myvolume

This command creates a new volume named myvolume using the local driver.

The driver type determines how the volume is created and managed, here are some common driver types:

Driver Category Description
local Local Storage The default driver that manages volumes on the local filesystem.
nfs Network File System A driver that manages volumes using the Network File System (NFS).
s3 Cloud Storage A driver that manages volumes using Amazon S3.
azure Cloud Storage A driver that manages volumes using Microsoft Azure.
gcp Cloud Storage A driver that manages volumes using Google Cloud Platform.

Specifying Volume Options

You can specify volume options when creating a volume, such as the type of volume and the device it should be mounted to:

docker volume create --driver local --opt type=none --opt device=/path/to/data myvolume

type=none specifies that the volume should not have a specific type.

device=/path/to/data specifies the path on the host machine where the volume should be mounted.


Listing Volumes

You can list all volumes on your system using the docker volume ls command:

docker volume ls

This command will display a list of all volumes on your system, along with their names and driver types.


Inspecting a Volume

You can inspect a volume using the docker volume inspect command:

docker volume inspect myvolume

This command will display detailed information about the volume, such as its name, driver, mount point, and labels.


Removing a Volume

You can remove a volume using the docker volume rm command:

docker volume rm myvolume

This command will remove the volume from your system.


Using Volumes in Containers

You can use volumes in containers by specifying the volume name in the docker run command:

docker run -v myvolume:/path/in/container myimage

This command will create a new container using the specified volume myvolume and mount it to the specified path /path/in/container in the container.


Volume in Dockerfile

You can also specify volumes in a Dockerfile using the VOLUME instruction.

Let's create a container with a volume, for this exemple we will create a web server running with Apache on port 80.

The volume will be used to store the website files.

First, create a directory for your web server application:

apache/
├── build/
│   ├── Dockerfile
│   └── httpd.conf
└── htdocs/
    └── index.html

Execute the following commands to create the files and directories with the required content:

mkdir -p apache/conf apache/htdocs apache/build
echo "Hello, World! Im CleSucre and I love the rain." > apache/htdocs/index.html
echo "ServerName localhost" > apache/build/httpd.conf
touch apache/build/Dockerfile

Add the following content to the Dockerfile in the apache/build directory:

# Use the official Apache image
FROM httpd:2.4

# Set the working directory in the container
WORKDIR /usr/local/apache2

# Copy the Apache configuration file into the container
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf

# Expose port 80 to allow outside connections to the ngnix server
EXPOSE 80

# Create a volume for the website files
VOLUME /usr/local/apache2/htdocs

# Set the default command to run when the container starts
CMD ["httpd-foreground"]

Build the image using the following command:

docker build -t myapache .

Create the volumes for the Apache configuration files and the website files:

docker volume create --driver local --opt type=none --opt device=/path/to/data apache-htdocs

Run the container using the following command:

docker run -d -p 8080:80 -v apache-htdocs:/usr/local/apache2/htdocs --mount type=bind,source=/home/clesucre/doc_tutorial/docker/apache/htdocs,target=/usr/local/apache2/htdocs myapache
Option Description
-d Runs the container in detached mode to keep it running in the background.
-p 8080:80 Maps port 80 on the host to port 8080 on the container.
-v apache-htdocs:/usr/local/apache2/htdocs Mounts the apache-htdocs volume to the /usr/local/apache2/htdocs directory in the container.

You can now access the web server by navigating to http://localhost:8080 in your web browser, and you should see the message Hello, World! Im CleSucre and I love the rain.

Next Step

Continue to Docker Networking to learn more about Docker Networking.

Previous | Next