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