Steps To Mount Volume In Docker Compose File
Step 1: Create a docker volume.
docker volume create --name=mysql-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
Step 3 : Run the docker container .
docker compose up -d
Step 4 : Enter into docker container .
docker exec -it <container-id> mysql -u root -p
Step 5 : Create a new database .
CREATE DATABASE `GFG-Docker`;
Step 6 : Now remove the docker container that is running mysql .
docker compose down
Step 7 : Again run a new docker container .
docker compose up -d
Step 8 : Now enter into new docker container and see the databases .
show databases;
You will see the database persists .
How do you Mount Volume in Docker Compose File ?
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.