DEV Community

John  Ajera
John Ajera

Posted on

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more