Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Integrating with Docker - CrewAI

Introduction

Docker is a platform that allows developers to create, deploy, and run applications in containers. Containers are lightweight and contain everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. This tutorial will guide you through the process of integrating CrewAI with Docker, from setting up Docker to deploying a CrewAI application in a Docker container.

Prerequisites

Before you begin, ensure you have the following installed on your system:

  • Docker
  • Docker Compose (optional, for multi-container applications)
  • Basic knowledge of Docker and command-line interface

Step 1: Installing Docker

First, you need to install Docker on your machine. Follow the instructions on the official Docker website to download and install Docker for your operating system.

Example

On Ubuntu, you can install Docker using the following commands:

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
                    Reading package lists... Done
                    Building dependency tree       
                    Reading state information... Done
                    The following additional packages will be installed:
                      ...
                    Setting up docker-ce (5:20.10.7~3-0~ubuntu-focal) ...
                    ...
                    

Step 2: Setting Up Your CrewAI Application

Create a simple CrewAI application. For this tutorial, we'll create a basic Python application. Create a new directory for your project and navigate into it:

mkdir crewai-app
cd crewai-app

Next, create a Python file named app.py with the following content:

touch app.py
                    # app.py
                    from flask import Flask
                    app = Flask(__name__)

                    @app.route('/')
                    def hello():
                        return "Hello, CrewAI!"

                    if __name__ == '__main__':
                        app.run(host='0.0.0.0', port=80)
                    

Step 3: Writing a Dockerfile

A Dockerfile is a text document that contains all the commands to assemble an image. Create a Dockerfile in the root of your project directory:

touch Dockerfile

Add the following content to the Dockerfile:

                    # Dockerfile
                    FROM python:3.8-slim-buster

                    WORKDIR /app

                    COPY requirements.txt requirements.txt
                    RUN pip install -r requirements.txt

                    COPY . .

                    CMD ["python", "app.py"]
                    

Create a requirements.txt file to list the dependencies:

touch requirements.txt

Add Flask to the requirements.txt:

                    # requirements.txt
                    Flask==2.0.1
                    

Step 4: Building the Docker Image

Now that you have a Dockerfile, you can build your Docker image. Run the following command in your project directory:

docker build -t crewai-app .

Docker will read your Dockerfile and build an image according to the instructions. You should see output similar to this:

                Sending build context to Docker daemon  7.168kB
                Step 1/6 : FROM python:3.8-slim-buster
                 ---> f7a292bbb70c
                Step 2/6 : WORKDIR /app
                 ---> Using cache
                 ---> 94c673d0c7a6
                Step 3/6 : COPY requirements.txt requirements.txt
                 ---> Using cache
                 ---> e87d20b1f3a3
                Step 4/6 : RUN pip install -r requirements.txt
                 ---> Using cache
                 ---> 7d9e6a2b6d38
                Step 5/6 : COPY . .
                 ---> Using cache
                 ---> 8a7e6b7e3d3e
                Step 6/6 : CMD ["python", "app.py"]
                 ---> Using cache
                 ---> 0a1e6b8b7e4d
                Successfully built 0a1e6b8b7e4d
                Successfully tagged crewai-app:latest
                

Step 5: Running the Docker Container

Once the image is built, you can run it as a container. Use the following command to run the container:

docker run -d -p 80:80 crewai-app

This command runs the container in detached mode and maps port 80 of the host to port 80 of the container. You can now access your CrewAI application by navigating to http://localhost in your web browser.

Conclusion

Congratulations! You've successfully integrated CrewAI with Docker. You learned how to create a Dockerfile, build a Docker image, and run a Docker container. This setup can be further extended for more complex applications and deployment scenarios.