> For the complete documentation index, see [llms.txt](https://backend-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://backend-course.cornellappdev.com/chapters/deployment/deployment-demo.md). Agents can also search and read this documentation through its [MCP server](https://backend-course.cornellappdev.com/~gitbook/mcp).

# Demo

Contributors: Alanna Zhou, Shungo Najima, Joshua Dirga, Chris Voon

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.

   * Important: Ensure your app.py runs on host 0.0.0.0 (e.g., app.run(host="0.0.0.0", port=5000)). If it defaults to 127.0.0.1, it won't accept external traffic inside Docker.

   You may use this Dockerfile template:

{% code title="Dockerfile" %}

```
FROM python:3.11-slim

WORKDIR /usr/src/app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "app.py"]
```

{% endcode %}

2. Configure your billing. For students enrolled in our course, navigate to [Google Cloud](https://cloud.google.com/) and **Start Free Trial** or **Redeem Credit**.
3. 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
4. Create your VM
   1. Machine Configuration (First tab on the left)
      1. Name: name it anything you want!
      2. Region: any should be fine, but the closest to Ithaca is Northern Virginia (us-east-4).
      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 (LTS)
   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 your VM!
5. Build your Docker image locally and push to Dockerhub
   1. `docker build --platform=linux/amd64 -t yourname/demo-6:v1.0.0 .` \
      Make sure to set the platform flag to be `linux/amd64` because our VM is using Ubuntu on x86 architecture. Remember that the format for the Docker image is `accountname/reponame:tagname`&#x20;
   2. Run `docker push yourname/demo-6:v1.0.0`  to push to Dockerhub
   3. Check that your image is on [Dockerhub](https://hub.docker.com).
6. SSH into your VM by clicking the "SSH" button under the "Connect" column
7. Follow the steps in the [Docker installation guide](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04) to install Docker (make sure you choose your Ubuntu version from the drop-down).
8. Pull your Docker image from Dockerhub using\
   `sudo docker pull yourname/demo-6:v1.0.0`\
   Check that it successfully pulled by listing the images on your VM using\
   `sudo docker images`&#x20;
9. Create a docker-compose.yml file using nano. `nano docker-compose.yml`\
   This will create a new docker-compose.yml file if it has not been created already.\
   \
   **Some useful nano tips:**\
   \-  type or paste directly to edit (no separate insert mode needed)\
   \-  press Ctrl + O then Enter to save the file

   \-  press Ctrl + X to exit nano (if you have unsaved changes, press Y then Enter when prompted)\
   \
   Make sure the image name is the same one you pushed to Dockerhub earlier. Remember that the port 80:5000 refers to `host's port : container's port`. The host's port (80) is the port exposed on your VM to the outside internet (standard HTTP). The container's port (5000) is the port inside the container, where you are running your app (remember your app.py file uses the port 5000). Here is a template you may use for the docker-compose.yml!

{% code title="docker-compose.yml" %}

```
services:
  demo:
    image: yourname/demo-6:v1.0.0
    ports:
      - "80:5000"
```

{% endcode %}

10. Check that your docker-compose.yml exists by running `cat docker-compose.yml`&#x20;
11. Run your app using `sudo docker compose up -d`  to run in detached mode
12. Check that it's 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!
13. On GCP (Google Cloud Platform), copy the External IP for your VM and paste it into a browser to access the backend on your browser (make sure the URL starts with `http://`, not `https://`, since we only configured HTTP on port 80). Copy the URL over to Postman to test, and CONGRATULATIONS YOU'VE MADE YOUR FIRST DEPLOYMENT!!
14. 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 yourname/demo-6:v1.0.0 .`  (Do this locally on your laptop's command line)
    2. Push using `docker push yourname/demo-6:v1.0.0` (Do this locally on your laptop's command line)
    3. Pull from Dockerhub using `sudo docker pull yourname/demo-6: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 for running your container
* A lot of the problems encountered in this deployment setup are port issues, so remember to use port `80:5000`  in your docker-compose.yml file. If you ever change it to something like `8080:5000`, make sure to add a custom GCP firewall rule allowing TCP traffic on port 8080.
