Docker Volumes Explained (PostgreSQL example)

Hussein Nasser · Intermediate ·🏗️ Systems Design & Architecture ·6y ago

Key Takeaways

The video demonstrates how to use Docker volumes with PostgreSQL instances, including creating a container, persisting data, and backing up volumes. It covers various use cases such as data replication, backup, and restore.

Full Transcript

hey guys what's up I'm bringing you a quick video to discuss an interesting topic called a darker volumes so if you have been a subscriber of this channel you might have seen me use docker a lot to spin up databases message queues and other interesting web servers right like proxies I use docker all the time and if you have noticed that every time we use docker we ended up with a ephemeral kind of data where the moment we destroy and remove the container our data is gone okay so let's explain why is that and how we can use docker volumes to persist the data of the container in a place that we can see copy back up and we can essentially make a backup of and spin up another container and reuse that data on another container so how about we do that okay so here I'm gonna use Postgres as an example and I have spin up PG admin here and I don't have any Postgres instances as of now so I am going to go ahead and spin up and it Postgres instance let's go ahead and do that using docker okay and obviously to know that you have darker just do docker run hello word make sure that you get this hello from docker and if you do that that means you're ready to rock and roll okay and alright so I'm going to spin up a docker container with a post Christ instance in it and to do that it is extremely simple you can do docker run and it's always a good idea to give your container name so I'm gonna give it a name called PG and we're gonna give it a port because Postgres is a client-server application so it's listening on some port and I want to expose the port 5 4 32 she's earning on the container here - five four three two which is running on my host right so I'm listening on 5 4 32 on my host ok assuming this is not in you and then finally the final thing is you specify the image from which you want to pull the post Chris I think this is Bosco's 12 forces the latest and if you run obviously what will happen here is and you specify the I didn't specify any data or anything so by default when you do that docker will pick a random directory on your host machine and use it as a volume and will store the data because Postgres will need data guys right it's a folder with all your databases and indexes post stored procedures and all that jazz right so now I can if you go to PG admin I'm gonna connect server connection let's go this Hossein mac and using the default thing post case post case that's the default password right and we're gonna call this PG right and if I connect I am obviously gonna establish the connection if I can spell my name correctly that's the hostname that's my machine name here and the port is for 450 that's the default port and that database is always you will get the data base for free here which is called post Chris and this is that no tables so go ahead if I create a table right now and called it I don't know employees right and there's the thing it's an employee and the employee table and I can start doing stuff and this I create tables like a data and that's it however if I go back to my container and stop it right if I stop my container and try to connect what will happen disconnect server and that's just go ahead and try to connect the server again right I'm gonna get an error right because the server is no anger no longer running that's not a problem because my containers store its pose is stopped it's not deleted right so I can do is do docker start PG that will start my container up again so I came connect again and you can see that I still have my table here so if I stop my container my container crash the data doesn't go away because the containers still there correct and it's referencing some folder that I have no idea about okay that's the problem so if I do darker stuff PG and I then do and I then do darker or n PG which means remove the container and then I do the same command to run the container again this will be a brand new container that means it will be a brand new folder with the data that it dumps in that means I just lost all my data let's confirm that so I'll spin out the database instance again go back and if I do write to click disconnect right and then write it click connect again put the password all right if I go back there are no tables because employees is done there's no data this is a brand new instance for the first time it's running how do I start up a container with Postgres have a bunch of data and and start adding a lot of data may be pouring million rows how do we do that it is extremely sample right so I'm gonna go ahead and do docker stop PG docker or MGP okay all right and here's the thing before we start the container here I want to specify a folder and in this case this is my folder a C user Huseyin data and I'm gonna create a folder here called PG it's blank right but here's a trick what I want to do is I do docker run if I can't find the command docker run name PG port and here's the thing before we end that I want to do - v1 is the volume and the volume on my host is for users the same Nasr data PG map pink - the volume or the data path of Postgres in the running container which is almost fixed all the time so it is var lab post grist sequel and then slash data so that path is always gonna be the same path on the container because it's the container is built up from I'm an image unless the image changes that path then we don't have to worry about it right that's why it's always a good idea to fix an image right you can always look like spaces here and then that maps to this folder so guess what now when I spin up that container it's gone I'm gonna start dumping all its data into my path that I specifically asked it to dump its data in that means I have full control on this folder I can back it up obviously or not while the Postgres is running right you can stop the container back up gzip it right and then maybe just destroy the container or run your test against this dead a post-christian instance and then once you're done extract a brand new folder and then point it again let's let's do this let's show you how to do that okay so what I'm gonna do here essentially as I'm gonna go ahead and reconnect obviously this is gonna be an empty right this is gonna be a brand empty database and I'm gonna go ahead and create another table here some table I don't know URLs okay so we were building a your shoulder for example and then let's build another table you you start doing your data you stop doing your stuff right just doing your stuff and here's the beautiful part if I go to finder the path itself and going to that path look at that peih-gee this is the data that it just was populated with data that I have didn't didn't do anything with right I just pointed that docker container to this folder and it's just a filling stuff and here's the critical thing now I have some data here I have some tables so what I would what I can do is I can just decide to stop container yeah I can make a backup of this guy right I can do tar - how do you do that SC z compress will this gzip and the file as let's call it PG tar.gz and I want to compress this folder okay and just like that I have now a compressed folder of this thing so so as you can see now I have a backup of my data and I can I can just decide to copy the entire directory into a completely different directory I can take this itself I'll take it to an entire other machine extract it there and spin up the container with that data so that's how about we do that right so let's just copy this folder PG as our means everything including the content from PG to Anna for this folder - PG brand-new okay and just like that I have PG brand-new right with a bunch of data here so now if I spin up a completely different container in a completely different port let's do that how about docker run give it a name PG brand-new right just the name and then the port will be just for fun I'm gonna use one two three four okay and let's just do this way and then mapping to port five four three two and then the volume is what there's the same Nasr data not PG just to be five ANSI right I want the data to be PG brand-new just cause right and then this maps to var let Postgres sequel and then data finally I won't spin our post quest database from the image right let's check this out and this will spin up the Postgres instance on a brand new pour remember this port now is one two three four my container technically is stopped right or maybe you can go ahead and remove it and that's even fine if I go back to my Edmund I have to establish a new connection this is no longer working obviously but if I stab leshan new server that's called this PG brand-new I should expect my employees and URL stable so the port is one two three four right and then same thing Postgres and Postgres and then save and then just like that go there and look at that my beautiful two tables just because I have the folder and I have zipped it you can take that zip file you can take that tour in two completely different machine complete different hosts with the with the darker there and essentially extract the host there and you can automate that process right and extract the folder there and spin up another container point the volume to that folder and I said just like that you have the data on another machine right but that's it essentially it's all really replication is just copying the data so the use case of this as you want to essentially build the test data like I have a huge set of test data and I want to run my tests against it but the the Mondial on my test the data is will be corrupted that that's it the data will be ruined so what do I do I want to make a backup of the database well you can do a PG clone which is very complicated but it is where is the use docker and have a zip file right extract a spinnaker container have your application connected the container right the application that runs the test connect the container run your test and once you're done destroy the container destroy the volume because you don't need it you have a backup right and when you want to run another test again on your newer version of the application extract it again spin up a brand new container and that said you always have a fresh copy of your data and that's just one use case of where you can use volumes volumes or powerful stuff right and yeah it's visually with kubernetes the volumes is always being used behind the scenes always especially because containers are stateful by design so if they are they are continuously being destroyed from one part and spin up and another apologist could be completely different host right so yeah that was like a quick video guys showing you how to use docker volumes on docker I hope you enjoy that see you in the next one you guys stay awesome

