Intro to Backend Development
  • Introduction
  • SP25 Syllabus
  • Apply to Take the Course
  • Getting Started
  • Weekly Feedback Form
  • Ed Discussion
  • Intro to Backend SP25 Google Calendar
  • Cheat Sheets
    • Assignment Requirements
    • Assignment FAQs
    • Error FAQs 😢
    • Concept FAQs
    • Postman
    • Command Line
    • Virtual Environment
  • Chapters
    • 1. Routes
      • Pre-Class TODO's
      • Lecture
      • Assignment Handout
      • API Specification
    • 2. Databases
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 3. Relational Databases
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 4. Abstractions
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 5. Containerization
      • Pre-Class TODO's
      • Docker Installation
      • Lecture
      • Demo
      • Assignment Handout
    • 6. Deployment
      • Lecture
      • Demo
      • Assignment Handout
    • 7. Images
      • Demo
      • Assignment Handout
    • 8. Authentication
      • Lecture
      • Demo
      • Assignment Handout
    • (Work in Progress) OAuth
      • Pre-Class TODO's
      • Lecture
      • Demo
      • OAuth 2.0 vs OpenID
      • Flask / OpenID example
  • Additional Topics
    • Git and Github
    • HackOurCampus
  • Other AppDev Courses
    • Intro to iOS Development
    • Intro to Android Development
    • Intro to Digital Product Design
  • Deprecated
    • Previous Semester Syllabi
      • FA22 Syllabus
      • SP22 Syllabus
      • FA21 Syllabus
      • SP21 Syllabus
      • FA20 Syllabus
      • SP20 Syllabus
    • Deployment Pre-Class TODO's
    • PA6 Assignment Handout
    • Deployment Demo
    • Final Project (Spring 2019)
      • Final Project Award Winners
Powered by GitBook
On this page

Was this helpful?

  1. Chapters
  2. 6. Deployment

Demo

Contributors: Alanna Zhou, Shungo Najima, Joshua Dirga

PreviousLectureNextAssignment Handout

Last updated 9 days ago

Was this helpful?

Watch Demo Video

Uploaded on the AppDev YouTube channel!

Steps to deploy your project to a virtual machine in Google Cloud!

  1. Prepare your project. Make sure it is ready to be containerized

    You may use this Dockerfile template here.

Dockerfile
FROM python:3.7

RUN mkdir usr/app
WORKDIR usr/app

COPY . .

RUN pip install -r requirements.txt
CMD python app.py
  1. Configure your billing, for students enrolled in our course, navigate to and Start Free Trial or Redeem Credit.

  2. Create your VPC Network

    1. Name: name it anything you want!

    2. Description: anything you want (optional)

    3. Enable automatic subnet creation

    4. Under Firewall Rules: enable Allow SSH

  3. Create your VM

    1. Machine Configuration (First tab on the left)

      1. Name: name it anything you want!

      2. Region: anyone should be fine, but closest to Ithaca is Northern Virginia.

      3. Machine type: any, but to save costs we use e2-small

    2. OS and storage (Second tab on the left)

      1. Click "Change" and change the operating system to Ubuntu

    3. Networking (Fourth tab on the left)

      1. Under firewall, check "Allow HTTP traffic"

      2. Under network interface, set the network to the VPC you created just now

    4. Click "Create" to create you VM!

  4. Build your Docker image locally and push to Dockerhub

    1. docker build --platform=linux/amd64 -t joshuadirga/demo-5:v1.0.0 . Make sure to set the platform flag to be linux/amd64 because our VM is using Ubuntu, a Linux based OS. Remember that the format for the Docker image is accountname/reponame:tagname

    2. Run docker push joshuadirga/demo-5:v1.0.0 to push to Dockerhub

    3. Check that your image is on .

  5. SSH into your VM by clicking the "SSH" button under the "Connect" column

  6. Follow steps 1 on the and the to install Docker and Docker Compose. (Make sure in the drop down choose the one with the latest ubuntu version)

  7. Pull your Docker image from Dockerhub using sudo docker pull joshuadirga/demo-5:v1.0.0 Check that it successfully pulled by listing the images on your VM using sudo docker images

  8. Create a docker-compose.yml file using vim. vim docker-compose.yml This will create a new docker-compose.yml file if it has not been created already. Some useful VIM commands: - press "i" on the keyboard to enter insertion mode (you can only edit in insertion mode) - press "esc" to escape insertion mode - once escaped from insertion mode type :w to save file or :q to exit VIM. Use :wq to save and exit at the same time. Make sure the image name is the same one you pushed to Dockerhub earlier. Remember that the port 80:5000 refers to container's port : app's port. The container's port is the port you are running you Docker Container on inside the VM. The app's port is the port inside the container, where you are running your app (remember you app.py file uses the port 5000). Here is a template you may use for the docker-compose.yml!

docker-compose.yml
version: '3'

services:
  demo:
    image: joshuadirga/demo-5:v1.0.0
    ports:
      - "80:5000"
  1. Check that you docker-compose.yml exists by running cat docker-compose.yml

  2. Run you app using sudo docker-compose up -d to run in detached mode

  3. Check that its running using sudo docker ps to list all the running containers. Copy your container's id, and then use the command sudo docker logs YOURCONTAINERID To view the logs from your container. The logs should be similar to what you see when you run your app locally!

  4. On GCP (Google Cloud Platform), click on the External IP for your VM to access the backend on your browser. Copy the URL over to postman to test, and CONGRATULATIONS YOU'VE MADE YOUR FIRST DEPLOYMENT!!

  5. Run sudo docker-compose down to kill your container. The next time you run, just run sudo docker-compose up -d again because your images are still in your VM! If you made a change to your backend code and want to redeploy, here are the steps:

    1. Build using docker build --platform=linux/amd64 -t joshuadirga/demo-5:v1.0.0 . (Do this locally on your laptop's command line)

    2. Push using docker push joshuadirga/demo-5:v1.0.0 . (Do this locally on your laptop's command line)

    3. Pull from Dockerhub using sudo docker pull joshuadirga/demo-5:v1.0.0 (Do this in your server)

    4. Use sudo docker-compose up -d to run!

Additional Tips:

  • You only need to change your docker-compose.yml if you are changing any of the configurations in running your container

  • A lot fo the problems encountered in this deployment setup is port issues, so remember to use port 80:5000 or something like 8080:5000 in your docker-compose.yml file.

Google Cloud
Dockerhub
Docker installation guide
Docker Compose installation guide