1. Docker must be installed.
2. Docker-compose must be installed.
- Pull “nginx” and “php-fpm” docker images
docker pull nginx:latest
docker pull php:7.1-fpm
(Nginx latest image is recommended and for PHP choose the specific version that your code supports)
Firstly, we will launch temporary docker for nginx
docker run --name nginx_temp -p 82:80 -d nginx:latest docker cp nginx_temp:/etc/nginx/ /etc/nginx/ docker rm -f nginx_temp
We are copying nginx configuration files into our system (afterwards we will use it for mounting) and then we will remove the temporary container
PS : make sure /etc/nginx folder doesn’t exists before firing this command
Now, we can create a docker-compose file
version: '3' services: web: image: nginx:latest container_name: nginx ports: - "80:80" - "443:443" volumes: - /usr/share/nginx/html:/usr/share/nginx/html - /etc/nginx:/etc/nginx/ restart: always php: image: php:7.1-fpm container_name: php ports: - "9000:9000" volumes: - /usr/share/nginx/html:/usr/share/nginx/html - /usr/local/etc/php/conf.d:/usr/local/etc/php/conf.d restart: always depends_on: - web
Above docker-compose file launches 2 containers, one for nginx and one for php.
For Nginx : we have mounted 2 directories
1. /usr/share/nginx/html –> We will put our code in this directory
2. /etc/nginx –> Nginx configuration files will reside in this directory
– We have exposed 80(HTTP) and 443(HTTPS) ports
For PHP : We have mounted 2 directories
1. /usr/share/nginx/html –> We will put our code in this directory (Code should be accessible to both the containers)
2. /usr/local/etc/php –> Initially this folder will be empty, but we’ll mount it from start so if we download any extensions for php and/or we need to change variables in php.ini file, changes persist even after restart of container
–> As, we have used php-fpm image, nginx will call php container on speicifc port, so we have exposed 9000 for the same.
–> Both containers are on restart always mode, so if any restart occurs to your system, it will automatically start both the containers.
Finally, let’s make docker container up and running using compose file
docker-compose up -d
Now, fire following commands and check if containers are in running mode or not.