Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Last active June 12, 2018 05:47
Show Gist options
  • Save pdonorio/65617634764d81229cba3c215903f32b to your computer and use it in GitHub Desktop.
Save pdonorio/65617634764d81229cba3c215903f32b to your computer and use it in GitHub Desktop.
The power of docker compose

How easy is to quick test and prototype

Nowadays docker compose is getting life so much easy for developers.

Let's say I want to quickly test some python code on elasticsearch... I just need to have a compose file like the one below, and then from the same directory:

docker-compose up -d
docker-compose exec py ash

$ pip install --upgrade ipython elasticsearch
[...]

$ ipython

In [1]: from elasticsearch import Elasticsearch

In [2]: el = Elasticsearch([{'host': 'el'}]) # connect to the other container

In [3]: el.index(index='myindex', doc_type='doc', body={'test': 'hello one'})
Out[3]:
{'_index': 'myindex',
 '_type': 'doc',
 '_id': 'UE978mMBWF94z-2fd4x_',
 '_version': 1,
 'result': 'created',
 '_shards': {'total': 2, 'successful': 1, 'failed': 0},
 '_seq_no': 0,
 '_primary_term': 1}

In [4]: el.index(index='myindex', doc_type='doc', body={'test': 'hello two'})
Out[4]:
{'_index': 'myindex',
 '_type': 'doc',
 '_id': 'UU978mMBWF94z-2fhIy0',
 '_version': 1,
 'result': 'created',
 '_shards': {'total': 2, 'successful': 1, 'failed': 0},
 '_seq_no': 0,
 '_primary_term': 1}

In [5]: el.search(index='myindex', size=10000, body={"query": {'match_all' : {}}
   ...: })
Out[5]:
{'took': 103,
 'timed_out': False,
 '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
 'hits': {'total': 2,
  'max_score': 1.0,
  'hits': [{'_index': 'myindex',
    '_type': 'doc',
    '_id': 'UE978mMBWF94z-2fd4x_',
    '_score': 1.0,
    '_source': {'test': 'hello one'}},
   {'_index': 'myindex',
    '_type': 'doc',
    '_id': 'UU978mMBWF94z-2fhIy0',
    '_score': 1.0,
    '_source': {'test': 'hello two'}}]}}

And finally to clean up correctly the resources:

# 1. stop containers
# 2. remove containers
# 3. remove networks from compose
# 4. remove volumes from compose
docker-compose down -v
version: '3'
services:
 el:
  image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
 py:
  image: python:3.6.5-alpine3.6
  command: sleep 1234567890
# ## uncomment here to have your current directory code accessible inside the python container
# volumes:
# - .:/tmp/code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment