How to create a docker-compose production build? Why do I need to copy both nginx and php files?

I have a simple Laravel application with Nginx, PHP and MySQL each in its own container. What I don’t understand is:

  1. why do I need to mount volumes for both my Nginx and also PHP? Isn’t PHP only a programming language not a server?

  2. Also, for production do I need to COPY my src to /var/www/html but do I need to do it for both Nginx and PHP? Or only for Nginx?

How can I COPY . /var/www/html for both Nginx and PHP? Why do I need both?

Here is my docker-compose-dev.yml file

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    
    container_name: nginxcontainer
    
    ports:
      - "8088:80"

    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf

    depends_on:
      - php
      - mysql

    networks:
      - laravel

  mysql:
    image: mysql:5.7.22

    container_name: mysqlcontainer

    restart: unless-stopped

    tty: true

    ports:
      - "4306:3306"

    volumes:
      - ./mysql:/var/lib/mysql

    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql

    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: php/Dockerfile-dev
    
    container_name: phpcontainer

    volumes:
      - ./src:/var/www/html

    ports:
      - "9000:9000"

    networks:
      - laravel

and here is my php/Docker-dev file

FROM php:7.2-fpm-alpine

RUN docker-php-ext-install pdo pdo_mysql
RUN chown -R www-data:www-data /var/www
RUN chmod 755 /var/www

PHP is a language and a server process when used as fpm.
The PHP server handle all the dynamic stuff for nginx. While the nginx container needs to have access to the statics assets (javascript, images and css mostly) of your apps to serve them accordingly since fpm dont serve the statics assets (at least by default). So your application have to be COPYed to the PHP and the nginx container. You could be more selective in your copy (only the php files in the php containers and all the static stuff for the nginx container). But most of the time we copy the whole thing in both containers

1 Like

@sebt3 Thank you for that explanation, I think I understand it now!