B
B
Bone2022-03-03 11:33:40
PHP
Bone, 2022-03-03 11:33:40

How to reuse a service in docker-compose?

Hello! I have snippet like this in docker-compose

php:
        build:
            context: "./docker/php/"
            args:
                TZ: ${TIMEZONE}
                UID: ${UID}
                GID: ${GID}
                DEBUG: ${APP_DEBUG}
        restart: unless-stopped
        volumes:
            - .:/var/www/html
As it is easy to understand, php is being built here. But I want another php service to run the worker, which will process tasks from the queue. Right now I just copy the whole php snippet and add entrypoint and command.
php-worker:
        build:
            context: "./docker/php/"
            args:
                TZ: ${TIMEZONE}
                UID: ${UID}
                GID: ${GID}
                DEBUG: ${APP_DEBUG}
        restart: unless-stopped
        volumes:
            - .:/var/www/html
        entrypoint: ["php"]
        command: ["/var/www/html/artisan", "queue:work", "--queue=es"]
I suspect that I am doing something wrong and this duplication of code can somehow be avoided. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zlatoslav Desyatnikov, 2022-03-03
@Bone

Good afternoon!
It is possible like this:

version: '3.9'

x-app:
  &php
  restart: always
  working_dir: /app
  environment:
    ...
  volumes:
    ...

app:
    <<: *php
    image: php8_fpm
    build:
      context: ./docker/fpm
      target: php8_fpm
      dockerfile: Dockerfile

queues:
    <<: *php
    image: php8_fpm
    command: php artisan horizon
    depends_on:
      - app

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question