Docker extend entrypoint script from docker-compose

Viewed 500

I'm very new to Docker.

I dockerized my Laravel application. This is the base image php:8.1.2-apache

At the end of Dockerfile I'm using my own entrypoint script

ENTRYPOINT ["/usr/local/bin/start"]

This script (/usr/local/bin/start) contains few commands like

composer install --no-interaction && 
php artisan config:cache && 
php artisan route:cache && 
php artisan view:cache && 
php artisan storage:link

Now I'm using this Docker Image for many things like laravel scheduler, queue etc...

What I want to do is to extend the entrypoint script from docker-compose file, so that whenever the containers get started the entrypoint script gets executed first then finally the main command which will be passing from docker-compose will be executed.

Something like:

  laravel-scheduler:
    image: laravel
    container_name: laravel-scheduler
    restart: always
    volumes:
      - .:/var/www/html
    command: php artisan schedule:work
2 Answers

First

You can create build_entrypoint.sh

#!/bin/bash

composer install --no-interaction && 
php artisan config:cache && 
php artisan route:cache && 
php artisan view:cache && 
php artisan storage:link

And use it ENTRYPOINT ["./build_entrypoint.sh"] in base Dockerfile

In docker-compose you can override the behavior: in command section start manually /build_entrypoint.sh + extended commands

something like command: /bin/sh -c "./build_entrypoint.sh && ./test_running.sh"

.

Second: pretty

Start a new service in docker-compose with your daemon based on the main php image.

docker/php/Dockerfile

FROM php:8.1-fpm

# ... others commands (setup composer and php-ext(s))

# Attention! We run this command to build our image
RUN composer install --no-interaction && 
  php artisan config:cache && 
  php artisan route:cache && 
  php artisan view:cache && 
  php artisan storage:link

docker-compose.yml

# Main php service
php:
    build:
        context: docker/php    # path to your dockerfile
    volumes:
        - .:/var/www/html

# PHP WORKER service with daemon work
php-worker:
    build:
        context: docker/php    # path to your dockerfile
    volumes:
        - .:/var/www/html
    command: php artisan schedule:work

Simply end the entrypoint script with exec "$@". It will honor the Compose command: in exactly the way you describe.

#!/bin/sh

# Do first-time setup steps that can't be done in the Dockerfile
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan storage:link

# Run the main container command
exec "$@"

Also see Understand how CMD and ENTRYPOINT interact in the Dockerfile documentation: the CMD (or the Compose command: override) gets passed as arguments to your entrypoint script. The exec "$@" invocation is a shell command to replace the current shell with those command-line arguments.

The one other important caveat is that, in the Dockerfile, ENTRYPOINT must be the JSON-array exec form. If it's bare-string shell form, the shell wrapping prevents this from working. The syntax you show in the question is correct.

Related