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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay