我有两个python / flask服务,我想要dockerize。一个服务s1
是用户的终点。第二个,s2
帮助s1
。所以用户调用s1
,s1
调用s2
。
我该怎么做才能让s2
知道s1
的ip?如何通过最小化配置参数的数量来做到这一点(理想情况下,只更改一个设置)?
到目前为止s1
:
FROM python:3-alpine
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
EXPOSE ${FLASK_RUN_ON_PORT}
CMD [ "python", "-m", "flask", "run", "--host=0.0.0.0" ]
使用.env:
FLASK_ENV=production
FLASK_APP=s1.py
FLASK_RUN_PORT=5000
到目前为止s2
:
FROM python:3-alpine
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
CMD [ "python", "-m", "flask", "run", "--host=0.0.0.0" ]
使用.env:
FLASK_ENV=production
FLASK_APP=s2.py
FLASK_RUN_PORT=5050
和码头组成如下:
version: '3'
services:
webservice2:
build: /path/to/s2
env_file:
- /path/to/s2/.env
ports:
- ${FLASK_RUN_ON_PORT}:${FLASK_RUN_ON_PORT}
volumes:
- .:/code
webservice1:
build: /path/to/s1
env_file:
- /path/to/s1/.env
ports:
- ${FLASK_RUN_ON_PORT}:${FLASK_RUN_ON_PORT}
volumes:
- .:/code
depends_on:
- webservice2
所以,总结一下。问题是:如何向s1
表明s2
在5050端口?
你没有做任何特别的事。
来自docs:
启用服务进行通信不需要链接 - 默认情况下,任何服务都可以通过该服务的名称访问任何其他服务。 (另见the Links topic in Networking in Compose。)
除非你做某些事情来改变默认行为,否则两个容器将连接到同一个网络,主机webservice1
将从webservice2
和反之亦然。