I cannot find any answer to my question and I can’t make the entrypoint run when using docker compose exec
. I’m using a custom entrypoint in my docker-compose.yml
:
version: "3.8"
services:
php:
image: php:8-fpm
entrypoint: ./docker-php-entrypoint.sh
command: ["php-fpm"]
The ./docker-php-entrypoint.sh
is executable and it echo something:
#!/bin/sh
set -e
echo "Hello World!"
exec "$@"
Since docker compose exec
executes commands for a running service:
docker compose up -d
docker compose exec php ls
The echo "Hello World!" is visible when the container starts, but not when executing exec php ls
.
Is supposed to work this way? In essence, can the entrypoint be used to run arbitrarty script before any command is invoked in the container?
2
Answers
That’s not how the
exec
command works. It allows you to run a command in an already running container. It would make no sense to execute the entrypoint as part of that, as it might run a long running process or similar, and never return, or exit, an then never actually run the command you wanted to.Hope that makes sense.
You’ve described the "entrypoint wrapper" pattern: in fact, you can use an entrypoint script to do any setup you need to do, and then the
exec "$@"
will run the main container command.A container normally only runs a single command, but conversely, a container isn’t especially expensive. If you
it will create a second container and launch its entrypoint, passing the alternate command
ls
as the command arguments to it. This can be a good way to debug an image, especially if it doesn’t start.You shouldn’t frequently need
docker{,-compose} exec
. If you do use it, as you note, it doesn’t run the container entrypoint a second time, but instead just runs the command you give it. The other important note this pattern is that theexec
environment does not inherit from the entrypoint process at all. For example, if your entrypoint script sets up environment variables from a dynamic source,docker-compose exec php env
won’t show those variables.Since the entrypoint is just a script (or another program) if you do need to use
exec
you can explicitly specify it, if you actually need to run it.