In this article we will use docker compose to spin up two containers. One will run a web server (Nginx) and one will run php-fpm (Hypertext Preprocessor FastCGI Process Manager). All requests for .php will be forwarded on to the php-fpm service for processing which is running in a separate container and be presented by Nginx. We will use images based off of Alpine which is a minimal Linux environment with a small footprint.

Please go through aws-azure-development-environment-setup-for-containerization prior to commencing with this article.

–>
Change directory in to the dev/docker path located within your home directory:

$ cd ~/dev/docker

Create a directory called myweb and go in to it:

$ mkdir myweb && cd myweb

Create five directories called nginx, nginx/conf, php-fpm, src and src/web:

$ mkdir -p nginx/conf php-fpm -p src/web

Create a docker-compose.yml file like so:

$ cat << 'EOF' > docker-compose.yml
> version: '3'
>
> services:
>     nginx:
>         image: myweb-nginx:v1
>         build: ./nginx
>         volumes:
>             - ./src/web:/var/www/html
>         ports:
>             - 80:80
>         depends_on:
>             - php-fpm
>
>     php-fpm:
>         image: myweb-php-fpm:v1
>         build: ./php-fpm
>         volumes:
>             - ./src/web:/var/www/html
> EOF

This sets the version to 3 for docker-compose and sets up two services called nginx and php-fpm. It will build what is inside each folder, overlay src/web in to them (so you can work independently on the content without rebuilding the container) and map port 80 (HTTP) to the inside HTTP/80 port. Nginx requires php-fpm so you denote that with a depends_on; this service will start first. The ‘image:’ will name and tag this accordingly on the docker images.

Create a Dockerfile inside nginx:

$ cat << 'EOF' > nginx/Dockerfile
> FROM nginx:stable-alpine
> COPY ./conf/default.conf /etc/nginx/conf.d
> EOF

Create a default.conf inside nginx/conf:

$ cat << 'EOF' > nginx/conf/default.conf
> server {
>    index index.php;
>    server_name "";
>    error_log /var/log/nginx/error.log;
>    access_log /var/log/nginx/access.log;
>    root /var/www/html;
>
>    location ~\.php$ {
>        try_files $uri =404;
>        fastcgi_split_path_info ^(.+.php)(/.+)$;
>        fastcgi_pass php-fpm:9000;
>        fastcgi_index index.php;
>        include fastcgi_params;
>        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
>        fastcgi_param PATH_INFO $fastcgi_path_info;
>    }
> }
> EOF

This will pull the stable Nginx docker image based off of Alpine and copy our default.conf in to the container. The default.conf sets up the web server to forward .php over to our container which is called ‘php-fpm’ on the default port of 9000 and sets the default index page as index.php.

Create a Dockerfile inside php-fpm:

$ cat << 'EOF' > php-fpm/Dockerfile
> FROM php:7.3-fpm-alpine
> EOF

This will pull the php-fpm 7.3 docker image based off of Alpine.

Create a index.php in src/web:

$ cat << 'EOF' > src/web/index.php
> <html>
>  <head>
>   <title>Hello World - PHP Test</title>
>  </head>
>  <body>
>   <?php echo '<p>Hello World</p>'; ?>
>  </body>
> </html>
> EOF

Now build both containers/launch both services and send in to non-interactive mode:

$ docker-compose up -d

Now use curl to make a request:

$ curl http://localhost

You should see:

<html>
 <head>
  <title>Hello World - PHP Test</title>
 </head>
 <body>
  <p>Hello World</p>
 </body>
</html>

You can see the containers running by typing:

$ docker ps

You can list the containers:

$ docker container ls

You can stop the containers:

$ docker-compose stop

You can remove the containers:

$ docker-compose down

You can list the images:

$ docker images

<–

References:

Source:
docker_myweb

« »