我正在使用 docker-compose 运行 Laravel 项目,这是
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
这是
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>
我还将其添加到服务器中的
httpd.conf
:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/>
</VirtualHost>
一切都很好,但我无法连接到其他容器,我尝试通过容器的 IP 地址访问它们,但这是不可能的。顺便说一句,它在我的计算机上运行,问题只是在服务器上。
这也是 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
当我通过curl使用容器的ip调用api时,出现以下错误:
"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)
另外,如果我直接通过
curl -s localhost:9001/api/blahblah
调用 api,我会收到此消息:
<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>
最终这是我通过
http://example.com/api/blahblah
调用 api 时收到的消息:
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
我忘记复制menu_service和restaurant_service的
public
目录的内容,导致错误。