Tip:
Highlight text to annotate it
X
Hey everybody, Mark from Unbounded here. Today we're going to show you how easy
it is to run Postgres in a Docker container on your local system. This is
great for doing simple testing on your laptop or wherever you might be. And then,
in the next video I'll show you how easy it is to automate that so that you can
run automated tests on your local system or in a CI/CD pipeline.
So let's get started. And don't forget -- click the like or
subscribe button if you find this video helpful.
Alright, I'm on my Mac laptop here
and I've got a Docker Compose YAML file that defines how we want to run the
Postgres container. And all the files I'm using here are available in a GitHub
repo that's in the description for this video on YouTube.
So the specific container image I'm running is the official Postgres image version 10.3,
based on the Alpine distribution. I picked Alpine because that makes for a
smaller sized container. And the container is going to listen on port 5432,
which is the default for Postgres. And with the network mode set to bridge,
I'm setting up this container to allow connections from the host computer, which
is what we want for manual testing purposes. I'm also setting the name of
the container explicitly to "postgres". Without that, Docker Compose will make a
unique name for the container, but I want to know that name beforehand so I can
use it later. You'll see where we use that in just a second. Then the Postgres
container allows configuration through environment variables, so I've set the
most common ones here. Obviously you'll want a much more secure password for
most uses. And then last, I'm going to have the container mount a local
directory on my laptop that has a SQL file in it that we'll use to initialize
the database when it first comes up.
Alright, and then this is the SQL file that
the database will run when it starts.
This one just creates a database
called "sampledb". It's a placeholder to show you how to put sample data in your database.
Alright now I'll run this YAML file with docker-compose up.
And the database has already started, read in our SQL file and is ready to connect to.
So now in order to check out the database and see whether or not it's
running, I'm going to run another container,
this one with the Postgres management UI called pgadmin4.
Alright this YAML file is pretty similar. Here's the image I want to run. I'm going to have it
listen on port 8080 instead of the default port 80. And again I'll use
bridge mode so we can access it from our host system. And important here is that
I'm telling Docker I want this container to have access to connect to another
container called "postgres". So that's where I used the name of the container
from the previous YAML file.
Alright now I'll switch to my web browser and connect to localhost port
8080 which is the pgadmin port I specified in the YAML file.
I log in with the credentials I specified as well.
and now I'm going to add the Postgres server so I'll call it "postgres". The host
is called "postgres" as well. And the user and password are just as I put into the
Postgres container YAML file.
And now let's take a look at the new server we just added. pgadmin is
connected to the local Postgres container and we can see that the sampledb is here.
Don't miss out on that next video! Click like and subscribe.