Original Description

In this short video I discuss docker volumes and demonstrate how to use volumes with postgres instances. We will create a postgres docker container, create some tables, backup our volume, destroy the container and spin up a brand new container with our existing volume. #POSTGRES docker run -p 5432:5432 --name pg -e POSTGRES_PASSWORD=postgres postgres #PGADMIN docker run -e PGADMIN_DEFAULT_EMAIL="hussein” -e PGADMIN_DEFAULT_PASSWORD=“password” -p 5555:80 --name pgadmin dpage/pgadmin4 Enjoy 🏭 Software Architecture Videos https://www.youtube.com/playlist?list=PLQnljOFTspQXNP6mQchJVP3S-3oKGEuw9 💾 Database Engineering Videos https://www.youtube.com/playlist?list=PLQnljOFTspQXjD0HOzN7P2tgzu7scWpl2 🛰 Network Engineering Videos https://www.youtube.com/playlist?list=PLQnljOFTspQUBSgBXilKhRMJ1ACqr7pTr 🏰 Load Balancing and Proxies Videos https://www.youtube.com/playlist?list=PLQnljOFTspQVMeBmWI2AhxULWEeo7AaMC 🐘 Postgres Videos https://www.youtube.com/playlist?list=PLQnljOFTspQWGrOqslniFlRcwxyY94cjj 🚢Docker https://www.youtube.com/playlist?list=PLQnljOFTspQWsD-rakNw1C20c1JI8UR1r 🧮 Programming Pattern Videos https://www.youtube.com/playlist?list=PLQnljOFTspQV1emqxKbcP5esAf4zpqWpe 🛡 Web Security Videos https://www.youtube.com/playlist?list=PLQnljOFTspQU3YDMRSMvzflh_qXoz9zfv 🦠 HTTP Videos https://www.youtube.com/playlist?list=PLQnljOFTspQU6zO0drAYHFtkkyfNJw1IO 🐍 Python Videos https://www.youtube.com/playlist?list=PLQnljOFTspQU_M83ARz8mDdr4LThzkBKX 🔆 Javascript Videos https://www.youtube.com/playlist?list=PLQnljOFTspQWab0g3W6ZaDM6_Buh20EWM 👾Discord Server https://discord.gg/CsFbFce Support me on PayPal https://bit.ly/33ENps4 Become a Patreon https://www.patreon.com/join/hnasr? Stay Awesome, Hussein
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Hussein Nasser · Hussein Nasser · 0 of 60

