Open In App

How do you Mount Volume in Docker Compose File ?

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is a containerization tool that encapsulates the application along with its dependencies and runtime into compact units called docker containers. On the other hand, Docker Compose is a service through which multiple Docker container applications can be managed easily. In this guide I will first what is Docker. Then I will discuss what is Docker Compose. After this, I will walk you through the different steps to mount a docker volume in a Docker Compose file.

What is Docker?

Docker is a containerization tool that helps in packaging the application and its dependency into compact units called docker containers. These docker containers contain all the application code, runtime, libraries, and other dependencies for running an application. Here developers first write a Dockerfile by mentioning the base image, working directory, commands to download dependencies, and commands to run the application. Then the Dockerfile is built using the `docker build ` command to create a docker image. Using this docker image developers can run their application on any system, the only condition is that docker should be installed on that system. The docker containers use very less resources of a system. Due to this reason, multiple docker containers can run on a single machine, which helps in maximization of resource usage of a system and also helps in reducing the overall infrastructure cost to deploy and run an application. In summary, we can say docker has become a very important tool for organizations to deploy and run an application on any machine without facing any hardware-related issues.

What is Docker Compose?

Docker Compose is an essential tool for managing multiple docker container applications. Docker Compose files are written by using a simple human-readable programming language that is YAML. Here in the YAML files all the configurations, services, docker volumes, dependencies, and other environment variables are mentioned so that the entire application can run smoothly. A single command `docker compose up can start building the docker images and run the application in the port specified. In this way, it saves time and effort in the development process, which leads to increased productivity and efficiency. As building and running the applications has become more easy, developers can focus more on improving the application logic. In summary, we can say Docker Compose is a vital service provided by Docker, which simplifies the complex application running process and accelerates the development cycle and deployment process.

Pre-requisites

Before moving on to the next section, make sure that you have installed Docker on your system. If Docker is not installed, then follow these detailed geeks for geeks articles to download Docker on your system.

Steps To Mount Volume In Docker Compose File

Step 1: Create a docker volume.

docker volume create --name=mysql-volume

create-docker-volume

Step 2 : Create a docker compose file by mentioning the docker volume .

version: '3'
services:
mysql:
image: mysql:5.7
volumes:
- mysql-volume:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
mysql-volume:
external: true

create-docker-compose-file-

Step 3 : Run the docker container .

docker compose up -d

run-docker-container-

Step 4 : Enter into docker container .

docker exec -it <container-id> mysql -u root -p

enter-into-docker-container

Step 5 : Create a new database .

CREATE DATABASE `GFG-Docker`;

CREATE-DATABASE

Step 6 : Now remove the docker container that is running mysql .

docker compose down

remove-the-docker-container-

Step 7 : Again run a new docker container .

docker compose up -d

again-run-the-docker-container-

Step 8 : Now enter into new docker container and see the databases .

show databases;

again-connect-to-see-persist-database

You will see the database persists .

Conclusion

Here in this article you have first learned what is Docker . Then you have learned about what is Docker Compose and how it manages multiple docker container only by using a single YAML file . After this you have created a docker volume and then you mounted this docker volume in the Docker container by mentioning it in the Docker Compose file .

How Do You Mount Volume In Docker Compose File ? – FAQ’s

What is docker volume ?

Docker volume are the persistent data storage methods used by docker .

Why docker volume is used ?

Docker volume is used to persist the data generated by multiple containers .

How to specify the docker volume in Docker Compose file ?

Under the `volumes‘ section docker volume is mentioned in the docker compose files .

Can multiple Docker volumes mounted in the Docker Compose files ?

Yes, multiple docker volumes can be mounted in the docker compose files by mentioning each volume under the `volumes` section .

How to see the docker volumes ?

You can use the command `docker volume ls` to see the docker volumes .



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads