Configuring a Docker PHP image

This post is mostly intended for docker beginners. Now, Getting straight to the point. In most use cases PHP is never us as a standalone container but is always used along with any/all elementals of the LAMP or LEMP stack.

Docker has its own mechanism known as docker-compose that is very helpful in running multiple containers as a single stack. All the services that make up your app/stack have to be placed in docker-compose.yml along with other required configurations like Environment variable, synced files, container names, ports. The next important file related to configs is the Dockerfile, where you can run almost any linux command to build your custom image.

With this 2 points taken care of, let’s go to selecting the PHP image, For the sake of this post lets stick to the below two.

For this Tutorial we will use the Official PHP image.

First add configurations to the docker-compose.yml file, full example here.

 services:
  php:
   build:
      context: './php/'
      args:
       PHP_VERSION: 7.2
    container_name: "test_php"
    networks:
      - backend
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      DB_HOST: $DB_HOST
      DB_USER: $DB_USER
      DB_PASSWORD: $DB_PASSWORD
      DB_NAME: $DB_NAME
      DB_DRIVER: $DB_DRIVER
    volumes:
        - ./src/:/usr/local/apache2/htdocs/ #set the docroot folder
        - ./php/php.ini:/usr/local/etc/php/php.ini #set custom php.ini file

Here, “context: ‘./php/’ “ is the location/folder of the PHP specific config files that we will discuss later. The next important thing is the volumes section, here we can set the docroot folder, and the customized php.ini file.

The section before the “:” is the absolute path the local resource and the part after the colon is the absolute path inside the docker image. The next question would be how do we get the php.ini file from the docker image ? there is a docker command that can be used for this:

docker cp docker-stack_php:/usr/local/etc/php/php.ini php.ini

Next is the Dockerfile, example below, full example here.

FROM php:7.2

#======Install Linux Dependencies
RUN apt-get install libjpeg-dev -y
RUN apt-get install libpng-dev -y

#======PHP Extentions
RUN docker-php-ext-install gd
RUN docker-php-ext-install zip

#======Install Composer
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

#======Install Composer packages like Drush
RUN composer global require drush/drush:8.x
RUN ln -s /root/.composer/vendor/drush/drush/drush /usr/bin/drush

#======Copy the php.ini file
#COPY  "php.ini" "$PHP_INI_DIR/php.ini"

“FROM” defines the target image, The “RUN” keyword can be used to run almost any linux command, The official docker image for php has a command “docker-php-ext-install” to install common php extensions, more details can be found here https://hub.docker.com/_/php. As can be see the Dockerfile is intended to house all your useful commands that can help build your custom docker image. As can be seen docker image are pretty straight forward to configure if you have some experience with using linux commands. Thats it thanks for reading.

LAMP stack with Docker

This post is a simple Tutorial on how to setup a LAMP stack using docker-compose.

If you have already installed docker & docker-compose, you can ignore the below step.

$ sudo apt-get update $ sudo apt-get install docker-ce

else follow instructions here

This LEMP stack uses: NGINX latest alpine MYSQL 5.6.40 PHP 7.1-fpm-alpine mailhog (local mailing service) adminer (database admin) traefik (load balancer) portainer (to debug docker)

We are using alpine image for this tutorial as they are small & compact in size. Alternatively you can also use PHP 5.6-fpm-alpine image.

Assuming that you have cloned this repository. Now navigate to the repository directory, and run below listed commands.

$ sudo docker build .

On first executing the build command it will down load all the specified docker images. Then it will execute commands specified in the Dockerfile, for each image.

$ sudo docker-compose up -d

The docker-compose up command will start all the containers mentioned in the docker compose file in the detached mode, hence the -d. If all goes well, all the containers should be running now. You can verify this usin the below command.

$ sudo docker-compose ps

This will display status of all the container either in up OR exit state.

$ sudo docker exec -it lampdock_nginx /bin/sh

Above command can be used to attach to a running container (like a local bash). Alternatively you can change container name, to connect to other running containers.

//ToDo : add folder structure.

Further Reading & References links:

Design a site like this with WordPress.com
Get started