← Previous Next →
1 Extending ArcObjects (IGeometry) - 01 - Getting Started
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
2 Extending ArcObjects  (IGeometry) - 02 - The Document, The Map and The Layers
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
3 Channel Update - New Book, New Job, New Videos
Channel Update - New Book, New Job, New Videos
Hussein Nasser
4 Learn Programming with VB.NET - 01 - Getting Started
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
5 Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
6 Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
7 Learn Programming with VB.NET - 04 - User Interface
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
8 Learn Programming with VB.NET - 05 - By Value v. By Reference
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
9 Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
10 Learn Programming with VB.NET - 07 - Conditional Statements
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
11 Learn Programming with VB.NET - 08 - Inheritance
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
12 Learn Programming with VB.NET - 09 - Strategy Design Pattern
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
13 Learn Programming with VB.NET - 10 -  How did I learn programming
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
14 IGeometry 2016 Retrospective - Channel Update
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
15 Javascript by Example - The Vook
Javascript by Example - The Vook
Hussein Nasser
16 Vlog - Keep your servers close and your database closer
Vlog - Keep your servers close and your database closer
Hussein Nasser
17 Vlog - Client/Server Programming Languages
Vlog - Client/Server Programming Languages
Hussein Nasser
18 Javascript By Example L1E01 - Getting Started
Javascript By Example L1E01 - Getting Started
Hussein Nasser
19 Persistent Connections (Pros and Cons)
Persistent Connections (Pros and Cons)
Hussein Nasser
20 Javascript By Example L1E02 - Building the Calculator Interface
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
21 Happy new Year from IGeometry!
Happy new Year from IGeometry!
Hussein Nasser
22 Synchronous v. Asynchronous
Synchronous v. Asynchronous
Hussein Nasser
23 Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
24 Show Your Work. Blog, Vlog, Write, Create and Develop!
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
25 Relational Database Atomicity Explained By Example
Relational Database Atomicity Explained By Example
Hussein Nasser
26 Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
27 What Comes First, User Experience or Software Architecture?
What Comes First, User Experience or Software Architecture?
Hussein Nasser
28 Javascript By Example L1E05 -  Evaluate the Calculator Expressions with eval
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
29 Fastest Way to Learn Programming Language or Technology
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
30 Javascript By Example L1E06 -  Fix Leading Zero Bug with Conditions
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
31 Stateful vs Stateless Applications (Explained by Example)
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
32 Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
33 Advice for New Software Engineers and Developers
Advice for New Software Engineers and Developers
Hussein Nasser
34 Why JSON is so Popular?
Why JSON is so Popular?
Hussein Nasser
35 Building Scalable Software - SLA, HS, VS
Building Scalable Software - SLA, HS, VS
Hussein Nasser
36 Vlog (Istanbul) - Datacenter Proximity
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
37 Should Software Engineers Learn Bleeding-Edge Technologies?
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
38 Do Developers Build Bad User Interfaces/Experience?
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
39 Learn By Doing.
Learn By Doing.
Hussein Nasser
40 I Wrote Bad Front-End Code That Broke Chrome
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
41 My Story
My Story
Hussein Nasser
42 Vlog - Horizontal vs Vertical Scaling
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
43 Can User Experience Help Build Better Rest API?
Can User Experience Help Build Better Rest API?
Hussein Nasser
44 Reverse engineering Instagram in flight mode
Reverse engineering Instagram in flight mode
Hussein Nasser
45 The Benefits of the 3-Tier Architecture (e.g. REST API)
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
46 Stateless v. Stateful Architecture (Podcast)
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
47 The evolution from virtual machines to containers
The evolution from virtual machines to containers
Hussein Nasser
48 Proxy vs. Reverse Proxy (Explained by Example)
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
49 Canary Deployment (Explained by Example)
Canary Deployment (Explained by Example)
Hussein Nasser
50 No Excuses
No Excuses
Hussein Nasser
51 Synchronous vs Asynchronous Applications (Explained by Example)
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
52 What is an Asynchronous service?
What is an Asynchronous service?
Hussein Nasser
53 Difference between Client Polling vs Server Push in Notifications
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
54 Software vs. Hardware AdBlockers (Explained by Example)
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
55 HTTP Caching with E-Tags -  (Explained by Example)
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
56 Simple Object Access Protocol Pros and Cons (Explained by Example)
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
57 Nodejs Express "Hello, World"
Nodejs Express "Hello, World"
Hussein Nasser
58 Reverse Engineering Instagram feed
Reverse Engineering Instagram feed
Hussein Nasser
59 Popup Modal Dialog with Javascript and HTML
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
60 MIME and Media Type sniffing explained and the type of attacks it leads to
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser

