2 回答
TA贡献1744条经验 获得超4个赞
这是因为您重用了包含将公开端口8000的部分的完整配置(顺便说一句,这是多余的,因为您的部分已经公开了端口)。python-apiportsexposeports
我会创建一个可供任何服务使用的通用部分。在你的情况下,它会是这样的:
version: '3.7'
x-common-python-api:
&default-python-api
build:
context: /Users/AjayB/Desktop/python-api/
networks:
- app-tier
environment:
- PYTHON_API_ENV=development
volumes:
- .:/python_api/
services:
python-api:
<<: *default-python-api
ports:
- "8000:8000"
depends_on:
- python-model
command: >
sh -c "ls /python-api/ &&
python_api_setup.sh development
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
python-model: &python-model
.
.
.
python-celery:
<<: *default-python-api
links:
- redis:redis
depends_on:
- redis
command: >
sh -c "celery -A server worker -l info"
redis:
.
.
.
TA贡献2003条经验 获得超2个赞
该文件中有很多内容,但其中大部分是不必要的。 在泊坞文件中几乎什么都不做; 当前网络系统不需要;撰写为您提供网络;您尝试将代码注入到应该已存在于映像中的容器中。如果清理所有这些内容,则您真正想要从一个容器重用到另一个容器的唯一部分是其(或),此时 YAML 锚点语法是不必要的。docker-compose.ymlexpose:links:defaultvolumes:build:image:
这应该在功能上等效于您在问题中显示的内容:docker-compose.yml
version: '3'
services:
python-api:
build:
context: /Users/AjayB/Desktop/python-api/
ports:
- "8000:8000"
# No networks:, use `default`
# No expose:, use what's in the Dockerfile (or nothing)
depends_on:
- python-model
# No volumes:, use what's in the Dockerfile
# No environment:, this seems to be a required setting in the Dockerfile
# No command:, use what's in the Dockerfile
python-model:
build:
context: /Users/AjayB/Desktop/Python/python/
ports:
- "8001:8001"
python-celery:
build: # copied from python-api
context: /Users/AjayB/Desktop/python-api/
depends_on:
- redis
command: celery -A server worker -l info # one line, no sh -c wrapper
redis:
image: redis:5.0.8-alpine
# No hostname:, it doesn't do anything
ports:
- "6379:6379"
# No command:, use what's in the image
同样,请注意,我们实际上从容器复制到容器的唯一内容是块;将在两个容器之间共享的所有其他设置(代码、公开的端口)都包含在 描述如何生成映像的 中。python-apipython-celerybuild:Dockerfile
另一方面,您需要确保所有这些设置实际上都包含在您的:Dockerfile
# Copy the application code in
COPY . .
# Set the "development" environment variable
ENV PYTHON_API_ENV=development
# Document which port you'll use by default
EXPOSE 8000
# Specify the default command to run
# (Consider writing a shell script with this content instead)
CMD python_api_setup.sh development && \
python manage.py migrate && \
python manage.py runserver 0.0.0.0:8000
添加回答
举报