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
And use it
ENTRYPOINT ["./build_entrypoint.sh"]
in base DockerfileIn docker-compose you can override the behavior: in command section start manually
/build_entrypoint.sh
+ extended commandssomething 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
docker-compose.yml
Simply end the entrypoint script with
exec "$@"
. It will honor the Composecommand:
in exactly the way you describe.Also see Understand how CMD and ENTRYPOINT interact in the Dockerfile documentation: the
CMD
(or the Composecommand:
override) gets passed as arguments to your entrypoint script. Theexec "$@"
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.