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.
Related
I have a docker-compose and apache.dockerfile which I am using to create a local server. It's a pretty basic setup.
I set up a virtual host in httpd-vhosts.conf and I know the server name and alias name is working because if i go to dev.flying I see the "It works!" page (which is the default index.html in htdocs).
However why wouldn't the document root declaration also be working?
docker-compose.yml
services:
apache:
build:
context: .
dockerfile: apache.dockerfile
container_name: dev_apache
depends_on:
- php
- mysql
ports:
- 80:80
volumes:
- ./wordpress:/var/www/html:delegated
apache.dockerfile
FROM httpd:alpine
ADD ./apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin email#email.com
DocumentRoot /var/www/html
ServerName dev.flying
ServerAlias dev.flying
ErrorLog logs/dev.flying-error_log
CustomLog logs/dev.flying-access_log commo
<Location /var/www/html>
ProxyPass http://localhost:9000/
ProxyPassReverse http://localhost:9000/
Order allow,deny
Allow from all
</Location>
</VirtualHost>
Looking into some documentations here is what I think you will need to do (maybe with some changes to fit in your configuration)
The httpd docker official image docs recommends to export the configuration first
$ docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > ./apache/my-httpd.conf # Dumping to your apache folder
then after editing as you want copy the edited part for your container. In this case you will need to tell apache to include vhosts configuration uncommenting the line
# Include conf/extra/httpd-vhosts.conf
After that you virtual host configuration will override any request made for the server even if not matching the ServerName (you'll not see the htdocs default anymore)
docker-compose.yml - Example
services:
apache:
image: httpd:alpine # Using the image directly since everything is accessible through the volumes
container_name: dev_apache
ports:
- 80:80
volumes:
- ./wordpress:/var/www/html:delegated
- ./apache/my-httpd.conf:/usr/local/apache2/conf/httpd.conf
- ./apache/httpd-vhosts.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
Edit
Also you will want a Directory directive to give permission in /var/www/html directory since the default directive in my-httpd.conf is to deny the entire server filesystem. Something like this:
<VirtualHost *:80>
<...> Your stuff <...>
<Directory /var/www/html>
Require all granted
</Directory>
</VirtualHost>
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 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'
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 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