Skip to content

How to containerize a php cli application🔗

Dockerfile🔗

Start from an existing php image:

1
FROM php:7.4-cli

Install required packages (optional):

1
2
3
RUN apt-get update && apt-get install --yes --no-install-recommends \
    package-1 \
    package-2

Install or enable an already installed php extension (optional):

1
2
RUN docker-php-ext-install ext-1 ext-2
RUN docker-php-ext-enable ext-3 ext-4

Or with PECL:

1
RUN pecl install ext-1 && docker-php-ext-enable ext-1

Install the php command you want, could be downloading a phar for exemple:

1
RUN wget https://example.com/download/command.phar -O /path/to/command

Define endpoints:

1
ENTRYPOINT ["php", "/path/to/command"]

More on Docker Official Images documentation.

Build the container🔗

1
docker build -t your_container_name .

Usage🔗

1
docker run --rm -it your_container_name [ARGUMENTS]

If you need to access to the host filesystem, you will need to mount volumes:

1
docker run --rm -it -v "$PWD:/path/to/workdir" -w "/path/to/workdir" your_container_name [ARGUMENTS]

Last update: December 20, 2024