Managing docker volumes using docker compose

Docker Compose is a tool used for defining and running multi-container Docker applications. The main advantage of using Docker Compose is that it works in all environments such as production, staging, development, testing, as well as CI workflows

Manage Docker Volumes using Docker Compose
Docker Volumes are mounted on Docker containers to store data the Docker Container generates while running. We can manage it with the help of Compose.
Docker Volumes are just parts of Docker Host’s filesystem (a directory or a block device formatted with a filesystem) that can be mounted inside a container at any desired location of the container’s filesystem

prerequisites for this setup
We will use the following version of Docker runtime and Docker-Compose:
#Docker version 20.10.6, build 370c289
#docker-compose version 1.29.1, build c34c88b2
#Compose file version 3: Works with 1.13.0 and above

Steps to use Docker Compose to manage Docker
Create a directory called ComposeSamples and within it create a file called docker-compose.yaml
# mkdir ComposeSamples
# cd ComposeSamples/
# cat docker-compose.yml
version: "3.0"
services:
web:
image: ghost:latest
ports:
- "2368:2368"
volumes:
- cms-content:/var/lib/ghost/content

volumes:
cms-content:
The above Compose file has a website running with the latest image of ghost CMS from Docker Hub’s official repository. A Ghost container’s default port 2368 and default mount point for the website’s contents /var/lib/ghost/content are considered as the container’s official documentation here.The volume called cms-content mounted at /var/lib/ghost/content.

Syntax and Verbosity
The syntax we can introduce to a volume using docker-compose is quite simple. We will start with something similar to a container and mention the name of the volume that we want to mount inside it.
#cat docker-compose2.yaml
version: "3.0"
services:
web:
image: ghost:latest
ports:
- "2368:2368"
volumes:
- /var/lib/ghost/content

If we want to be a bit more verbose, then you will have to mention the Docker Volume as a top-level definition
#cat docker-compose3.yaml
version: "3.0"
services:
web:
image: ghost:latest
ports:
- "2368:2368"
volumes:
- cms-content:/var/lib/ghost/content
## Define that cms-content is in fact a volume.
volumes:
cms-content:

Bind Mounts
Bind mounts are parts of the host file system that can be mounted directly inside the Docker container. To introduce a bind mount, simply mention the host directory you want to share and the mount point inside the Docker container where it ought to be mounted:
#volumes:
- /home//projects/ghost: /var/lib/ghost/content
We can use any path instead of /home//projects/ghost on the Docker host we want, as long as we have access to it.

When we use a more verbose syntax, the syntax file will be as given below
volumes:
- type: bind
source: /home/USER/projects/ghost
target: /var/lib/ghost/content

The application data are backed up, and secure and the containers as disposable environments, even in production.



Recent Comments

No comments

Leave a Comment