By default, the data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
The docker commands we have used in the lab for volume mapping are,
docker run --name postgresdb -v NAMEOFYOURVOLUME:/var/lib/postgresql/data -e POSTGRES_PASSWORD=YOURPASSWORD -d postgres
docker exec -it postgresdb sh
createdb -U postgres YOURDBNAME //To Create a new DB
psql -U postgres YOURDBNAME //To Connect to the DB
CREATE TABLE Employee (EmpId int, Name varchar(50));
\dt //To list all tablesd
INSERT INTO Employee (EmpId, Name, Location) VALUES (1, 'Vijay', 'Netherlands');
SELECT * FROM Employee
\q // To exit from DB
docker rm -f postgresdb //To stop and remove the container forcefully
docker volume ls //To see all volumes
docker run --name postgresdbnew -v NAMEOFYOURNEWVOLUME:/var/lib/postgresql/data -e POSTGRES_PASSWORD=YOURPASSWORD -d postgres
docker exec -it postgresdbnew sh
psql -U postgres YOURDBNAME
SELECT * FROM Employee
docker rm -f postgresdbnew //To stop and remove the container forcefully
docker volume rm postgres-database //To Remove the volume
Watch video Docker Volume Mounting (Hands-on) online without registration, duration hours minute second in high quality. This video was added by user CodeWithVijay 08 August 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 317 once and liked it 3 people.