Docker: A primer for PHP Developers

               In section 8.13 ("Configuring PHP-FPM") and section 9.13 ("Docker Logs"), we used the CMD instruction in our Dockerfile                to specify which default command should run when a container is started from an image.

               It is possible to use CMD to set parameters used by images when running a specific container.

               The ENTRYPOINT instruction should be used when you want the container to always use the same base command                AND/OR (!) the user should be allowed to append additional commands at the end of the command line.

               In our case, we create the start-container.sh Bash script below.

               With this script, the user must enter any command. If he/she doesn't, supervisord is started by default.

               We add start-container.sh to our Dockerfile below as our ENTRYPOINT script.

               The image is then rebuilt.

start-container.sh
#!/usr/bin/env bash

##
# Ensure /.composer exists and is writable
#
if [ ! -d /.composer ]; then
    mkdir /.composer
fi

chmod -R ugo+rw /.composer

##
# Run a command or start supervisord
#
if [ $# -gt 0 ];then
    # If we passed a command, run it
    exec "$@"
else
    # Otherwise start supervisord
    /usr/bin/supervisord
fi

Dockerfile
FROM ubuntu:18.04

LABEL maintainer="Kâmi Barut-Wanayo"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y gnupg tzdata \
    && echo "UTC" > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata

RUN apt-get update \
    && apt-get install -y curl zip unzip git supervisor sqlite3 \
       nginx php7.2-fpm php7.2-cli \
       php7.2-pgsql php7.2-sqlite3 php7.2-gd \
       php7.2-curl php7.2-memcached \
       php7.2-imap php7.2-mysql php7.2-mbstring \
       php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \
       php7.2-intl php7.2-readline php7.2-xdebug \
       php-msgpack php-igbinary \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && echo "daemon off;" >> /etc/nginx/nginx.conf

RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

ADD default /etc/nginx/sites-available/default
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ADD php-fpm.conf /etc/php/7.2/fpm/php-fpm.conf
ADD start-container.sh /usr/bin/start-container
RUN chmod +x /usr/bin/start-container

ENTRYPOINT ["start-container"]

                 Rebuilding our container image using the same command we used in previous sections:

$ docker build -t  kbarut/app:latest -f docker/app/Dockerfile docker/app
							

Kâmi Barut-Wanayo © 2024 - 2025