Getting Started

This guide will help you get up and running with Contree SDK. By the end of this guide, you’ll understand how to create clients, work with images, and run your first commands.

Creating a Client

The first step is to create a Contree client. You can choose between async and sync versions depending on your application needs.

Here’s how to create a client and verify the connection by listing available images:

1# Get client
2client = Contree(token=token)
3
4# Get images (to verify that connection works)
5await client.images()

See Contree for all client options.

1# Get client
2client = ContreeSync(token=token)
3
4# Get images (to verify that connection works)
5client.images()

See ContreeSync for all client options.

Working with Images

Images are the foundation of Contree. You can pull existing images from registries or work with images that are already available in your Contree environment.

Pulling Images

You can pull images in several ways - by UUID, by tag, and from external registries:

 1print(f"Selected {image_uuid=}")
 2print(f"Selected {image_tag=}")
 3
 4print("\nPulling by UUID:")
 5result = await client.images.pull(image_uuid)
 6print(f"Pull by UUID: {result.uuid=}, {result.tag=}, {result.state=}")
 7
 8print("\nPulling by tag:")
 9result = await client.images.pull(image_tag)
10print(f"Pull by tag: {result.uuid=}, {result.tag=}, {result.state=}")
11
12print("\nImporting public image:")
13result = await client.images.pull("docker://ghcr.io/linuxserver/code-server:latest")
14print(f"Import public: {result.uuid=}, {result.tag=}, {result.state=}")

See pull() for all image pulling options.

 1print(f"Selected {image_uuid=}")
 2print(f"Selected {image_tag=}")
 3
 4print("\nPulling by UUID:")
 5result = client.images.pull(image_uuid)
 6print(f"Pull by UUID: {result.uuid=}, {result.tag=}, {result.state=}")
 7
 8print("\nPulling by tag:")
 9result = client.images.pull(image_tag)
10print(f"Pull by tag: {result.uuid=}, {result.tag=}, {result.state=}")
11
12print("\nImporting public image:")
13result = client.images.pull("docker://ghcr.io/linuxserver/code-server:latest")
14print(f"Import public: {result.uuid=}, {result.tag=}, {result.state=}")

See pull() for all image pulling options.

Image Sources

You can pull images from several sources:

  • By tag: "ubuntu:latest" - Pull from your Contree registry

  • By UUID: "550e8400-e29b-41d4-a716-446655440000" - Pull a specific image version

  • Docker Hub: "docker://docker.io/busybox:latest" - Import from Docker Hub

  • Other registries: "docker://ghcr.io/user/image:tag" - Import from other Docker registries

Running Commands

Once you have an image, you can run commands inside it. Each command execution creates a new version of the image with your changes.

Basic Command Execution

You can run various shell commands and handle their output:

 1image = await client.images.pull("busybox:latest")
 2print(f"Pulled {image=}")
 3
 4result = await image.run(shell="echo 'Hello World'")
 5print(f"Simple echo: {result.stdout=}, {result.stderr=}, {result.exit_code=}")
 6
 7result = await image.run(shell="pwd")
 8print(f"Current directory: {result.stdout=}, {result.exit_code=}")
 9
10result = await image.run(shell="ls -la")
11print(f"Directory listing: {result.stdout=}, {result.exit_code=}")
12
13result = await image.run(shell="cat -", stdin="Hello from stdin\n")
14print(f"Cat with stdin: {result.stdout=}, {result.exit_code=}")
15
16result = await image.run(shell="echo 'Error message' >&2; exit 1")
17print(f"Error command: {result.stdout=}, {result.stderr=}, {result.exit_code=}")

See run() for all command execution options.

 1image = client.images.pull("busybox:latest")
 2print(f"Pulled {image=}")
 3
 4result = image.run(shell="echo 'Hello World'").wait()
 5print(f"Simple echo: {result.stdout=}, {result.stderr=}, {result.exit_code=}")
 6
 7result = image.run(shell="pwd").wait()
 8print(f"Current directory: {result.stdout=}, {result.exit_code=}")
 9
10result = image.run(shell="ls -la").wait()
11print(f"Directory listing: {result.stdout=}, {result.exit_code=}")
12
13result = image.run(shell="cat -", stdin="Hello from stdin\n").wait()
14print(f"Cat with stdin: {result.stdout=}, {result.exit_code=}")
15
16result = image.run(shell="echo 'Error message' >&2; exit 1").wait()
17print(f"Error command: {result.stdout=}, {result.stderr=}, {result.exit_code=}")

See run() for all command execution options.

Understanding the Results

When you run a command, you get back a result object that contains:

  • stdout: Standard output from the command

  • stderr: Standard error from the command

  • exit_code: The exit code (0 for success, non-zero for errors)

  • uuid: The UUID of the new image version created by this command