摘要:2个单独的应用程序,都使用docker-compose,如何才能同时提供http://app-1.test
和http://app-2.test
?
描述:
我觉得我错过了一些 super 简单的东西。我有2个php-fpm(通过nginx)应用程序,都由类似的docker-compose设置运行,有点像:
# docker-compose.yaml
version: '3'
services:
app:
build:
context: .
dockerfile: docker/Dockerfile
container_name: app_1
tty: true
depends_on:
- db
- dbtest
working_dir: /var/www
volumes:
- ./:/var/www
webserver:
image: nginx:stable
container_name: app_1_webserver
restart: always
ports:
- "80:80"
depends_on:
- app
volumes:
- ./:/var/www
- ./docker/app.conf:/etc/nginx/conf.d/default.conf
links:
- app
# ...
在我的/etc/hosts
上,我可以添加类似127.0.0.1 app-1.test
现在,我可以转到浏览器app-1.test
来调用该应用程序。第二个也有类似的设置,但是由于端口80被阻塞,它当然不会上升。我当然可以更改端口,但是URL类似于
app-2.test:81
而不是app-2.test
。我该怎么办,以便可以使用其他本地主机名运行第二个应用程序?还是使用其他端口是最好的方式?
最佳答案
你不能您可以做的是在图像(第三个图像)的前面添加一个“路由器”,该路由器根据主机名进行路由(代理传递)。
Apache或Nginx通常用于这类事情。
例如与Apache服务器
https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
<VirtualHost *:80>
ServerName app-1.test
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://image1:80/
ProxyPassReverse / http://image1:80/
ErrorLog /var/log/apache2/error.log
LogLevel info
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName app-2.test
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://image2:80/
ProxyPassReverse / http://image2:80/
ErrorLog /var/log/apache2/error.log
LogLevel info
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
现在,您可以在/ etc / hosts文件中的同一IP上添加两个名称,并且服务器可以根据提供的主机名( ServerName )在内部进行路由。http://image1:80/
(及其类似)引用应更改为docker内部dns,如docker-compose.yml中指定的
关于docker - 为docker-compose Web应用程序使用与localhost不同的主机名/URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63833507/