Docker Project for DevOps Engineers๐ณ

In this blog post, we'll walk through the steps to create a Dockerfile for a simple web application using Node.js or Python. We'll build the image, run the container, verify the application, and push the image to a repository like Docker Hub. Let's get started! ๐
๐ฆ Task Overview
Create a Dockerfile for a simple web application (Node.js or Python)
Build the image using the Dockerfile
Run the container
Verify that the application is working as expected by accessing it in a web browser
Push the image to a public or private repository (e.g., Docker Hub)
๐ ๏ธ Step 1: Create Your Web Application
Node.js Example
Initialize a new Node.js project:
mkdir my-node-app cd my-node-app npm init -yCreate a simple
server.jsfile:const http = require('http'); const hostname = '0.0.0.0'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World! ๐\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Python Example
Initialize a new Python project:
mkdir my-python-app cd my-python-appCreate a simple
app.pyfile:from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World! ๐' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
๐ Step 2: Create a Dockerfile
For Node.js
Create a file named Dockerfile in the project directory:
# Use the official Node.js image
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Command to run the app
CMD ["node", "server.js"]
For Python
Create a file named Dockerfile in the project directory:
# Use the official Python image
FROM python:3.9
# Set the working directory
WORKDIR /usr/src/app
# Copy requirements.txt and install dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 5000
# Command to run the app
CMD ["python", "app.py"]
Note:
For Python, create a requirements.txt file with:
Flask
๐ง Step 3: Build the Docker Image
Run the following command in your terminal:
docker build -t my-app .
๐โโ๏ธ Step 4: Run the Container
Run your application in a container:
For Node.js
docker run -p 3000:3000 my-app
For Python
docker run -p 5000:5000 my-app
๐ Step 5: Verify the Application
Open your web browser and go to:
Node.js:
http://localhost:3000Python:
http://localhost:5000
You should see "Hello, World! ๐" displayed on the page.
โ๏ธ Step 6: Push the Image to a Repository
Log in to Docker Hub:
docker loginTag your image:
docker tag my-app your-dockerhub-username/my-appPush the image:
docker push your-dockerhub-username/my-app
๐ Conclusion
Congratulations! You've successfully created a Dockerfile for a simple web application, built the image, run the container, verified the application, and pushed it to a repository. Docker simplifies the process of deploying applications, and you can now share your app easily!



