Home > Software design >  Docker extend entrypoint script from docker-compose
Docker extend entrypoint script from docker-compose

Time:05-16

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

CodePudding user response:

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"

CodePudding user response:

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