I am trying to use docker-compose to load a php application which boot with a .htaccess.
If i use a proxypassmatch it works, but only for one route, as soon as the url change, the file is unfound (i read that proxypass/reverseproxy/proxypassmatch) can't be use with .htaccess ...?
If i use the traditional setting, with a virtualhost, the .htaccess is working but the php file do not load, unstead its content appear on the screen (whatever the route).
I have spent so many hours looking everywhere without any answer... Or maybe changing the image for a single container php-httpd ? but i like the idea to separate them.
If someone know how to fix it, it would be great thank you.
See the docker-compose file
version: "3.2"
services:
php:
build: './php/'
volumes:
- ./MediterPourGrandir/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
- mysql
ports:
- "80:80"
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
#restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
the apache Dockerfile
FROM httpd:2.4.33-alpine
RUN apk update; \
apk upgrade;
# Copy apache vhost file to proxy php requests to php-fpm container
RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf && \
sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /usr/local/apache2/conf/httpd.conf
COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
>> /usr/local/apache2/conf/httpd.conf
the apache config file
serverName localhost
<VirtualHost *:80>
DocumentRoot /var/www/html/
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Send apache logs to stdout and stderr
CustomLog /proc/self/fd/1 common
ErrorLog /proc/self/fd/2
</VirtualHost>
and the php Dockerfile
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; \
apk upgrade;
RUN apk update --no-cache \
&& apk add --no-cache $PHPIZE_DEPS \
&& apk add --no-cache mysql-dev \
&& docker-php-ext-install pdo pdo_mysql
I finally change the image (i liked not but it looks like nobody knows how to do with this one, even on Docker forum... ), i use php:7.2.1-apache then it works great. See the config of the docker file: running-virtual-hosts-in-apache-docker-container
I connect it to the mysql's image (like it was previously) using docker-compose.
version: "3.2"
services:
php-apache:
build:
context: ./apache-php
ports:
- 80:80
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
#restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
#volumes:
#- ./var/www/html/'monsupersite (1).sql':/docker-entrypoint-initdb.d/'monsupersite (1).sql'
Related
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#email.com
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
Thanks for #NicoHaase for pointing me in the right direction.
This is the piece I was missing:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php-fpm-container:9000"
</FilesMatch>
More details here:
How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)
This might also fix:
<FilesMatch \.(?i:php)$>
SetHandler application/x-httpd-php
</FilesMatch>
I'm new to Docker. I'm trying to change Document Root for my app.
my-app directory has my php codes.
Tree
Project
- docker-compose.yml
- html
- my-app
- php
- 000-default.conf
- Dockerfile
docker-compose.yml
version: "3"
services:
php:
build: ./php/
# image: php:7.2-apache
volumes:
- ./html:/var/www/html
ports:
- 8080:80
container_name: php7.2
restart: always
Dockerfile
FROM php:7.2-apache
RUN apt-get -y update && apt-get -y install vim
COPY 000-default.conf /etc/apache2/sites-available/
000-default.conf
<VirtualHost *:8080>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/my-app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
EnableSendfile off
</VirtualHost>
I command docker-compose up -d --build, however document root never change to my-app where I expect. I confirmed 000-default.conf has changed by COPY inside of container.
Please give me same advice.
Thanks.
You appear to have your ports back-to-front. In your docker-compose.yml file, you've set host port 8080 and container port 80 (👍) but your v-host is listening on container port 8080.
I'm not actually seeing any reason for you to override the default site config nor for you to have a Dockerfile at all. The default site serves content from /var/www/html over container port 80 so you don't need to change that.
Try this config instead
version: "3"
services:
php:
image: php:7.2-apache
volumes:
- "./html:/var/www/html"
ports:
- "8080:80"
Run the stack using
docker-compose up -d
then open http://localhost:8080/my-app/ in your browser.
I try to set up Apache2 and PHP-FPM via unix socket but result is
(111)Connection refused: AH02454: FCGI: attempt to connect to Unix domain socket /run/php/php7.2-fpm.sock (*) failed
docker-compose.yml
version: "2"
services:
php:
build: "php:7.2-rc-alpine"
container_name: "php"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
apache2:
build: "httpd:2.4-alpine"
container_name: "apache2"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
ports:
- 80:80
links:
- php
www.conf
listen = /run/php/php7.2-fpm.sock
httpd-vhosts.conf
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
But it's work when connect via TCP.
www.conf
listen = 127.0.0.1:9000
httpd-vhosts.conf
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
Okie, so have the repo helped to fix the issue.
Issue #1 - www.conf being copied in apache container
You had below statement in your apache container Dockerfile
COPY ./www.conf /usr/local/etc/php-fpm.d/www.conf
This is actually intended for the php container which will be running php-fpm and not the apache container
Issue #2 - Socket was never being created
Your volume bind - "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock" was creating the socket and they were not being created by php-fpm as such. So you created a blank file and trying to connect to it won't do anything
Issue #3 - No config in php to create socket
The docker container by default create listen to 0.0.0.0:9000 inside the fpm container. You needed to override the zz-docker.conf file inside the container to fix the issue.
zz-docker.conf
[global]
daemonize = no
[www]
listen = /run/php/php7.2-fpm.sock
listen.mode = 0666
Updated docker file
FROM php:7.2-rc-fpm-alpine
LABEL maintainer="Eakkapat Pattarathamrong (overbid#gmail.com)"
RUN docker-php-ext-install \
sockets
RUN set -x \
&& deluser www-data \
&& addgroup -g 500 -S www-data \
&& adduser -u 500 -D -S -G www-data www-data
COPY php-fpm.d /usr/local/etc/php-fpm.d/
Issue #4 - Sockets being shared as volumes to host
You should be sharing sockets using a named volume, so the socket should not be on host at all.
Updated docker-compose.yml
version: "2"
services:
php:
build: "./php"
container_name: "php"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "phpsocket:/run/php"
apache2:
build: "./apache2"
container_name: "apache2"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "phpsocket:/run/php"
ports:
- 7080:80
links:
- php
volumes:
phpsocket:
After fixing all the issues I was able to get the php page working
I have such simple dockerfile for PHP:
# Base image
FROM php:7-fpm
# Update packages list
RUN apt-get --yes update;
# Install SSH server, set root password and allow root login
RUN apt-get --yes install openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Run SSH server
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
And such docker-compose.yml file
web:
image: nginx:latest
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/conf.d/:/etc/nginx/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/log/:/var/log/nginx/
ports:
- "8080:80"
working_dir: /usr/share/nginx/html/
links:
- php
- db
container_name: l1.web
environment:
- VIRTUAL_HOST=l1.app
php:
build: ../builds
dockerfile: Dockerfile-php7-fpm
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/php/config/:/usr/local/etc/php/
working_dir: /usr/share/nginx/html/
links:
- db
container_name: l1.php
ports:
- "22020:22"
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_DATABASE=
- MYSQL_USER=
- MYSQL_PASSWORD=
expose:
- 3306
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/mysql/data/:/var/lib/mysql/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/conf.d/:/etc/mysql/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/log/:/var/log/mysql/
ports:
- "33060:3306"
container_name: l1.db
The problem - everything is working fine until I add in my dockerfile the last shown line:
CMD ["/usr/sbin/sshd", "-D"]
If I add this line, SSH is working fine but I'm getting 404 when displaying the page. When I comment this line, I'm getting page without a problem but obviously this SSH is not working.
What could be the problem with this? I just want to add I need this SSH service in PHP container (and running docker exec in this case is not an option)
The base image php-fpm ends with
CMD ["php-fpm"]
Your own CMD would override that (meaning php .
One workaround would be at least to ADD and call a wrapper script which would:
call php-fpm
launch sshd daemon
But that wouldn't play well with stop/kill signals, which would not stop everything. There are special images for managing more than one main process.
The OP Marcin Nabiałek confirms in the comments below:
I've created such file:
#!/bin/sh
# Start PHP
php-fpm -D
# Start SSH
/usr/sbin/sshd -D
and it seems to be working now without a problem.
A complete answer from #VonC and #Marcin Nabialek.
# php-fpm -D
# /usr/sbin/sshd -D
# use \n to make the content into multiple lines
RUN printf "php-fpm -D\n/usr/sbin/sshd -D" >> /start.sh
RUN chmod +x /start.sh
CMD ["/start.sh"]
I am trying to dockerize Laravel 5.2 app. For this I am using following images,
php:apache
mysql:5.7
Below is my docker-compose.yml
web:
build: .
volumes:
- ./:/var/www/html
ports:
- "9899:80"
links:
- db
command : [/usr/sbin/apache2ctl, -D, FOREGROUND]
db:
image: mysql:5.7
volumes:
- /home/data:/var/lib/mysql
environment:
MYSQL_DATABASE: custom
MYSQL_ROOT_PASSWORD: custom
And my Dockerfile
FROM php:apache
RUN apt-get update && docker-php-ext-install pdo pdo_mysql
RUN rm -f /etc/apache2/sites-available/000-default.conf
ADD ./settings/000-default.conf /etc/apache2/sites-available
Both Dockerfile and docker-compose.yml are in the laravel root directory. To run laravel based app, server must point to public folder. So, you can see I am replacing apache2's default configuration file with below 000-default.conf file,
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Everything runs fine on docker-compose up command but, when I browse localhost:9899 I get Forbidden error, but localhost:9899/public launches laravel app correctly. That means my supplied 000-default.conf is not having effect and server still points to /var/www/html/ instead of /var/www/html/public.
So I tried, exec to get into running container to check the 000-default.conf. And I could see my file instead of default. I am not getting my head around this issue. I want apache to consider my 000-default.conf . I hope you guys can see what I am doing wrong.
Apache does not look into the sites-available directory but rather in the sites-enabled directory.
You can either ADD your config-file into the latter directory, or set up a symlink:
ADD ./settings/000-default.conf /etc/apache2/sites-available
RUN ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/000-default.conf