×


Manage Docker Volumes using Docker Compose

Are you trying to manage Docker Volumes using Docker Compose?

This guide will help you.

Docker Compose is a tool used for defining and running multi-container Docker applications.
This means that we can organize or arrange Docker containers to use them in a better way with the help of Docker Compose.
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.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Docker related configuration.
In this context, we shall look into how to use Docker Compose to manage Docker Volumes.

How to 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.
Before going into the steps to manage Docker Volumes with the help of Docker Compose we will see what are the prerequisites for this setup.


We will use the following version of Docker runtime and Docker-Compose:

1. Docker version 18.09.2, build 6247962
2. Docker-compose version 1.23.2, build 1110ad01
3. Compose file version 3: Works with 1.13.0 and above

Steps to use Docker Compose to manage Docker ?

Now we will see the steps our Support Engineers use to manage Docker volumes with the help of Docker Compose.
Taking Ghost CMS deployment as an example here, we will check the steps to follow.

1. First, we will create a directory ‘ComposeSamples’ and inside it, we will create a file ‘docker-compose.yaml’.

$ mkdir ComposeSamples
$ cd ComposeSamples
Contents of docker-compose.yaml:
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.
Hence we can say that the volume called cms-content mounted at /var/lib/ghost/content.

[Need assistance with Docker Container configuration? We are happy to help you!]


2. 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.
We will give the below syntax if we do not wish to give a name:

version: "3.0"
services:
web:
image: ghost:latest
ports:
- "2368:2368"
volumes:
- /var/lib/ghost/content

For making it  more verbose, we will mention the Docker Volume as a top-level definition given below:

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:

3. Bind Mounts
Bind mounts are parts of the host file system which can be mounted directly inside the Docker container.
For adding a bind mount we will simply mention the host directory we wish to share and the mount point inside the Docker container will get mounted.
volumes:

- /home/<USER>/projects/ghost: /var/lib/ghost/content

We can use any path instead of /home/<USER>/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

[Need urgent assistance in managing Docker Containers in Ubuntu? We are available to help you. ]


Conclusion

This article will guide you on steps to use #Docker #Compose for manging Docker #Volumes.
When you execute a docker-compose command, the volumes #directive in docker-compose. yml file mounts source directories or volumes from your computer at target paths inside the container. If a matching target path exists already as part of the container image, it will be overwritten by the mounted path.
A #Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession. This page describes the commands you can use in a Dockerfile .
List of Docker #Commands:
i. docker run – Runs a command in a new #container.
ii. docker start – Starts one or more stopped containers.
iii. docker stop – Stops one or more running containers.
iv. docker build – Builds an image form a Docker file.
v. docker pull – Pulls an #image or a repository from a registry.