0

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?

qalis
  • 1,314
  • 1
  • 16
  • 44
  • The solutions in [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) that don't involve Docker health checks will work here too. `curl http://localhost:9200/` in a loop until it returns success. – David Maze Feb 01 '23 at 12:13

0 Answers0