skip to Main Content

i want to start a service after a specific script/container is ended.
In particular i want the "app" container start and it will do his staff , this container will execute a python script. After that service finish,i want that another container it will start and his name is "Benthos".
In particular i want the benthos service start when the execution of the python script is ended.
Docker comopose

services:
  app:
    build: 
      context: .
      dockerfile: ./Dockerfile.python
    volumes:
      - ./src:/app/
    container_name: python-container
    depends_on:
      database:
        condition: service_healthy

  benthos:
    build:
     context: .
     dockerfile: ./Dockerfile.benthos  
    ports:
      - 8010:4195
    volumes:
      - ./src:/src/
    container_name: benthos-container
    stdin_open: true
    tty: true
    depends_on:
      database:
        condition: service_healthy

  database:
    image: cassandra:latest
    ports:
      - 9042:9042
    container_name: cassandra-container
    healthcheck:
      test: ["CMD-SHELL", "cqlsh -u cassandra -p cassandra -e 'DESCRIBE KEYSPACES'"]
      interval: 30s
      timeout: 10s
      retries: 5

i would add to the benthos service another

depends_on:
  app:
    condition: srvice_terminate

the service app , is a simple python service, that execute a script and this it is:
db.py

from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement

def create_keyspace_and_table(session):
    keyspace_query = """
    CREATE KEYSPACE IF NOT EXISTS store WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
    """
    session.execute(keyspace_query)

    session.set_keyspace('store')

    create_table = """
    CREATE TABLE IF NOT EXISTS store.mytable (
        id UUID PRIMARY KEY,
        message TEXT
    )
    """
    session.execute(create_table)

def insert_data(session, message):
    insert_query = """
    INSERT INTO store.mytable (id, message) VALUES (uuid(), %s)
    """
    session.execute(insert_query, (message,))

def print_data(session):
    select_query = "SELECT * FROM store.mytable"
    rows = session.execute(select_query)

    for row in rows:
        print(f"ID: {row.id}, Message: {row.message}")

def delete_all(session):
    delete_query = "TRUNCATE store.mytable"
    session.execute(delete_query)

if __name__ == "__main__":
    
    cluster = Cluster(['database']) 
    session = cluster.connect()

    try:
        create_keyspace_and_table(session)

        data = [
            ("Ciao sono il primo"),
            ("Ciao sono il secondo"),
            ("Ciao sono il terzo")
        ]

        print("insert values")
        for message in data:
            insert_data(session, message)

        print_data(session)
        
    finally:
        print("finish")

i want to start the other service when the execution of the python script pass the print_data(sesion)

i don’t found if docker implement a costructor that detect if a service is ended

Looking on internet, documentation

2

Answers


  1. Chosen as BEST ANSWER

    i add this in the section fo depends_on

    benthos:
            condition: service_completed_successfully
    
    

    it attend the benthos service is terminate


  2. A typical pattern for this sort of setup is an entrypoint wrapper script. You can write a short shell script that does any required first-time setup, in your case running the migration script, and then switches over to the main container command

    #!/bin/sh
    
    ./seed_database.py || exit 1
    
    exec "$@"
    

    You’ll only have a single container, a single image, and a single Dockerfile. Set this wrapper script as the image’s ENTRYPOINT, and leave your existing CMD unchanged. (If you currently have the main container command as the ENTRYPOINT, change it to CMD.)

    COPY seed_database.py entrypoint.sh ./
    ENTRYPOINT ["./entrypoint.sh"]
    CMD ["benthos", "..."]
    

    Since your seed script is in Python but it looks like the Benthos core is in Go, you may need to install a Python runtime to be able to run this at all.

    In the Compose file, then, you can get rid of the short-lived seed job and have only the single container plus its data store.

    version: '3.8'
    services:
      benthos:
        build: .
        ports:
          - 8010:4195
        depends_on:
          database:
            condition: service_healthy
      database:
        image: cassandra:latest
        # and the rest of this service from the original question
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search