Building a Simple Cloud-Native App with Docker
John Ajera
Posted on November 26, 2024
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}`);
});
- Install dependencies:
npm init -y
npm install express
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"]
Step 3: Build and Run the Docker Container
- Build the Docker image:
docker build -t cloud-native-app .
- Run the container:
docker run -p 3000:3000 cloud-native-app
Step 4: Access the Application
- Open your browser and visit
http://localhost:3000
. You should see:
Hello from Docker Cloud-Native App!
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.
💖 💪 🙅 🚩
John Ajera
Posted on November 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.