This video teaches how to use Docker volumes to persist data from PostgreSQL containers, ensuring data is retained even after container removal. It demonstrates various use cases, including data replication and backup.

Key Takeaways
  1. Run a Docker container with a PostgreSQL instance
  2. Specify a volume for the container using the Docker run command
  3. Stop and start the container to manage its lifecycle and associated volume
  4. Remove the container and recreate it to test volume persistence
  5. Create a new Docker volume using docker volume create
  6. Mount the Docker volume to a PostgreSQL container using the -v flag
💡 Docker volumes can be used to persist data across container restarts and share data between containers, making it a crucial tool for data management and replication.

Related Reads

📰
Server Components vs Client Components: The Mental Model Shift Every Vite Developer Needs
Understand the difference between server and client components in Vite development to improve your workflow and application performance
Dev.to · Digital dev
📰
Go context.Context Done Right: Cancellation, Timeouts, and Values
Learn to use Go's context.Context correctly for cancellation, timeouts, and values to improve your concurrent programming skills
Medium · Programming
📰
How I Built My Own Terminal in C
Learn how to build a terminal in C by managing memory with raw char pointers and exploring low-level systems
Medium · Programming
📰
Junior vs Senior iOS Interview: What Actually Changes at Each Level
Learn what changes in iOS interviews as you progress from junior to senior levels and why it matters for career growth
Medium · Programming
Up next
Book Summary and Review - The Kubernetes Bible (2nd ed.) by Gineesh & Russ #Kubernetes #K8s #MLOps
Abonia Sojasingarayar
Watch →