Laravel on docker runs but can not CURL to its container - php

My laravel project runs on docker, and I can browse the project with http://localhost:8000. But when it comes to sending a request to the API of this project (API and Website are in the same project and container named web_master), I get this error:
cURL error 7: Failed to connect to localhost port 8000: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:8000/api/v1/user/register
I can send requests to other sites like google.com, but I can not send requests to the project.
this is a part of my docker-compose file:
services:
mysql:
image: mysql:8.0.30
container_name: mysql_master
restart: always
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: 123
MYSQL_DATABASE: master
volumes:
- ./mysql-data:/var/lib/mysql
ports:
- '33060:3306'
networks:
- laravel_master
web:
build:
context: .
container_name: web_master
ports:
- '8000:80'
volumes:
- ./core:/var/www/app
- ./apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
depends_on:
- mysql
networks:
- laravel_master
and the virtual host file:
<VirtualHost *:80>
ServerName laravel_app
DocumentRoot /var/www/app/public
<Directory /var/www/app>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Can anybody help me?

Related

Reverse proxy phpmyadmin through apache with a PHP container

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.

Fetching picture from folder in PHP Docker

In my PHP application i am using docker on my ubuntu machine. Previously it was working perfectly but now its not. I am trying to show image from my uplaoded folder. The link showing the the image src is correct.Plus file also exist in folder but the image is not showing . When i try to access image directly from browser URL its saying
Forbidden
You don't have permission to access this resource.
Apache/2.4.38 (Debian) Server at localhost Port 80
My Dockerfile is
FROM php:7.3.28-apache
RUN docker-php-ext-install mysqli
RUN chown -R www-data /var/*
RUN chown -R 777 /var/*
RUN a2enmod rewrite
EXPOSE 80
everything is working fine but only the image is not coming in my browser.
You can see in the above pic image src is there but its showing blank and image is exist in the folder.Also i tried to change the permission to 777 but didnt worked.
sudo chmod -R 777 uploads
Docker Compose.yaml
version: '3.1'
services:
php:
container_name: php_medix_uk
depends_on:
- db
build:
context: .
volumes:
- .:/var/www/html/
- ./config/vhosts:/etc/apache2/sites-enabled
- ./config/php/php.ini:/usr/local/etc/php/php.ini
ports:
- "80:80"
restart: "always"
# MySQL
db:
image: mysql:8.0.25
container_name: mysql_host
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: medix_pharma
MYSQL_USER: medix_pharma
MYSQL_PASSWORD: medix_pharma
volumes:
- ~/mysql/medix_uk:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
# phpMyAdmin
phpmyadmin:
# container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOSTS=mysql_host
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- 8080:80
depends_on:
- db
inside ./config/vhosts directory i have default.conf file
DocumentRoot /var/www/html/
<Directory /var/www/html/>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Thanks

Apache / PHP Docker containers are just serving the PHP source files and not executing the PHP files correctly

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>

cURL between Docker service php-fpm containers results in "Connection refused"

after looking for an answer 2 weeks along, going deep into the docker compose and docker networks documentations, I'd like to ask for some help right here.
I am creating two Web API services, let's called them a back API (back.api.dev) and a front API (front.api.dev).
What I tried so far :
The back API is connected to a MySQL database, and the front API only sends cURL requests to the back API. Both APIs are built upon Symfony and are processed by a docker PHP-FPM container. Everything is served by a docker Apache 2.4 container.
Sending requests through Postman and cURL requests to back.api.dev & front.api.dev are both working great. It works both from my host, but also from the Apache container. I also added 127.0.0.1 back.api.dev and 127.0.0.1 front.api.dev to my /etc/hosts host machine file. The back API is well connected to the database as well.
But when I send a request to a specific front API route which runs a cURL request to the back API using GuzzleHTTP client and send the answer back to the user, I get a cURL error 7: Failed to connect to back.api.dev port 80: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://back.api.dev/api/videos/
I also tried to send cURL requests from the CLI inside the front_api container but the result is the same. I also tried to send it directly to the port 9000 handled by php-fpm but I get a cURL error 56: Recv failure: Connection reset by peer error.
Here's the docker-compose.yml file :
version: "3.8"
networks:
my_api_network:
driver: bridge
# external:
# name: my_api_network_default
services:
apache:
container_name: 'api_apache'
image: bitnami/apache:latest
ports:
- 8080:8080
# - 8443:8443
volumes:
- ./docker/apache/vhosts/back-api-dev.conf:/vhosts/back-api-dev.conf:ro
- ./docker/apache/vhosts/front-api-dev.conf:/vhosts/front-api-dev.conf:ro
volumes_from:
- php_backend_api
- php_frontend_api
depends_on:
- php_backend_api
- php_frontend_api
networks:
- my_api_network
php_backend_api:
hostname: 'back.api.dev'
container_name: 'php_backend_api'
build:
context: docker/php7-fpm
network: host
args:
TIMEZONE: 'UTC'
volumes:
- ./docker/php7-fpm/php.ini:/usr/local/etc/php/php.ini:ro
- ./back_api/:/var/www/back_api:cached
- ./back_api/vendor:/var/www/back_api/vendor:delegated
- /var/www/back_api/var/
networks:
- my_api_network
php_frontend_api:
hostname: 'front.api.dev'
container_name: 'php_frontend_api'
build:
context: docker/php7-fpm
network: host
args:
TIMEZONE: 'UTC'
volumes:
- ./docker/php7-fpm/php.ini:/usr/local/etc/php/php.ini:ro
- ./front_api/:/var/www/front_api:cached
- ./front_api/vendor:/var/www/front_api/vendor:delegated
- /var/www/front_api/var/
networks:
- my_api_network
db:
container_name: 'mysql_db'
image: mysql:5.7
restart: always
volumes:
- ./docker/data/mysql:/var/lib/mysql:delegated
- ./docker/mysql:/etc/mysql/conf.d:ro
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
TZ: ${TIMEZONE}
command: --sql_mode="STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER" --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
networks:
- my_api_network
Here is my back API Apache Virtualhost :
<VirtualHost *:8080>
ServerName back.api.dev
DocumentRoot "/var/www/back_api/public"
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php_backend_api:9000/var/www/back_api/public/$1
<Directory "/var/www/back_api/public">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
</Directory>
LogLevel debug
# LogLevel warn
# LogLevel notice
ErrorLog /opt/bitnami/apache2/logs/error-back.log
CustomLog /opt/bitnami/apache2/logs/access-back.log combined
</VirtualHost>
My guess is that Apache is not enough configured to forward incoming curl requests to the php-fpm instance. I looked after Docker networks, aliases, drivers, extra_hosts but nothing has helped so far to fix this issue.
Thank you for your help.
I'm thinking that there's no url http://back.api.dev/api/videos/ from the front end.
maybe it needs a host entry in the front end like you've done on your host box.
sorry I don't have enough points to put this in as a suggestion comment rather than an answer.

Traefik PathPrefix redirect to apache

Hi I have Traefik set up in docker as reverse proxy.
It works well until server tries to redirect to some url.
ie.
I have setup services like this:
docker-compose.yml
version: "3.4"
services:
php:
image: 192.168.1.17/rab/php:latest
networks:
- backend
- proxy
labels:
- "traefik.enable=true"
- "traefik.backend=php"
- "traefik.frontend.rule=Host:192.168.1.27"
- "traefik.docker.network=proxy"
- "traefik.port=9000"
container_name: php
apache:
image: 192.168.1.17/rab/apache:latest
networks:
- proxy
links:
- php
labels:
- "traefik.enable=true"
- "traefik.backend=apache"
- "traefik.frontend.rule=Host:192.168.1.27; PathPrefixStrip:/rab;"
- "traefik.docker.network=proxy"
- "traefik.port=80"
- "traefik.frontend.entryPoints=http"
- "traefik.frontend.headers.SSLRedirect=false"
container_name: apache
networks:
backend:
proxy:
external:
name: traefik_proxy
So if I enter http://192.168.1.27/rab/login.php, it works and the login page shows up.
But, when I successfully login, it tries to redirect me to http://192.168.1.27/index.php instead of http://192.168.1.27/rab/index.php and this does not work
The apache configuration is:
ServerName localhost
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
DocumentRoot /var/www/html/
<Directory /var/www/html/>
DirectoryIndex login.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
How to fix this?
Please help
Thanks

Categories