Starting up our server and running the curl command curl localhost:3000/health
, we find that we get an error. Our server is complaining about us not having a database. Since this is a fresh install, we have no data at all. SQL comes with a file called init.sql
inside of your db
folder that will create a database, add the necessary tables for our application, and prepopulate the table with a single entry. We just have to put it in the folder that MySQL will use to populate the database!
Instructor: [0:00] Our server is now running, so if we open up a new tab and we do a cURL on localhost:3000/help, we can see that there is another error message. Well, I guess we're making progress, as this is a different one this time.
[0:16] This time your server is complaining about a missing database. That makes sense. We are running a fresh install of our database right now, so there is no data in there. Here's a neat trick. A lot of containers will have a folder from which any file present will be executed before starting the server.
[0:36] In this case, any .sql file in the /docker-entrypoint-initdb.d folder will be executed against our MySQL server as soon as the container is started. If there were any .sh file in there, they would also be automatically executed.
[0:55] In the db folder of your application, there is a .sql file. This SQL script will create a database, add the necessary tables for our application, and pre-populate the table with a single entry. You can view the content of the file by running a cat init.sql. In order to put this init.sql file in the folder that MySQL will use to populate the database, we will mount a volume with a -v flag.
[1:29] First, let's sure that our MySQL container is stopped. Let's go back to our server. Then, you will need to tell Docker to use the init.sql file and map it to the Docker entrypoint in the db folder of our container. Using the previous command, you can use the -v flag to mount your current working directory /init.sql into the /docker-entrypoint-initdb.d folder into the init.sql file.
[2:14] Our container is now started and should be pre-populated. Let's go back to our backend server again and try to run our server. Open up a new tab. Let's attempt a cURL on localhost:3000/help. You should now see a server status JSON object that shows the database as being true.
[2:46] If you stopped your frontend previously, you can now start it again. Let's open up a new tab, go into our front, and run npm run serve. Now, point your browser to localhost:8080, and you will see the running application. You should immediately see an entry on the home page for an image with no caption that has the label, "Hello."
[3:14] If you go to the generate meme tab, you'll be able to enter a keyword and see a random image. You can generate a new image from here, or you can add a caption and hit the captionize button. This will send a request to the Node.js server to decompose the GIF and recreate it with the caption you've just specified.
[3:42] Finally, you will have the option to save this to the gallery, which will add an entry to the database. One last thing about containers. They are ephemeral. They are meant to be easily disposed of. Once you stop them, no changes that you did to them will be saved.
[4:05] If you go back to your terminal and restart the MySQL server with the exact same command, this will restart a server with a brand-new database with the initial set of data specified in the init.sql file. Check your server, and make sure that it is still running. Now, go back to localhost:8080 and refresh the page. You will see that any image you saved previously now has disappeared.
[4:31] This is great for testing, since you always start fresh. It is a problem for your production server, where you'll want to persist this data. You can mount a volume that will contain your database in order to save this data, but this will be tackled in a later lesson.
[4:47] Congratulations. You've now started the application. In this lesson, you used Docker to start a MySQL server, and your application is now connecting to it, all of this without actually having to install MySQL. Hopefully, you can start seeing how containers can be useful by now.
got "Mounts denied" error message on macOS