Docker

How are Volumes Defined in Docker Compose YAML?

Volumes are a way of preserving data created by and utilized by Docker containers. In a docker-compose.yml file, users can define volumes and attach them to services. Volumes allow data storage and sharing data between multiple containers or between a host system and a container.

This write-up will illustrate different methods to define volumes in Docker compose YAML.

How are Volumes Defined/Specified in Docker Compose YAML File?

In Docker Compose YAML, volumes are defined using the “volumes” key. There are different methods to specify volumes in Docker compose YAML, such as:

Method 1: Define Docker Host-mounted Volumes

To define a Docker host-mounted volume in Docker compose YAML, use the “volumes” key and specify the host path and container path. For instance, we have specified the “C:/Docker/Test” host path, and “/apps” is a container path:

version: '3'
services:
  web:
    image: nginx
    volumes:
      - C:/Docker/Test:/apps

    ports:
    - "8080:80"

We have defined the host-mounted volume in the “docker-compose.yml” file successfully.

Method 2: Define Docker Internal Named Volumes

To create and define the new volume in the compose file, utilize the “volumes” key, and specify the name for the new volume and container path. For instance, we have specified the “myVol” name to the volume, and “/app” is the container path:

version: '3'
services:
  web:
    image: nginx
    volumes:
      - myVol:/apps

    ports:
    - "8080:80"  

volumes:
  myVol:

We have successfully defined the volume for the “web” service. It will use the “myVol” volume to store data.

Note: While executing the “docker-compose up” command for the first time, it creates volumes and Docker uses the same volume again whenever the user executes the command later.

Method 3: Define Docker External Named Volumes

Another way of defining the volume in the Docker compose YAML file, use the already existing volume outside the compose. After that, reference it in the compose file using the “external: true” key:

version: '3'
services:
  web:
    image: nginx
    volumes:
      - myVol1:/apps

    ports:
    - "8080:80"  

volumes:
  myVol1:
    external: true

In this method, the “external” key uses the already existing volume i.e., “myVol1”.

We have explained different ways to define the volumes in Docker compose YAML file.

Conclusion

In Docker Compose YAML, volumes are defined using the “volumes” key in the “docker-compose.yml” file. There are different methods to define volumes in Docker compose YAML, such as defining Docker host-mounted volumes, defining Docker internal named volumes, and defining Docker external named volumes. This write-up has illustrated different methods to define volumes in Docker compose YAML.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.