Docker Volumnes

Give ability to store data permanent. Because container store data Temporary. or Docker volume is ability to give backup of data to host machine.

-v is cmd for docker volume.

docker system prune -y
// this will delete which container is stop or taking unwanted space.
 
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest
// this will launch mysql container with this cmd.

docker exec -it <cont_id> bash
// this will enter into mysql container and execute bash shell.

 mysql -u root -p
// enter into mysql program in cli mode.

show databases;
// to see all databases.

create database kyc_devops;
//creating new databases kyc  devops.

use kyc_devops;
//use that databases.

CREATE TABLE messages( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT);
//Created table message with auto increment.

Screenshot 2024-11-13 184954.png

 insert into messages (message) values ("Kyc Submitted");
//by this cmd u will inserting Kyc Submitted msg into message column and in messages database.

 select * from messages;
// by this you will retrive all data from messages database.

exit
//this will exit from mysql.

exit
//this will exit from mysql container.

docker ps // container is running?
docker stop <cont_id> // stop mysql container.
docker rm <cont_id> //remove mysql container.
//by this three cmd i remove the container and delete it
docker ps //now what i see that container is not running

docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest
//so what i did that i launch container again with same cmd.

docker exec -it <cont_id> bash
//enter again into container.

mysql -u root -p
//login int mysql database.
 
show databases;
//what we see that our database kyc_devops is not there and so our database is gone.

exit //exit from mysql 
exit //exit from container
docker ps // container is running?
docker stop <cont_id> // stop mysql container.
docker rm <cont_id> //remove mysql container.
//by this three cmd i remove the container and delete it

So For this is there is solution called docker volumne.

mkdir mysql-volume // create a folder in host machine for backup.
 
cd mysql-volume // go inside that folder
pwd //see path & copy that for mapping with container
/home/ubuntu/mysql-volume

 docker run -d -v /home/ubuntu/mysql-volume:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest
//by this cmd -v is use to map host folder with the mysql folder where it store data eg.(/var/lib/mysql)
 
 //interview question--> what is flag that use to create volumne in docker container?
 
 docker exec -it <cont_id> bash
// this will enter into mysql container and execute bash shell.

 mysql -u root -p
// enter into mysql program in cli mode.

show databases;
// to see all databases.

create database kyc_devops;
//creating new databases kyc  devops.

use kyc_devops;
//use that databases.

CREATE TABLE messages( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT);
//Created table message with auto increment.

insert into messages (message) values ("Kyc Submitted");
//by this cmd u will inserting Kyc Submitted msg into message column and in messages database.

 select * from messages;
// by this you will retrive all data from messages database.

exit
//this will exit from mysql.

exit
//this will exit from mysql container.

cd mysql-volume/ //got inside this folder to check your data is map or not.
ls // you will see some files.

docker ps // container is running?
docker stop <cont_id> // stop mysql container.
docker rm <cont_id> //remove mysql container.
//by this three cmd i remove the container and delete it

//now we have deleted container lets create new container with -v again and see what happen.

docker run -d -v /home/ubuntu/mysql-volume:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest
//creating new container again with -v to same folder path /home/ubuntu/mysql-volume.

 docker exec -it <cont_id> bash
// this will enter into mysql container and execute bash shell.

show databases;
// to see all databases.

use kyc_devops;
//use that databases.

show tables;
//to see all tables which were created in past.

 select * from messages;
// by this you will retrive all data from messages database.
// here u will see your data so this is how docker volumne is use for container for persistence the data.

docker volume ls
//see all volume in docker

docker volume inspect <id>
//detail about volume

docker volume create mysql-data
//creating volume name mysql-data
//mysql data is logical entity 

//there is two volume first is unamed volume where u give path eg(-v) and 
second is named volume where u did not give the path just give name eg.mysql-data.

docker volume ls
//to see volume mysql-data volumne is created or not 

docker volume inspect mysql-data
//to see path where mysql-data is mouted. you will see Mountpoint key and value will be your path.

we can do like this also 
docker volume create nginx-volume

docker run -d -v nginx-volume:/var -p 80:80 nginx:latest
//running container with logical volume which is nginx-volume. 

docker volume ls
//see nginx-volume is there or not.

docker inspect nginx-volume
//to check mouted path folder to it.

sudo su
//switch to user

cd /var/lib/docker/volumes/nginx-volume/_data
//copy and paste that path

ls
//to check data is presented or not.

docker volume prune // to remove the uneccessary volume

Docker Networking

now lets start with docker networking

there is 7 types of network in docker

  1. bridge —> two way between container and this is default all container have their own bridge
  2. user defined bridge —>  provide more control and flexibility than the default bridge network
  3. host — bydefault map with hostname
  4. overlay —> Overlay networks connect multiple Docker daemons together.
  5. macvlan —>Assign a MAC address to a container.
  6. ipvlan —>IPvlan networks provide full control over both IPv4 and IPv6 addressing.
  7. none —>Completely isolate a container from the host and other containers.

clone the repository.