
Lesson Description
The "Postgres Database Docker Container" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey prepares the data layer of the application. The application will connect to a Postgres database running in a Docker container. A docker-compose.yml file is created and the Docker container is started.
Transcript from the "Postgres Database Docker Container" Lesson
[00:00:00]
>> Melkey: So the next part we're gonna do is starting to poke at our database. I said that typically I like starting from the lowest level and build my way up. We started with the API layer, now we're gonna start working in that store file, that database layer, a clear problem is we need a database.
[00:00:17]
So we're gonna create our database here. The First thing I want you to do, if you don't do this, you might have a pretty unfortunate situation if you do a git push, not the end of the world, but I want you to touch dot git ignore.
>> Melkey: Now and git ignore we're gonna do slash database and I'm gonna also just database like, so just to be safe.
[00:00:47]
And this is basically going to ignore any database values that we are gonna create in our local environment here, and so they don't get pushed up to get home, that one be good. All right, now the next fog gonna create is the doctor compose file, I'm always excited for this.
[00:01:10]
At the root of your project, let's go ahead and add a new file Docker dash compose dot yml.
>> Melkey: Now, if this is your first time using Docker, like I said earlier, the best explanation I have, it's a black box that runs software that doesn't actually exist on your computer.
[00:01:32]
What your computer is aware of is Docker and the software that lives within Docker is containerized within Docker. We're gonna do a few things that kind of bridges the black box of Docker onto a local machine, which is persisting and having volumes, all right? But we're going to use Docker to have our database.
[00:01:54]
And I've used this, I don't want to say trick, because it's not a trick, but I've used this approach for local development for many, many different years. It's a quick, cheap way to spin up a Postgres database on your computer, and it's 14 lines of code, all right, so, and that's not including spaces, spaces, it's more.
[00:02:17]
But here we'll start in a dogged compose just it's say the version I'm using, version 3.8 okay? That's probably an outdated version, but it's not broken, don't fix it. Here we'll specify the services, and we will name the service DB for database, okay? The tabs do matter a little bit.
[00:02:44]
>> Melkey: Okay, so the first thing we'll put is the container name, and we call this just workout Db.
>> Melkey: Okay, and next the most important argument is the image. Now, we have a black box, how does it know what software to run? That software is identified by the image, okay?
[00:03:07]
The image tells Docker, hey, pull this software, the snapshot of the software into your Docker container and you're going to run it. So here, I'm gonna use a fairly older It's not that old, but it's old. It's not the cutting edge version of Postgres. You can, it's not going to be a problem, again, Postgres is extremely reliable, it's extremely backwards compatible.
[00:03:29]
So do what you have to do, I'm using version 12.4 and to specify the image, we just type in the name of the software, Postgres, okay? 12, dash or 12.4 slash alpine.
>> Melkey: So this is telling exactly what snapshot version of Postgres we want our Docker container to use.
[00:04:00]
Next up, we're gonna define the volume. Now, the volume here has a particular rule. If we don't specify a volume, and it doesn't persist, every time we run up and down our database, we wipe everything, which is not the best experience, right? If you wanna store some users or Developing.
[00:04:21]
It's gonna mount a volume. This volume is going to be mounted in our database location, and this is we're specifying this, doesn't necessarily exist right now, but we're telling Docker this where you can mount the volume to. Postgres dash data, colon slash, after the colon is where it's gonna exist In the Docker container.
[00:04:42]
So that exists in /var/lib/postgresql/data or to give it read/write abilities. Next, we're gonna specify the ports. So this is telling Docker, you can use this part of my actual machine to port it on your actual Docker container or the Docker instance. We're to use 5432 and bind it to 5432, which is the default that Postgres uses, okay?
[00:05:16]
And here we're gonna supply with some environment variables. These environment variables are going be Important, because gonna tell us how to basically log into a Postgres database from not only our application, but using psql as well. So here we do Postgres underscore Db. When it says Postgres, okay?
[00:05:37]
I'm gonna say Postgres underscore user, it's gonna be Postgres as well. Postgres underscore password, postgres, okay? And then one last command is restart unless stopped.
>> Speaker 2: The restart should be one tab in, right? Or is that one out.
>> Melkey: That one's out. The resource is not in the environment.
[00:06:13]
>> Speaker 2: Okay, and then, sorry, the port, I always forget this with Docker, is the first one the Docker port?
>> Melkey: No, first one's our port.
>> Speaker 2: Port, okay.
>> Melkey: Yep.
>> Melkey: And the same applies to volumes as well, right? The volume is on the left side of the colon, it's what we're giving Docker and in the right side is what Docker is going to be binding it from, it's on container.
[00:06:40]
>> Melkey: Okay, cool, so now you have that Rin, we can take a quick moment to see if it works. So here, if you have Docker installed and Docker Compose installed, you should be able to write, Docker Compose up-build. So docker space compose space up dash dash build. So if I run this you.
[00:07:02]
You can see here, we now have a working Postgres database running on our Docker container.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops