Current section: Persisting Data and Automatic Deployment 5 exercises

Persist Application Data Between Deployments

As of right now, incrementing and decrementing the counter works.

But when we make a change to add a console.log and redeploy, the counter is back to zero.

The count resets because we have not persisted the volume. Every time we deploy, the hard drive for our app is basically brand new.

In the

Loading lesson


Instructor: 0:00 You may have noticed a problem with our app as you've been deploying things and whatever. We're on this page, I can increment, I can decrement, everything's awesome. We're at five.

0:09 If I go here and I say I want to add a console.log the current count, currentCount. Great. Then we do fly deploy of that, then it actually deploys pretty quick because we've got the builder all set up. Lots of our layers in our Dockerfile are already cached, but it does need to push a couple of changes.

0:32 When that finally does deploy, and we can look at our logs right here to see, it shuts down the old one, starts up the new one, in just a moment as that gets deployed. Here it is starting an instance, configuring a virtual machine, doing all of that. Now it's running node start. Now it's ready to accept traffic and it's at zero. What's going on here?

0:58 The problem is that we have not persisted the volume. We've got our database sitting on a hard drive and every time we deploy, that hard drive is inside of our Docker container. We're deploying a new hard drive basically every time and it's creating a brand new SQLite database.

1:17 You'll actually notice that here it says the following migrations have been applied. It shouldn't have to apply any migrations because last time we deployed, it applied migrations. That's what's going on. Is we are not persisting the volume. This is actually pretty straightforward and simple to do with Fly. Fly has built in support for persisted volumes that are encrypted.

1:38 We say fly vol create data, size 1, 1 gigabyte, and this is going to allow us to choose what region we want the volume in. We're going to want to select the region where our app lives, so we'll do sjc.

1:53 With that, it's going to take a moment, create the volume for us, and now the only change that we actually need to make is to go into our fly.toml and specify that we have a volume that we want to mount. We'll do that in our mounts, where we'll say source is data, that's what we called our volume right here, create data.

2:17 Where we want to put it is up to us but we're going to say destination is /data. That's where I like to put mine. The other thing we have to change here is our Dockerfile, we need to specify where our SQLite database lives.

2:33 We started out specifying that it lives under /app which is within our app directory which is new every time. We want it to just be in simply /data because that is where the mounted volume will be and that's the persisted volume.

2:47 Our SQLite database is now going to stay inside of this special hard drive that is going to live with our application between deploys. With those changes now, we can say fly deploy, and that's going to get our new configuration deployed both with our Docker configuration as well as our fly.toml.

3:07 This should persist our database between deploys, and so we'll test that out. With that successfully deployed let's come over here, refresh the app. We are at zero again because this is a new SQLite database, but now I increment it four times and I'm going to get rid of this log or change it to say current count is, and fly deploy.

3:32 We'll deploy it again and make sure that when we come back it is still four, just to make sure that this persisted volume actually took.

3:42 With that successfully deployed, let's refresh, and boom, it's still at four. That totally worked. All that we had to do was say fly vol create, and we specified what we wanted to call it, it was data, and the size we specified 1.

4:00 Now we've got a persistent volume that we can mount inside of the mounts config of our fly.toml, and then reference inside of our Dockerfile for that database URL so that Prisma will create the SQLite database and then reference it inside of our persisted volume.