Home > Software design >  Make container X to wait n seconds untill localstack queues are created
Make container X to wait n seconds untill localstack queues are created

Time:09-07

I'm trying to create a docker-compose file to run localstack (for sqs), 1 mysql databases and 2 services together. The problem I'm trying to deal with is that the services starts to build and run before the queues are created (which I don't want).

Is there a way to make the services sleep ? I've tried to use health check but it didn't make a difference.

Here's how the file looks:

version: "3.8"
services:

localstack:
        container_name: "DGT-localstack_main"
        image: localstack/localstack
        ports:
          - "4566:4566"            # LocalStack Gateway
          - "4510-4559:4510-4559"  # external services port range
          - "53:53"                # DNS config (only required for Pro)
          - "53:53/udp"            # DNS config (only required for Pro)
          - "443:443"              # LocalStack HTTPS Gateway (only required for Pro)
        environment:
          - DEBUG=${DEBUG-}
          - PERSISTENCE=${PERSISTENCE-}
          - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
          - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}  # only required for Pro
          - DOCKER_HOST=unix:///var/run/docker.sock
        volumes:
          - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
          - "/var/run/docker.sock:/var/run/docker.sock"
        networks:
          - localstack_network

awslocal_cli:
        image: amazon/aws-cli
        depends_on: 
          - localstack
        entrypoint: /bin/sh -c
        networks:
          - localstack_network
        command: >  
          '
            echo "########### Creating profile ###########"
    
            aws configure set aws_access_key_id ignore
            aws configure set aws_secret_access_key ignore
            aws configure set region eu-north-1
    
            echo "########### Creating SQS ###########"
            aws sqs create-queue --endpoint-url=http://localstack:4566 --queue-name=FIRST_QUEUE
            aws sqs create-queue --endpoint-url=http://localstack:4566 --queue-name=SECOND_QUEUE
            
    
            echo "########### Listing SQS ###########"
            aws sqs list-queues --endpoint-url=http://localstack:4566
           '

db:
        container_name: db
        image: mysql:8.0.28
        command: --lower_case_table_names=1
        ports:
          - "3308:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=maindb
        volumes:
        - ./db_config/core/data:/var/lib/mysql
        networks:
          - localhost_network

api:
    container_name: Api
    image: api:1.0
    build:
      context: blabla
      dockerfile: blabla
    ports:
      - blabla
    env_file: ./Server/common.env
    environment:
      - blabla
    restart: on-failure 
    depends_on:
      - core
    networks:
      - localhost_network

core:
        container_name: Core
        image: core:1.0
        build:
          context: blabla
          dockerfile: blabla
        ports:
          - "5115:80"
        env_file: .blabla
        environment:
          - blabla
        restart: on-failure 
        depends_on:
          - localstack
          - awslocal_cli
          - db
        networks:
          - localstack_network
          - localhost_network

networks:
      localstack_network:
      localhost_network:

I'm sorry for the bad indentation (they are right indented in my file)

CodePudding user response:

Use the depends_on function.
If you want container B to start after container A you would need to create you docker-compose like this

A:
    image: ...
    other_settings: ...
  
B:
    image: ...
    other_settings: ...
    depends_on: A

This would ensure that container B is only started if container A is already up and running.

If there is a URL to check if the queue is already created you could also try this:
https://docs.docker.com/compose/startup-order/

CodePudding user response:

You can try to wrap starting command in script wait-for-it.sh. Complete example is hire: https://docs.docker.com/compose/startup-order/

  • Related