Docker is a game-changing technology that enables us to run applications locally. We package our Apps into Containers that include everything needed to run the application

Docker Resources

Phidata enables us to define docker resources as pydantic objects so we can build our application layer purely in python. In most cases you will not be creating the Docker Resources directly, instead we’ll use Apps to create the resources for us.

Benefits

  • Define containers and images as pydantic objects with input and type validation.
  • Allows re-use and testing of resources.
  • Import them in software layer like regular python objects.
  • Package multiple resources into Apps so we can define “Applications as Code”.
  • Enable AI features that interact with the resource from python code.

Container

The DockerContainer class defines a container, for example use the following code to define a container running the whoami image. Start it using phi start resources.py

resources.py
from phi.docker.resource.container import DockerContainer

whoami = DockerContainer(
    name='whoami',
    image='traefik/whoami',
    ports={'80': 80},
)

Test it by opening http://localhost:80 or using:

curl -X POST http://localhost:80

The same can be defined as an App:

resources.py
from phi.docker.app.whoami import Whoami

whoami = Whoami()

Stop resources using phi stop resources.py

Image

The DockerImage class defines an image, for example use the following code create your own python image and run it in a container. Build it using phi start resources.py

from phi.docker.resource.container import DockerContainer
from phi.docker.resource.image import DockerImage

python_image = DockerImage(
    name="my/python",
    tag="3.11",
    path=".",
    # push_image=True,
)

python_container = DockerContainer(
    name='python',
    image=python_image.get_image_str(),
)

Make sure to add the Dockerfile in the current directory.