Skip to main content

Command Palette

Search for a command to run...

Docker Project for DevOps Engineers๐Ÿณ

Published
โ€ข3 min readโ€ขView as Markdown
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

  1. Create a Dockerfile for a simple web application (Node.js or Python)

  2. Build the image using the Dockerfile

  3. Run the container

  4. Verify that the application is working as expected by accessing it in a web browser

  5. Push the image to a public or private repository (e.g., Docker Hub)


๐Ÿ› ๏ธ Step 1: Create Your Web Application

Node.js Example

  1. Initialize a new Node.js project:

     mkdir my-node-app
     cd my-node-app
     npm init -y
    
  2. Create a simple server.js file:

     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

  1. Initialize a new Python project:

     mkdir my-python-app
     cd my-python-app
    
  2. Create a simple app.py file:

     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:

You should see "Hello, World! ๐ŸŒ" displayed on the page.


โ˜๏ธ Step 6: Push the Image to a Repository

  1. Log in to Docker Hub:

     docker login
    
  2. Tag your image:

     docker tag my-app your-dockerhub-username/my-app
    
  3. Push 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!