I'm running a Laravel project using docker-compose and here's the docker-compose.yaml
:
version: '3.7'
services:
apis:
ports:
- "9000:80"
build:
dockerfile: Dockerfile
context: ./apis
menu_service:
ports:
- "9001:80"
build:
dockerfile: Dockerfile
context: ./menu-service
restaurant_service:
ports:
- "9002:80"
build:
dockerfile: Dockerfile
context: ./restaurant-service
And this is Dockerfile
:
FROM php:8.3-apache
RUN apt-get update && \
apt-get install -y \
libzip-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev && \
docker-php-ext-configure gd && \
docker-php-ext-install gd \
zip
RUN a2enmod rewrite
RUN docker-php-ext-install pdo_mysql zip
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
COPY apis.conf /etc/apache2/sites-available/
RUN a2ensite apis.conf
RUN service apache2 restart
COPY . /var/www/html
WORKDIR /var/www/html
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN composer self-update --2
RUN composer install
RUN composer dump-autoload
RUN php artisan key:generate
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
apis.conf
:
Listen 9000
<VirtualHost *:9000>
ServerName localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
Also I added this to httpd.conf
in the server:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/>
</VirtualHost>
Everything is fine but I can't connect to the other containers and I tried to reach them by IP address of container but it wasn't possible. BTW, it works on my computer and the issue just is on the server.
Also this is the output of netstat:
$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:9002 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9001 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:44195 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::443 :::* LISTEN
tcp6 0 0 :::9002 :::* LISTEN
tcp6 0 0 :::9000 :::* LISTEN
tcp6 0 0 :::9001 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
When I call the api using container's ip by curl I got this error:
"message": "cURL error 28: Failed to connect to 192.168.96.2 port 80 after 133115 ms: Couldn't connect to server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
Also if I call the api directly by curl -s localhost:9001/api/blahblah
I got this message:
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<hr>
<address>Apache/2.4.59 (Debian) Server at localhost Port 9001</address>
</body></html>
And eventually this is the message when I call the api by http://example.com/api/blahblah
:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request
Reason: Error reading from remote server
I forgot to copy the contents of public
directory for menu_service and restaurant_service, which caused the error.