Project
Objective : Create a docker image of python script , publish on docker-hub and use it in k8s
Steps –
- Create Script/Application to deploy
- Build
- Create a file named
Dockerfilewithout any file extension in your project directory,[refer sample code below] - Create a
requirements.txtfile in the same directory as yourDockerfileif you have any dependencies. If you don’t have any dependencies, you can skip this step. - Build the Docker image from the same directory as your
Dockerfile:- docker build -t demo_log_app .
- This assumes that your Dockerfile is in the current working directory and is named
Dockerfile. The dot.indicates the current directory.
- This assumes that your Dockerfile is in the current working directory and is named
- Run the Docker container:
- docker run demo_log_app
- docker build -t demo_log_app .
- Create a file named
- Push docker image to registry[optional]
- 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.
- If you’re using Docker Hub, you need to log in first:
- docker login
- Tag your image with your Docker Hub username and the repository name:
- docker tag demo_log_app your-dockerhub-username/demo_log_app:latest
- Push the image to the Docker Hub:
- docker push your-dockerhub-username/demo_log_app
- Deploy docker image
- 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
- Docker Compose:If you’re using Docker Compose to manage multiple containers, create a
docker-compose.ymlfile, and then run:- docker-compose up
- 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.
- Local Deployment:
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