skip to Main Content

I have an application, that work with rabbitmq. There is 2 php scripts (send and receive messages) but i can run only one script using Dockerfile

CMD ["php", "./send.php"]

But i have to run two scripts. My tutor ask me to do two containers for each script:

version: "3"

services:

  rabbit_mq:
    image: rabbitmq:3-management-alpine
    container_name: 'rabbitmq'
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - ./docker/rabbitmq/data/:/var/lib/rabbitmq/
      - ./docker/rabbitmq/log/:/var/log/rabbitmq
      - ./docker/rabbitmq/conf/:/var/conf/rabbitmq
    environment:
      - API_URL=Api:8000

  send:
    build:
      context: './docker'
      dockerfile: Dockerfile
    image: php:7.4.cli
    container_name: send
    ports:
      - 8000:8000
    volumes:
      - ./:/app
    depends_on:
      - rabbit_mq

  receive:
    image: php:7.4.cli
#    build:
#      context: './docker'
#      dockerfile: Dockerfile
    container_name: receive
    ports:
      - 8001:8001
    volumes:
      - ./:/app
    depends_on:
      - rabbit_mq

What can I do to run 2 scripts using "docker-compose up" command? I serf a lot of web-pages, but couldn’t find anything, I really need your help!

2

Answers


  1. you did not specify if those scripts terminate process or not, but to run them, you can make docker-compose like this:

    version: "3"
    
    services:
      rabbit_mq:
        # existing configuration
    
      send:
        # existing configuration
        command: ["php", "./send.php"]
    
      receive:
        # existing configuration
        command: ["php", "./receive.php"]
    
    Login or Signup to reply.
  2. If you would like to run it as part of the docker-compose you can add these lines to certain container blocks in the composer file:

    command: php send.php
    command: php receive.php
    

    https://docs.docker.com/compose/compose-file/#command

    If you need more complicated things like restarting on failure, take a look at using `supervisor’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search