From the course: Web Servers and APIs using C++

Deploying our container to Heroku - C++ Tutorial

From the course: Web Servers and APIs using C++

Start my 1-month free trial

Deploying our container to Heroku

- [Instructor] In order to deploy our app to Heroku we need to do a few things. First, we need to prep the app to be ran from a container. We need to containerize it. Second, we need to log in to Heroku from the host machine. Then we'll need to create a Heroku app, we'll need to push our container to Heroku, release the container to production, and finally, test it in a browser. Before we can do any of this, we need to persist our volume to the docker image. Volumes are ephemeral things, and aren't part of the docker image. Since our app is working, we'd like to put the working version into an image. Let's run the latest version of our CPP box image. So we're gonna say, docker run -ti cppbox:latest bash. Now with this container running, we're gonna open up another terminal window or a tab, and I have another tab right here. And in this window, we're gonna make sure that we're at the cppweb directory, here we are, we're there, and we're gonna do a docker ps. This will list all of the running containers. Copy the ID of the one you want. Now your ID is going to be different than mine, so make sure you copy yours, not mine. Type docker cp, cp for copy. We're gonna start from the current directory, which is cppweb. We're going to give it the container ID, which is this, then we do a :/usr/src/cppweb. So we're gonna call it cppweb from within the container as well. We're gonna copy that. Next, we're gonna commit those changes back to the image, so we're gonna take it from the container to the image. And we're gonna say docker commit, give it the same container ID, space, and this time we'll give it a different name. We'll call it hello_crow:latest. Again, remember to use your container's ID, not mine. Now with our changes committed back to the image, let us go ahead and go back to Atom, and from within Atom, I'm gonna right-click on the project directory, New, File, and we're gonna create a docker file here, Dockerfile. And this is gonna be a pretty simple one. I'm gonna say FROM hello_crow, so use the crow that we just committed. We're gonna give it a working directory, WORKDIR. That's gonna be /usr/src/cppweb/hello_crow/build and remember build is where we have the running version of our app. And then finally we're gonna type CMD ["./hello_crow"]. Now what's going on here is that this is just saying, hey, use the build directory as our starting directory when this image is launched, and in that directory, launch the hello_crow app. So we're gonna save that, and we're gonna go back to the terminal, and remember from the terminal we are now gonna go to our hello_crow directory, so cd hello_crow, and within this directory, we're first gonna log in to Heroku, so we're gonna do heroku space login. Give it your credentials. Then your password. And if you enabled it, you'll be asked for your two-factor authentication, so mine is gonna be right there for this moment, and there we go, we're logged in. Next we're gonna log into the container service of Heroku. So we're gonna say, heroku space container:login. Usually this command won't prompt you for anything if you're already logged in. Now we're going to create a Heroku app. We're gonna say, heroku create. And take note of what it's giving us here. It's telling us the name of our app is infinite-falls-24876. Here is its URL on the web. And if you want to do pushes to it via Git, here is its Git URI. Now once we've done that, now we're gonna go ahead and build it, so we're gonna say, docker build with a dash T, so we're gonna tag this hello_crow and period. And that just means the current directory. Now that we've updated the image, we're gonna say, heroku container:push. This is a web app, we're gonna use a dash A to give it our app's name, and we'll just copy it right here, the infinite-falls-24876 with a space, copy that, and hit Enter. Now this might take a while so it might be a good time to take a short break as it uploads all the files to Heroku for the first time. Now we need to release our container, so the first command just pushed it up to Heroku, now we need to release it. So we're gonna say, heroku container:release, we need to say web, dash A, then the name, hit Enter. And then finally, we need to go heroku open dash A, and the name of our app. It might take it a little bit to do a cold start. And there we have it. We can see hello, troy, and that is our app now running on the web in Heroku, good job.

Contents