Docker Volumes

A docker volume allows you to persist data generated by and used by Docker containers. A volume can exist on the host, be available in the container and doesn’t increase the size of the container using it. The container exists outside the life cycle of a given container, meaning that even if you kill the container, the volume will stay in tact. Another benefit is that a volume can be shared amongst multiple containers. Volumes can be managed by the Docker CLI which makes it extremely easy and volumes can also be hosted on cloud storage which opens the doors to a wide range of functionality and options.

You can create a volume in numerous ways.

With a docker compose you can specify:

version: "3.7"

services
    postgres:
        image: mysql
        volumes:
            # local:container
            # so our machine (host) our mysql data will be stored at ./docker/data
            # and in the container location is /var/lib/mysql/data
            - ./docker/data:/var/lib/mysql/data

The cool thing with a docker compose is that you can use environment variables for dynamic replacement of values so if you use a CI/CD, like Jenkins you can have the location be dynamic:

version: "3.7"

services
    postgres:
        image: mysql
        volumes:
            # DB_DATA_PATH should be an environment variable
            # and docker will pick that up
            - ${DB_DATA_PATH}:/var/lib/mysql/data

Using the docker API we can create and manage volumes:

# create volumes
docker volume create my-volume

# list volumes
docker volume ls

# inspect a volume
docker volume inspect my-volume
> [
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-volume/_data",
        "Name": "my-volume",
        "Options": {},
        "Scope": "local"
    }
]

# remove all unused volumes to free up spsace
docker volume prune

Using the docker run command we can attach a volume to a container as well. If the volume doesn’t exist yet, Docker will create it for you

docker run -d --name my-container -v new-volume:/app postgres:latest

Check the documentation for docker-compose volumes. Curious how to do the same in Kubernetes? Check out our post on shared volumes in kubernetes - using hostPath.

Read more about it here

Instagram Post