Add a SQLite Console Shortcut with the Dockerfile
With a long-running node server and a database, sometimes it's useful to ssh into the virtual machine to explore the file system, and look at the database.
You can do that with Fly!
Connecting to the Running Virtual Machine
To get a console into the running virtual machine, use the `fly ssh c
Instructor: 0:00 With a long-running node server and a database, sometimes it could be really nice to be able to dive into that virtual machine, SSH into it, explore the file system, and look at the database. You can do that with Fly using the Fly SSH command. It will say fly SSH console to get a console into our running virtual machine.
0:23 Now that we're connected, we can ls -al and see here are all of the directories that we've got. Now, because of our dockerfile, we know that our app is running inside of app, and we also created the mounts config in our fly.toml so we know our database is inside of data. We could ls -al everything in app, and there's our application. List everything in data and there's our sqlite like database.
0:47 Things are looking great here. If we now run bash, now we have access to bash and all the cool things that bash can do because SSH is not quite enough. We also can run sqlite3 because we installed that as part of our dockerfile as well. We do an app to get to install sqlite3 and we can interact directly with our database, which is in data/sqlite.db
1:09 If we do tables, we can see here are all of the tables. We can select * from count, and there's our count just sitting in our database. We could do whatever database queries we want to directly in here.
1:22 It's really nice to be able to do that, but it's a little bit of work that I don't want to have to do every time I want to look into my database to have to first run the fly SSH console command and then do sqlite.db. I might forget where that is and all of that. We're actually going to do a little handy thing inside of our dockerfile to make this a bit easier.
1:46 In our dockerfile, right below where we set up these environment variables, I'm going to paste in this little command that basically creates a new file inside of usr/local/bin/database-cli. That file is going to contain a simple script that uses set -xe that'll make sure to print out what is being run and also make sure it has the proper exit code based on the command that's being run here.
2:13 Then it'll run sqlite3 across the database URL. That is the contents of the file that goes into usr/local/bin/database-cli. Then we make sure that is executable. With that now, we simply deploy this. Then we can run fly SSH console, give it the uppercase C flag for the first command that is executed.
2:35 Then we run it right into our database-cli. Let's get that deployed with fly deploy. With that deployed now, we can run fly SSH, console -C database-cli, and boom, here I am in the sqlite. I can do tables and select * from count and whatever other SQL commands that I want to execute against my production database.
3:01 Of course, use that with discretion, but it is really nice to be able to jump straight into that using this really simple echo command to create a new file right in our dockerfile. You could do this with any other sorts of commands that you want to be able to easily execute within your Docker VM running on fly. That's how you make sqlite -cli accessible via the fly SSH consol