Skip to main content
Version: 0.0.x

Build Docker Image - Python Worker

This guide will walk you through the process of dockerizing a Python worker that uses the VideoSDK Python SDK. Dockerizing your worker allows you to package it along with all its dependencies, ensuring consistency across different environments.

Prerequisites

  • Docker installed on your system.
  • Valid VideoSDK Account

Step 1: Create Your Python Worker Script:

Below is a minimalistic example of a Python worker using VideoSDK:

import asyncio
import os
from videosdk import (
MeetingConfig,
VideoSDK,
Participant,
Stream,
MeetingEventHandler,
ParticipantEventHandler,
Meeting,
)

from dotenv import load_dotenv

load_dotenv()
VIDEOSDK_TOKEN = os.getenv("VIDEOSDK_TOKEN")
MEETING_ID = os.getenv("MEETING_ID")
NAME = os.getenv("NAME")

loop = asyncio.get_event_loop()

meeting: Meeting = None


class MyMeetingEventHandler(MeetingEventHandler):
def __init__(self):
super().__init__()

def on_meeting_left(self, data):
print("meeting left")

def on_participant_joined(self, participant: Participant):
print("participant joined")
participant.add_event_listener(ParticipantEventHandler(participant=participant))

def on_participant_left(self, participant: Participant):
print("participant left")


class ParticipantEventHandler(ParticipantEventHandler):
def __init__(self, participant: Participant):
super().__init__()
self.participant = participant

def on_stream_enabled(self, stream: Stream):
print("stream enabled", stream.kind)

def on_stream_disabled(self, stream: Stream):
print("stream disabled", stream.kind)


def main():
global meeting

meeting_config = MeetingConfig(
meeting_id=MEETING_ID,
name=NAME,
mic_enabled=False,
webcam_enabled=False,
token=VIDEOSDK_TOKEN,
)
meeting = VideoSDK.init_meeting(**meeting_config)

print("adding event listener...")
meeting.add_event_listener(MyMeetingEventHandler())

print("joining into meeting...")
meeting.join()


if __name__ == "__main__":
main()
loop.run_forever()

Step 2: Create requirements.txt

If your Python script relies on other dependencies, you should specify them in a requirements.txt file. Here’s an example:

videosdk==0.0.5
python-dotenv==1.0.1
asyncio==3.4.3

Step 3: Configure Environment Variables

To securely pass your Auth Token and other sensitive information to the Docker container, you should use environment variables. Here, you will create a .env file to store your environment variables locally.

VIDEOSDK_TOKEN="YOUR_VIDEOSDK_TOKEN"
MEETING_ID="YOUR_MEETING_ID"
NAME="YOUR_NAME"

Step 4: Write a Dockerfile

A Dockerfile is a script that contains a series of commands used to create a Docker image. Here’s an example Dockerfile for your Python worker:

FROM python:3.11-slim

RUN apt-get update && apt-get install -y python3-dev

WORKDIR /app

COPY requirements.txt .

RUN python -m pip install -r requirements.txt

COPY . .

ENTRYPOINT ["python", "-u" , "main.py"]

Step 5: Build the Docker Image

In the same directory as your Dockerfile and Python script, run the following command to build your Docker image:

docker build -t videosdk-python-worker .

This command creates a Docker image named videosdk-python-worker.

Step 6: Run the Docker Container

Once the image is built, you can run it using the following command:

docker run --env ENV_VARS="$(cat ./.env)" videosdk-python-worker

Step 7: Push to a Container Registry (Optional)

If you want to deploy your Dockerized python worker on a platform like AWS, GCP, or Azure, you might want to push the image to a container registry like Docker Hub.

Log in to Docker Hub:

docker login

Tag your image:

docker tag videosdk-python-worker your_dockerhub_username/videosdk-python-worker

Replace your_dockerhub_username with your dockerhub username.

Push your image to Docker Hub:

docker push your_dockerhub_username/videosdk-python-worker
tip

Stuck anywhere? Check out this example code on GitHub

Conclusion

You've successfully dockerized your Python worker using the VideoSDK Python SDK, including configuring environment variables for security. This Dockerized version can be deployed in any environment that supports Docker, ensuring consistency and scalability.

Got a Question? Ask us on discord