Technology, Programming & Tech Blog

  • Deploying a Flask or Node.js App on a Cloud VM Using Docker

    Deploying a Flask or Node.js App on a Cloud VM Using Docker

    If you’re a developer looking to deploy a Flask (Python) or Node.js (JavaScript) web application, using Docker on a Cloud VM is one of the best ways to ensure portability, scalability, and easy management.

    In this guide, we’ll cover:
    Why Use Docker for Deployment?
    Choosing a Cloud VM Provider
    Deploying a Flask App in Docker
    Deploying a Node.js App in Docker


    🚀 Why Use Docker for Web App Deployment?

    Docker provides containerization, allowing you to package your app and dependencies into a lightweight, portable container.

    🔹 Benefits of Docker for Deployment:
    ✔️ Portability – Run the same container on any machine
    ✔️ Scalability – Easily scale up with multiple containers
    ✔️ Security – Isolates your app from the host system
    ✔️ Easy Updates – Deploy new versions with a single command


    ☁️ Choosing a Cloud VM Provider

    To host your application, you’ll need a Cloud VM provider. Some great options include:

    🔹 Vultr Cloud VMs – Affordable and powerful VPS hosting
    🔹 AWS EC2 – Enterprise-grade cloud computing
    🔹 DigitalOcean Droplets – Easy-to-use Linux servers

    💡 For this guide, we’ll use Vultr, but the steps apply to any cloud provider.


    🛠 Setting Up Your Cloud VM

    1️⃣ Create a Cloud VM

    • Sign up at Vultr
    • Choose Ubuntu 22.04 as the OS
    • Select at least 1GB RAM & 1 vCPU

    2️⃣ Connect to Your Server
    Once the VM is created, SSH into it:

    ssh root@YOUR_VM_IP
    

    🐳 Installing Docker on Your Cloud VM

    First, update your system:

    sudo apt update && sudo apt upgrade -y
    

    Then, install Docker:

    sudo apt install -y docker.io
    

    Verify Docker is installed:

    docker --version
    

    ✅ Docker is now set up on your Cloud VM!


    🌍 Deploying a Flask App in Docker

    Let’s deploy a simple Flask app inside a Docker container.

    Step 1: Create the Flask App

    First, create a new project directory:

    mkdir ~/flask-app && cd ~/flask-app
    

    Create app.py:

    nano app.py
    

    Add the following Flask code:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return "Hello, Flask in Docker!"
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
    

    Step 2: Create a Dockerfile

    Next, create a Dockerfile:

    nano Dockerfile
    

    Add the following content:

    # Use an official Python runtime as a base image
    FROM python:3.9
    
    # Set the working directory
    WORKDIR /app
    
    # Copy the application files
    COPY app.py /app/
    
    # Install Flask
    RUN pip install flask
    
    # Expose the app port
    EXPOSE 5000
    
    # Run the application
    CMD ["python", "app.py"]
    

    Step 3: Build and Run the Container

    Now, build the Docker image:

    docker build -t flask-app .
    

    Run the container:

    docker run -d -p 5000:5000 flask-app
    

    Step 4: Access Your Flask App

    Your app should now be running at:

    http://YOUR_VM_IP:5000
    

    🎉 You’ve deployed a Flask app using Docker!


    🌎 Deploying a Node.js App in Docker

    Let’s now deploy a Node.js app in Docker.

    Step 1: Create the Node.js App

    mkdir ~/node-app && cd ~/node-app
    

    Create server.js:

    nano server.js
    

    Add this Node.js code:

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
        res.send('Hello, Node.js in Docker!');
    });
    
    app.listen(3000, () => {
        console.log('Server is running on port 3000');
    });
    

    Step 2: Create a Dockerfile

    nano Dockerfile
    

    Add the following content:

    # Use an official Node.js runtime as a base image
    FROM node:16
    
    # Set the working directory
    WORKDIR /app
    
    # Copy package.json and install dependencies
    COPY package.json /app/
    RUN npm install
    
    # Copy the rest of the application files
    COPY . /app/
    
    # Expose the app port
    EXPOSE 3000
    
    # Run the application
    CMD ["node", "server.js"]
    

    Step 3: Create a package.json File

    nano package.json
    

    Add this content:

    {
      "name": "docker-node-app",
      "version": "1.0.0",
      "main": "server.js",
      "dependencies": {
        "express": "^4.17.1"
      }
    }
    

    Step 4: Build and Run the Container

    Build the Docker image:

    docker build -t node-app .
    

    Run the container:

    docker run -d -p 3000:3000 node-app
    

    Step 5: Access Your Node.js App

    Your app should now be running at:

    http://YOUR_VM_IP:3000
    

    🎉 You’ve successfully deployed a Node.js app using Docker!


    ☁️ Want a More Scalable Deployment? Use Docker Compose!

    Instead of running multiple docker run commands, you can use Docker Compose to manage both Flask and Node.js containers in one file.

    Create a docker-compose.yml file:

    version: '3'
    services:
      flask:
        build: ./flask-app
        ports:
          - "5000:5000"
      node:
        build: ./node-app
        ports:
          - "3000:3000"
    

    Start both containers at once:

    docker-compose up -d
    

    🔥 Now both apps are running with a single command!


    🎯 Conclusion: Deploy Your Apps with Ease

    By using Docker on a Cloud VM, you can:
    Easily deploy Python (Flask) and JavaScript (Node.js) apps
    Ensure your apps run consistently across environments
    Scale applications with Docker Compose

    💻 Looking for a powerful Mini PC to self-host instead?
    👉 Check Out AMD Mini PCs

    ☁️ Want a reliable Cloud VM for hosting?
    👉 Deploy on Vultr

    📖 Want to Master Linux for Server Deployments?
    👉 Best Linux OS Books

    🐍 Need More Python Knowledge?
    👉 Top Python Books

    🔥 Have questions? Drop them in the comments below! 🚀