Day 17: Docker Project for DevOps Engineers (Part-1)

DevOps Learning

Let's create a Docker image for a simple web application, run it, verify its functionality, and then push it to a repository.

Clone the project:

https://github.com/skay49/flaskApp.git

You can use the Dockerfile that comes with the project, or you can create your own.

1. Create a Dockerfile:

First, let’s create a Dockerfile for our web application. I’ll provide examples for a Python app:

Python Example (Dockerfile):

# Use an official Python image as the base
FROM python:3.9

# Set the working directory inside the container
WORKDIR /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 your app listens on
EXPOSE 5000

# Start the app
CMD ["python", "app.py"]

2. Build the Docker Image:

Navigate to the directory containing your Dockerfile and run the following command to build the image:

docker build -t my-web-app .

3. Run the Container:

Now let’s run the container based on the image we just built:

docker run -d -p 5000:5000 my-web-app

4. Verify the Application:

Open a web browser and visit http://localhost:5000 . You should see your web application running!

5. Push the Image to a Repository:

To push the image to a repository (e.g., Docker Hub), follow these steps:

  1. Log in to your Docker Hub account:

     docker login
    
  2. Tag your local image with your Docker Hub username and repository name:

     docker tag my-web-app yourusername/my-web-app
    
  3. Push the image to Docker Hub:

     docker push yourusername/my-web-app
    

Thank you for reading😉.