Logo

dev-resources.site

for different kinds of informations.

Building a Simple Cloud-Native App with Docker

Published at
12/1/2024
Categories
devops
cloudnative
microservices
node
Author
jajera
Author
6 person written this
jajera
open
Building a Simple Cloud-Native App with Docker

Building a Simple Cloud-Native App with Docker

In this demo, we'll create a basic cloud-native application using Docker. This app will run in a container, and we’ll explore how Docker enables cloud-native principles such as portability and isolation.

Step 1: Create a Simple Node.js Microservice

  • First, create a server.js file:
  const express = require('express');
  const app = express();
  const port = process.env.PORT || 3000;

  app.get('/', (req, res) => {
    res.send('Hello from Docker Cloud-Native App!');
  });

  app.listen(port, () => {
    console.log(`Server running on port ${port}`);
  });
Enter fullscreen mode Exit fullscreen mode
  • Install dependencies:
  npm init -y
  npm install express
Enter fullscreen mode Exit fullscreen mode

Step 2: Dockerize the Application

  • Create a Dockerfile to define the container:
  # Use official Node.js image from Docker Hub
  FROM node:20

  # Set working directory inside the container
  WORKDIR /app

  # Copy package.json and install dependencies
  COPY package*.json ./
  RUN npm install

  # Copy the rest of the app's source code
  COPY . .

  # Expose the port the app will run on
  EXPOSE 3000

  # Command to run the app
  CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

Step 3: Build and Run the Docker Container

  • Build the Docker image:
  docker build -t cloud-native-app .
Enter fullscreen mode Exit fullscreen mode
  • Run the container:
  docker run -p 3000:3000 cloud-native-app
Enter fullscreen mode Exit fullscreen mode

Step 4: Access the Application

  • Open your browser and visit http://localhost:3000. You should see:
  Hello from Docker Cloud-Native App!
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've successfully built and run a simple cloud-native application using Docker. This app is now isolated in a container, and you can easily move or scale it across different environments without worrying about dependencies.

cloudnative Article's
30 articles in total
Favicon
Exploring the CNCF Landscape: A Comprehensive Overview of Cloud Native Technologies
Favicon
Highlights from Our January 2025 Cloud Native KL Event: Open Policy Agent, DexIDP Single Sign-On, and More!
Favicon
Understand Kubernetes Troubleshooting Cheat Sheet Now
Favicon
Understanding Node Problem Detector in Kubernetes: Beyond Default Node Conditions
Favicon
Site Reliability Engineering (SRE)
Favicon
What is Cloud Native?
Favicon
Managing Sensitive Data in Kubernetes: A Comprehensive Guide to K8s Secrets
Favicon
How to Automate CI/CD Pipelines with GitHub Actions
Favicon
Frugal Architecture: Embracing Cost-Effective Cloud-Native Design
Favicon
Hidden Power of Go: Unveiling the Secrets of a Robust Language
Favicon
Do you run your database on Kubernetes?
Favicon
Implementing Automated Scaling with Horizontal Pod Autoscaling (HPA)
Favicon
10,000 Followers on Dev.to
Favicon
12 Factor App Principles Explained
Favicon
Unlocking Cloud-Native Security with Cilium and eBPF
Favicon
A Cost-Effective Guide to prepare and pass the KCNA
Favicon
Bare Metal Provisioning Consulting Services Experts
Favicon
Cloud Migration Best Practices
Favicon
The Power of AWS Services
Favicon
Generics in Go: Transforming Code Reusability
Favicon
Leveraging AWS Lambda for a Modern Nigerian Lifestyle: A Serverless Computing Solution
Favicon
Leveraging AWS EC2 for a Modern Nigerian Lifestyle: A Cloud Computing Solution
Favicon
Kubernetes CRDs: The Backbone of Kubernetes Extensibility
Favicon
Tackling CPU Throttling in Kubernetes for Better Application Performance
Favicon
Key Lessons and Mistakes from Setting Up EKS Clusters
Favicon
Building a Simple Cloud-Native App with Docker
Favicon
Optimizing Kubernetes for High Availability (HA)
Favicon
Deploying and Managing Microservices with Kubernetes
Favicon
Scaling Applications with Kubernetes: A Guide to Horizontal Pod Autoscaling (HPA)
Favicon
Leveraging Docker for Cloud-Native Application Development

Featured ones: