Reuse custom PHP Docker image for job queue in Docker compose

Reuse custom PHP Docker image for job queue in Docker compose

Depending on your setup, most likely you want to run the job queue container with the same image as the php container. Most time I see people executing the running php container and starting a job queue directly in there.

A nicer way is to simply run a copy of the image as a separate container. Instead of running the same build you can use the previously build php image directly in your docker compose configuration.

version: '3.7'

services:
  app:
    build: ./.docker/php
    image: app
    volumes:
      - ./:/var/www/html:rw,cached
    env_file:
      - .env
  job-queue:
    image: app
    volumes:
      - ./:/var/www/html:rw,cached
    env_file:
      - .env
    deploy:
      restart_policy:
        condition: on-failure
    command: php bin/console messenger:consume async -vv

The trick is using the image keyword additionally to the build. With using the image, you name the image and make it available to the other services as an image to use.