I’m trying to run a Laravel app in my local environment via Docker. I want to setup separate containers for each service i.e. Apache, PHP, MySQL. I also want to keep Composer, Artisan and PHPUnit in separate containers as well. This is more for neatness than anything else.
All the containers spin up with no issues and I can access each one no problems via the ‘docker-compose exec [container name] /bin/sh’ command.
The problem I’m having is that the index.php in the public folder is not being executed correctly. Apache is just serving up the file contents.
I can’t figure out what I’m doing wrong. I’ve tried using an Nginx container instead of Apache but I get the same issue. I’m guessing my Apache container does not recognize my PHP container.
Is there anything I’m doing wrong below?
My docker-compose.yml file is as follows:
version: '3.8'
networks:
cpw:
name: cpw_network
services:
apache:
build:
context: .
dockerfile: apache.dockerfile
container_name: cpw_apache
depends_on:
- php
- mysql
ports:
- 8080:80
- 8443:443
volumes:
- ./:/var/www/html
networks:
- cpw
php:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_php
volumes:
- ./:/var/www/html
networks:
- cpw
mysql:
image: mysql:5.7.32
container_name: cpw_mysql
environment:
MYSQL_DATABASE: cpw
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
networks:
- cpw
composer:
image: composer:latest
container_name: cpw_composer
volumes:
- ./:/var/www/html
working_dir: /var/www/html
networks:
- cpw
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_artisan
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: [ "php", "artisan" ]
networks:
- cpw
phpunit:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_phpunit
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: [ "/var/www/html/vendor/bin/phpunit" ]
networks:
- cpw
My apache.dockerfile is as follows:
FROM httpd:alpine
ADD ./apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
RUN sed -i 's,#Include conf/extra/httpd-vhosts.conf,Include conf/extra/httpd-vhosts.conf,g' /usr/local/apache2/conf/httpd.conf
RUN mkdir -p /var/www/html
My php.dockerfile is as follows:
FROM php:7.4.12-fpm-alpine
RUN mkdir -p /var/www/html
RUN apk --no-cache add shadow && usermod -u 1000 www-data
RUN docker-php-ext-install pdo pdo_mysql
My httpd-vhosts.conf is as follows:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/public
ServerName localhost
ServerAlias localhost
ErrorLog logs/localhost-error_log
CustomLog logs/localhost-access_log common
<Directory /var/www/html/public>
AllowOverride All
DirectoryIndex index.php index.php
Options -Indexes
Require all granted
</Directory>
</VirtualHost>
Any help appreciated.
Regards,
Stephen
2
Answers
Thanks for @NicoHaase for pointing me in the right direction.
This is the piece I was missing:
More details here:
How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)
This might also fix: