Skip to main content

Command Palette

Search for a command to run...

Day 17 Docker Project for DevOps Engineers

Published
2 min read
Day 17 Docker Project for DevOps Engineers
F

Hi there! I'm Fauzeya 👩‍💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀

Step-by-Step Guide: Docker Project with Dockerfile


1. Create a Simple Flask Web Application

Directory Structure:

my-flask-app/ │ ├── app.py ├── requirements.txt └── Dockerfile

app.py (Your main application file):

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World! This is my Flask app!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

requirements.txt (Python dependencies):

Flask==2.0.1

2. Create the Dockerfile

Dockerfile:

# Use the official Python image from the Docker Hub
FROM python:3.9

# Set the working directory inside the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the required Python packages
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application files into the container
COPY app.py .

# Specify the command to run the application
CMD ["python", "app.py"]

# Expose the application port
EXPOSE 5000

3. Build the Docker Image

Open your terminal, navigate to the directory containing your Dockerfile, and run the following command:

docker build -t my-flask-app .
  • What It Does: This command builds a Docker image named my-flask-app based on the instructions in the Dockerfile.

4. Run the Docker Container

After building the image, run the container with the following command:

docker run -d -p 5000:5000 my-flask-app
  • What It Does:

    • -d: Runs the container in detached mode (in the background).

    • -p 5000:5000: Maps port 5000 on your host to port 5000 in the container.


5. Verify the Application is Running

Open your web browser and go to http://localhost:5000. You should see:

Hello, World! This is my Flask app!

6. Push the Docker Image to a Repository

Before pushing, make sure you're logged in to Docker Hub:

docker login

Then, tag your image and push it:

docker tag my-flask-app your-dockerhub-username/my-flask-app
docker push your-dockerhub-username/my-flask-app
  • What It Does:

    • docker tag: Tags your local image for the Docker Hub.

    • docker push: Pushes the tagged image to your Docker Hub repository.


Summary of Steps

  1. Create a simple Flask application with an entry point and dependencies.

  2. Write a Dockerfile to package your application.

  3. Build the Docker image using docker build.

  4. Run the container and verify the application works.

  5. Push the image to a Docker repository.