Docker Tutorials

Project

Objective : Create a docker image of python script , publish on docker-hub and use it in k8s

Steps –

  1. Create Script/Application to deploy
  2. Build
    1. Create a file named Dockerfile without any file extension in your project directory,[refer sample code below]
    2. Create a requirements.txt file in the same directory as your Dockerfile if you have any dependencies. If you don’t have any dependencies, you can skip this step.
    3. Build the Docker image from the same directory as your Dockerfile:
      • docker build -t demo_log_app .
        1. This assumes that your Dockerfile is in the current working directory and is named Dockerfile. The dot . indicates the current directory.
      • Run the Docker container:
        1. docker run demo_log_app
  3. Push docker image to registry[optional]
    1. If your Docker image is stored locally on your machine, you might want to push it to a Docker registry (e.g., Docker Hub) so that it can be easily pulled by other machines or services.
    2. If you’re using Docker Hub, you need to log in first:
      • docker login
    3. Tag your image with your Docker Hub username and the repository name:
      • docker tag demo_log_app your-dockerhub-username/demo_log_app:latest
    4. Push the image to the Docker Hub:
      • docker push your-dockerhub-username/demo_log_app
  4. Deploy docker image
    1. Local Deployment:
      • If you’re deploying the Docker image locally, you can run the container using the following command:
      • docker run your-dockerhub-username/demo_log_app
    2. Docker Compose:If you’re using Docker Compose to manage multiple containers, create a docker-compose.yml file, and then run:
      • docker-compose up
    3. Orchestration Tools (e.g., Kubernetes):If you’re deploying to an orchestration platform like Kubernetes, you’ll need to create Kubernetes manifests and apply them using kubectl.

Code reference Steps-

#1 Application/script: demo_log_app.py

import logging
import time

# Configure the logging system
logging.basicConfig(filename='example.log', level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

# Create a stream handler to also log to the console
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)

# Get the root logger
root_logger = logging.getLogger('')
root_logger.setLevel(logging.INFO)

# Add the stream handler to the root logger
root_logger.addHandler(console_handler)

# Log a line
logging.info('This is a log message.')

def demoLogWait():
    logging.info('Waiting for a minute...')
    time.sleep(60)  # Sleep for 60 seconds (1 minute)
    logging.info('Waited for a minute. Continue with the script.')

# Call the demoLogWait method
demoLogWait()

#2 Build : Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Run script.py when the container launches
CMD ["python", "demo_log_app.py"]

#Deploy

use deployment command mentioned in steps above as per the requirement and tools

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment