How to Create a Docker Postgres Image with Preloaded Data #docker #postgres #coding

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to W code in this video we're going to learn how to create a postra Docker image with pre-loaded data specifically we're going to create an image so that whenever a Docker container is Created from it it will have a table called subscriber created and it will have four rows which are these right here so how do we do this so to initialize a Docker postrace image with tables configuration and data we need to use initialization scripts and these are SQL or script files that are placed inside a specific folder in the docker image so for example to get this all this data into a Docker image we just need to copy this file into a specific folder in the image and the folder that we need to copy this SQL file into is called let me uh paste it right here it's called Docker entrypoint init db. D so any scripts placed inside this folder will be executed after the default postrace user and database are created so so for example let's me create a simple Docker file in here and paste some stuff here so this Docker file right here will create a postc image and then copy this subscriber. SQL file into the docker entry point in it db. D folder with this line here so when we're building this image we just copy this file into this directory note that we also set some necessary environment variables for the Post gra image to work properly which are these here and now we just need to build the docker image with the docker build command so let's build this image and we're just going to call it p-i and now let's create a container from this image using the docker run command so clear out this it's just going to be Docker name we're going to name the container p-c we're going to have it we're going to map the port 9001 on our Local Host machine to 5432 in the container which is where of course postc will be running by default we're going to set up a volume to VAR lib postare SQL ddata just so we can have a volume to make sure the data isn't removed when the image is destroyed but so now let's just run this command and if we check the logs here we can actually see right here running Docker entrypoint subscriber. SQL we can see create table and it's inserted four rows which are these here so this means that our container when we start up a container from this image we will by default have a subscriber table and also some data in it and one thing I want to note though is that these scripts will not be ran if the container is started with a data directory that already contains data so for example we set up a postc volume right here my postc volume to varb postc SQL ddata if we applied a volume that had data inside this directory then our scripts inside Docker entrypoint and at DB would not be ran so if we created another image and we attached this volume then this script would not be ran because the volume shows that there's data inside that directory but so now just to further prove that this worked let me open up a second terminal right here and what we're going to do is we're going to go inside this Docker container and just query it just to make sure everything's there so what I did here is just executed A bash cell and an interactive one against our container and now let's use psql which is a terminal based postra client to connect to this database so we're connecting to my DB as the user wit code and now let's just quick the subscriber table so all we need to do is select all from subscriber and we can see our four rows right here so this is all it takes to create a postrace container or image with pre-populated data if you like this video please consider supporting me by downloading my Chrome extension called wetter but besides that thank you for liking and subscribing and I hope to see you in the next one have a good one
Info
Channel: WittCode
Views: 607
Rating: undefined out of 5
Keywords: WittCode, wittcode, docker postgres preloaded data, docker postgres image, docker postgres container, docker postgres, docker postgres empty
Id: EtWTa27G4wE
Channel Id: undefined
Length: 3min 52sec (232 seconds)
Published: Sat Mar 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.