我试着创建一个像这样的Docker服务:
version: '3'
services:
app:
image: php:8.2-fpm
working_dir: "/var/www"
volumes:
- ./src:/var/www/html
networks:
- internal
nginx:
restart: always
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./src:/var/www/html
depends_on:
- app
networks:
- internal
networks:
internal:
name: internal
个字符
但是当我将app
容器更改为apiapp
时,出现了一个问题:
services:
apiapp:
image: php:8.2-fpm
working_dir: "/var/www"
volumes:
- ./src:/var/www/html
networks:
- internal
nginx:
restart: always
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./src:/var/www/html
depends_on:
- apiapp
networks:
- internal
networks:
internal:
name: internal
$ docker-compose up -d
[+] Running 3/3
✔ Network internal Created 0.0s
✔ Container my-php-apiapp-1 Started 0.5s
✔ Container my-php-nginx-1 Started 0.9s
$ curl "http://localhost:8080"
curl: (52) Empty reply from server
的字符串
我先删除我的服务,然后输入docker-compose up -d
,所以我假设my-php-app-1
与my-php-apiapp-1
无关,但我真的不知道为什么容器名称很重要。
我试图在Docker上找到““app”container”,但是Compose FAQs和Key features and use cases of Docker Compose对我的问题没有帮助。
Nginx设置文件基本上来自qiita上的一篇文章:
# https://qiita.com/shir01earn/items/f236c8280bb745dd6fb4
server {
listen 80;
root /var/www/html;
index index.php;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
root /var/www/html;
index index.php;
try_files $uri $uri/ $uri.php /index.php?$query_string =404;
}
}
型
1条答案
按热度按时间omhiaaxx1#
当你的Nginx配置说
字符串
它使用FastCGI协议将请求转发到主机名
app
解析为的端口9000。在编写上下文中,服务名称
型
解析为主机名,假设服务具有兼容的
networks:
。(另请参阅Docker文档中的Networking in Compose。)如果你想将
app
容器重命名为其他名称,你需要将fastcgi_pass
行也更改为匹配。如果Nginx配置看到一个不存在的主机名,你可能会得到一个启动时错误,这就是为什么你在示例中得到一个“连接拒绝”错误。