DEV Community

Cover image for 🤖End to end LLMOps Pipeline-Part 3 - Docker 🤖
Prashant Lakhera
Prashant Lakhera

Posted on

🤖End to end LLMOps Pipeline-Part 3 - Docker 🤖

In the past two days, we've learned about Hugging Face and FastAPI. Now that the code is ready, it's time to bring your LLMOps skills into play.

What is Docker?
Docker is an open platform designed to simplify the development, shipping, and running of applications in isolated environments known as containers. These containers bundle everything your application needs to run - code, runtime, libraries, and system tools - ensuring that your application behaves the same, regardless of where it is deployed. Docker's lightweight nature allows developers to automate the deployment of applications inside these portable containers, making it an essential tool in modern software development.

What is a Docker Image?
A Docker image is a lightweight, standalone, executable package that contains everything needed to run a specific piece of software. This includes the application code, runtime, libraries, environment variables, and configuration files. Docker images are created from a Dockerfile and can be stored in repositories like Docker Hub or other container registries. These images are the building blocks of Docker containers, providing the blueprint for what goes inside each container.

What is a Dockerfile?
A Dockerfile is a text file that contains a set of instructions to build a Docker image automatically. It outlines everything that goes into your Docker image, from the base image to the environment variables, files, and commands needed to run your application. By defining the environment in which your application runs, Dockerfile ensures consistency across different deployment environments.

Creating the Docker Image
Now that we have a basic understanding of Docker, Docker images, and Dockerfiles, it's time to put these concepts into practice by creating a Docker image for our FastAPI application.
Prerequisites:
Docker installed on your local machine.
A main.py file created in Part 2, which contains the FastAPI application.
A requirements.txt file listing the dependencies.

Dockerfile
Here's the Dockerfile we'll use to build the Docker image for our FastAPI application:

# Use the official Python image from the Docker Hub
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /app
# Copy only the requirements file to leverage Docker cache
COPY requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code to the working directory
COPY . .
# Expose port 8000
EXPOSE 8000
# Use a minimal entrypoint and CMD
ENTRYPOINT ["python"]
CMD ["main.py"]
Enter fullscreen mode Exit fullscreen mode

Dockerfile Explanation:
Base Image: FROM python:3.10-slim: This line specifies the base image to use, in this case, the slim version of Python 3.10. The slim version is lightweight, containing only the essential packages needed to run Python.
Working Directory: WORKDIR /app: This command sets the working directory inside the container to /app, where all subsequent commands will be executed.
Copying Dependencies: COPY requirements.txt .: This command copies the requirements.txt file from your local machine to the container's working directory, allowing Docker to cache the installed dependencies and speed up subsequent builds.
Installing Dependencies: RUN pip install - no-cache-dir -r requirements.txt: This command installs the Python dependencies listed in the requirements.txt file. The - no-cache-dir option ensures that pip doesn't store any installation files, keeping the Docker image size smaller.
Copying Application Code: COPY . .: This command copies the rest of your application's code into the container's working directory.
Exposing Port: EXPOSE 8000: This command indicates that the container listens on port 8000 at runtime. To access the application from outside the container, map the port using the -p option when you run the container.
Entrypoint and CMD: ENTRYPOINT ["python"] sets the entrypoint for the container to the Python command, meaning the container will execute Python when it starts. CMD ["main.py"] specifies the default Python script to run, which is the FastAPI application in this case.

Building and Running the Docker Image
Building the Docker Image:
To build the Docker image, use the following command:

docker build -t fastapi-app .
Enter fullscreen mode Exit fullscreen mode

This command instructs Docker to build the image from the Dockerfile in the current directory and tag it as fastapi-app.
Running the Docker Container:
Once the image is built, you can run the container using:

docker run -p 8000:8000 fastapi-app
Enter fullscreen mode Exit fullscreen mode

This command runs the container, mapping port 8000 of the container to port 8000 on your host machine. You can now access the FastAPI application at http://localhost:8000.

Image description

📚 If you'd like to learn more about this topic, please check out my book. Building an LLMOps Pipeline Using Hugging Face
https://pratimuniyal.gumroad.com/l/BuildinganLLMOpsPipelineUsingHuggingFace

Top comments (0)