Hi Geeks,
Here I have dockerised Nginx webserver and php-fpm in different containers and deployed into docker swarm for high performance. The complete infrastructure of this project is explained in the below diagram.
I have used docker compose feature to create services for nginx and php-fpm which will be then deployed from my docker swarm's master node to worker nodes. Also configured a NFS server to server the website contents to my worker nodes. So the developer can update the code from the nfs server.
- Docker Swarm
- Overlay network
- Docker Compose
- Nginx webserver, PHP-FPM
- NFS server
The first thing you have to do is, of course, install Docker (if you haven’t already). The second prerequisite is getting Docker Compose (it is included in the Mac toolbox)
We’ll start by getting ourselves a web server and based on our requirements this will be containers running nginx and php-fpm. These containers will be running on the alpine os to minimalize the disk usage.
Below is my docker-compose.yml file for creating nginx and php-fpm services.
version: '3'
services:
php:
image: php:fpm-alpine3.13
restart: always
container_name: php
volumes:
- /var/nfs:/var/www/html
networks:
- newnet
nginx:
image: nginx:alpine
restart: always
container_name: nginx
volumes:
- /var/nfs:/var/www/html/
- /var/nfs/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
networks:
- newnet
networks:
newnet:
driver: overlay
I have already setup my NFS server and mounted the /var/nfs under all workers nodes. The image for the nginx with web app and php-fpm will be built using the Dockerfile defined above.
Server block configuration for my web-app,
server {
listen 80;
listen [::]:80;
access_log off;
root /var/www/html;
index index.php;
server_name example.com;
server_tokens off;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on wordpress:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000; <<<<<<<<<<<<<<<
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
In the above server block configuration I have used php-fpm's container name to server the php request for the nginx webserver.
When running Docker Engine in swarm mode, you can use "docker stack deploy" to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file.
To avoid creating the container under master node, I'm setting the master node's availability as drain.
docker node update --availability=drain <Manager_IP_Address>
- Set up a Docker registry. A registry is required to distribute images to all of the docker engines. Start the registry as a service on your swarm:
$ docker service create --name registry --publish published=5000,target=5000 registry:2
- Check its status with docker service ls
$ docker service ls
Once it reads 1/1 under REPLICAS, it’s running. If it reads 0/1, it’s probably still pulling the image.
- Test the web-app with docker compose.
$ docker-compose up -d
- Check that the app is running with docker-compose ps:
$ docker-compose ps
- Confirm the webapp is running fine and Bring the app down:
$ docker-compose down --volumes
- Push the generated image to the previously registry,
$ docker-compose push
The stack is now ready to be deployed.
- Create the stack with docker stack deploy:
$ docker stack deploy --compose-file docker-compose.yml webapp-stack
The last argument is a name for the stack. Each network, volume and service name is prefixed with the stack name.
- Check that it’s running with docker stack services webapp-stack:
$ docker stack services webapp-stack
Once it’s running, you should see 1/1 under REPLICAS for both services. This might take some time if you have a multi-node swarm, as images need to be pulled.
I have also setup an AWS Application load balancer by using my worker nodes to manage the traffic into the webapp.
Thank you