I have a Docker Compose and Python scripts to fill some of its containers with initial data after startup. I need to run those scripts after containers are up. For example:
services:
elasticsearch:
image: elasticsearch:8.6.0
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- "9200:9200"
- "9211:9300"
volumes:
- elasticsearch:/usr/share/elasticsearch/data
And I have insert_elasticsearch_events.py
script to create indexes, insert events etc. My current solution is very heuristic - just add waiting in Makefile:
start:
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 \
docker compose --file docker/docker-compose.yml up --build --detach
# wait for Elasticsearch to go up and then fill it with data
sleep 1
python docker/insert_elasticsearch_events.py
I have seen multiple similar questions, but they all concern running script inside the container, i.e. COMMAND
in Docker or command
in Docker Compose. This is not what I need to do - my scripts are local, and are run outside of Docker Compose.
Is there any nicer way to do this, instead of just waiting a constant amount of time?