Jina with Docker Compose

Jina natively supports deploying your Flow and Executors locally with docker-compose.

Preliminaries

Please first make sure Docker Compose is installed locally.

Deploy your Flow

To deploy a Flow with Docker Compose, first, you need to generate a yaml file with all the Executors’ services descriptions. Then, you can use the docker-compose up -f <file.yml> command to start all the services and start accepting requests.

Caution

All Executors in the Flow should be used with jinahub+docker://... or docker://....

To generate YAML configurations for Docker Compose from a Jina Flow, one just needs to call:

  1. flow.to_docker_compose_yaml('docker-compose.yml')

This will create a file ‘docker-compose.yml’ with all the services neeeded to compose and serve the Flow.

Examples

Indexing and searching images using CLIP image encoder and PQLiteIndexer

Caution

Before starting this example, make sure that CLIPImageEncoder and PQLiteIndexer images are already pulled to your local machine.

You can use:

jina hub pull jinahub+docker://CLIPImageEncoder jina hub pull jinahub+docker://PQLiteIndexer

This example shows how to build and deploy a Flow with Docker Compose with CLIPImageEncoder as encoder and PQLiteIndexer as indexer.

  1. from jina import Flow
  2. f = Flow(port_expose=8080, protocol='http').add(
  3. name='encoder', uses='jinahub+docker://CLIPImageEncoder', replicas=2
  4. ).add(name='indexer', uses='jinahub+docker://PQLiteIndexer', uses_with={'dim': 512}, shards=2)

Now, we can generate Docker Compose YAML configuration from the Flow:

  1. f.to_docker_compose_yaml('docker-compose.yml')

As you can see, the Flow contains services for the gateway and the rest of executors.

Now, you can deploy this Flow to you cluster in the following way:

  1. docker-compose up -f docker-compose.yml

Once we see that all the Services in the Flow are ready, we can start sending index and search requests.

  1. from jina.clients import Client
  2. from jina import DocumentArray
  3. client = Client(host='localhost', port=8080)
  4. client.show_progress = True
  5. indexing_documents = DocumentArray.from_files('./imgs/*.jpg').apply(lambda d: d.load_uri_to_image_blob())
  6. docs = client.post(
  7. '/index', inputs=indexing_documents, return_results=True
  8. )
  9. print(f'Indexed documents: {len(docs)}')
  10. query_doc = indexing_documents[0]
  11. query_responses = client.post(
  12. '/search', inputs=query_doc, return_results=True
  13. )
  14. matches = query_responses[0].data.docs[0].matches
  15. print(f'Matched documents: {len(matches)}')