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.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay