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
Related
I'm trying to setup a docker environment with the the following containers: apache, php, mysql, phpmyadmin.
I have a problem making both phpmyadmin and my applications work at the same time, and I was wondering if it wasn't because I use a PHP container to run my PHP instead of phpmyadmin that also have PHP installed.
I understood that phpmyadmin runs with PHP (hence the name), though I was wondering if it was possible to have a docker image without PHP and then to proxy the requests to php files from phpmyadmin to the php container.
I've already established a reverse proxy from apache to phpmyadmin because I don't need phpmyadmin to run it's own apache server, and I would like to do the same with PHP.
I tried to proxy php requests from apache to my php container, but I doesn't work.
Here is my full compose.yaml
version: '3.8'
services:
php:
build:
context: './php/'
args:
- PHP_VERSION=${PHP_VERSION}
volumes:
- "${APPS_VOLUME:-apps}:/var/www/html/"
networks:
- backend
restart: unless-stopped
stdin_open: true
tty: true
container_name: php
apache:
build:
context: './apache/'
args:
- APACHE_VERSION=${APACHE_VERSION}
ports:
- '${APACHE_PORT:-8000}:80'
volumes:
- "${APPS_VOLUME:-apps}:/usr/local/apache2/htdocs/"
- "pma:/usr/local/apache2/htdocs/phpmyadmin/"
networks:
- frontend
- backend
depends_on:
- php
- mysql
working_dir: /usr/local/apache2/htdocs/
restart: unless-stopped
container_name: apache
mysql:
image: mysql:${MYSQL_VERSION}
ports:
- '${MYSQL_PORT:-3307}:3306'
volumes:
- "db-data:/var/lib/mysql"
networks:
- backend
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
command: --federated
working_dir: /usr/
restart: unless-stopped
container_name: mysql
phpmyadmin:
image: phpmyadmin:${PHPMYADMIN_VERSION:-latest}
volumes:
- "pma:/var/www/html/"
networks:
- backend
environment:
- PMA_HOST=mysql
- PMA_ABSOLUTE_URI=http://localhost:${APACHE_PORT:-8000}/phpMyAdmin/
restart: unless-stopped
container_name: phpmyadmin
volumes:
apps:
name: apps
db-data:
name: db-data
pma:
name: pma
networks:
frontend:
name: frontend
backend:
name: backend
And my apache conf
ServerName localhost
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so
<VirtualHost *:80>
# Proxy .php requests to port 9000 of the php-fpm container
#ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000/var/www/html/"
</FilesMatch>
DocumentRoot /usr/local/apache2/htdocs/
<Directory /usr/local/apache2/htdocs/>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Proxy phpmyadmin request to port 80 of the phpmyadmin container
ProxyPass "/phpMyAdmin/" "http://phpmyadmin/"
ProxyPassReverse /phpMyAdmin/ http://phpmyadmin/
# Send apache logs to stdout and stderr
CustomLog /proc/self/fd/1 common
ErrorLog /proc/self/fd/2
</VirtualHost>
Is my configuration wrong? Do I have to use the phpmyadmin container to handle PHP requests or is there a way to separate them?
Thanks
Edit: Changed the title of the issue after Nigel Ren observation
I seem to have found a solution, I don't know if it is how I'm supposed to do but I can now use the phpmyadmin interface and open my apps. There's still some problems with both of them, but I'm not sure if it have anything to do with the apache configuration
I used the fpm-alpine image of phpmyadmin instead of the normal one and changed my conf file like so:
ServerName localhost
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so
LoadModule proxy_http_module /usr/local/apache2/modules/mod_proxy_http.so
<VirtualHost *:80>
# Proxy phpmyadmin request to the phpmyadmin container
ProxyPassMatch ^/phpMyAdmin/(.*\.php(/.*)?)$ fcgi://phpmyadmin:9000/var/www/html/$1
ProxyPassReverse /phpMyAdmin/ fcgi://phpmyadmin:9000/var/www/html/
# Proxy .php requests to port 9000 of the php-fpm container
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
ProxyPassReverse / fcgi://php:9000/var/www/html/$1
DocumentRoot /usr/local/apache2/htdocs/
<Directory /usr/local/apache2/htdocs/>
DirectoryIndex index.php
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>
I'm not sure the ProxyPassReverse are required as I seem to have the same result without them.
I just realized that phpmyadmin is simply an app. The docker image just provides you the latest version along with an environment to make it ready to use, but you can really just copy the source files into your apps directory and parse the php files with your php container like you do for any other app.
So to answer one of my first questions, you don't need to have both containers because you don't even need the phpmyadmin image.
I feel stupid.
Hope it helps someone so I can at least feel like it was not all in vain.
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 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 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 have successfully set up Docker, Apache2, PHP in separate containers. I can access my website via http://localhost:8080.
The problem I have is to do with /etc/hosts file (I think). Let's take a look at my docker-compose file:
apache2:
build:
context: ./apache2/
dockerfile: Dockerfile
container_name: apache2
image: httpd:2.4.39-alpine
ports:
- 8080:80
volumes:
- ../:/srv/app
extra_hosts:
- "custom-website-name.local:127.0.0.1"
As you can see I did add the extra host in there and it even appears in /etc/hosts file in the container. However, no matter what I do and what changes do I make(manually on the LIVE container and in config) I can't force my setup to recognize that new host.
I also have vhost config in place:
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so
<VirtualHost *:80>
ServerName www.custom-website-name.local
ServerAlias custom-website-name.local
# Proxy .php requests to port 9000 of the php-fpm container
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://webapp:9000/srv/app/public/$1
DocumentRoot /srv/app/public
<Directory /srv/app/public>
DirectoryIndex index.php
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>
Which is copied into the config. It all seems to be not enough at this point I can access my website only via http://localhost:8080 and http://custom-website-name.local:8080 gives me This site can’t be reached which means that new name was not resolved.
The question of course is: what am I missing? I have no clue what else I can try (and I did try the whole bunch of things).
EDIT:
I did add an entry to local (non docker) /etc/hosts as well - did not help.
Solved it!
HERE you can find a solution posted by a user named octesian. I looked only briefly at the entire thing but it seems to be some weird undocumented docker quirk. I do recommend to go deeper into networking documentation of Docker.
It looks like in the build section of apache2 service I had to add network: host so:
apache2:
build:
network: host
context: ./apache2/
dockerfile: Dockerfile
container_name: apache2
image: httpd:2.4.39-alpine
ports:
- 8080:80
I bet it is not perfect and will cause different issues down the line when I decide to introduce more websites but for now, it is a success and valid solution.