Python 芹菜试图在泊坞站组成中占用端口号并制造问题

docker-compose.yml:


  python-api: &python-api

    build:

      context: /Users/AjayB/Desktop/python-api/

    ports:

      - "8000:8000"

    networks:

      - app-tier

    expose:

      - "8000"

    depends_on:

      - python-model

    volumes:

      - .:/python_api/

    environment:

      - PYTHON_API_ENV=development

    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

    build:

      context: /Users/AjayB/Desktop/Python/python/

    ports:

      - "8001:8001"

    networks:

      - app-tier

    environment:

      - PYTHON_API_ENV=development

    expose:

      - "8001"

    volumes:

      - .:/python_model/

    command: >

      sh -c "ls /python-model/

             python_setup.sh development

             cd /server/ &&

             python manage.py migrate &&

             python manage.py runserver 0.0.0.0:8001"


  python-celery:

    <<: *python-api

    environment:

      - PYTHON_API_ENV=development

    networks:

      - app-tier

    links:

      - redis:redis

    depends_on:

      - redis

    command: >

      sh -c "celery -A server worker -l info"


  redis:

    image: redis:5.0.8-alpine

    hostname: redis

    networks:

          - app-tier

    expose:

      - "6379"

    ports:

      - "6379:6379"

    command: ["redis-server"]

python-celery在里面,它应该作为一个单独的容器运行。但它试图占用与 相同的端口,这绝不应该是这种情况。python-apipython-api


慕森王
浏览 84回答 2
2回答

慕无忌1623718

这是因为您重用了包含将公开端口8000的部分的完整配置(顺便说一句,这是多余的,因为您的部分已经公开了端口)。python-apiportsexposeports我会创建一个可供任何服务使用的通用部分。在你的情况下,它会是这样的:version: '3.7'x-common-python-api:&nbsp; &nbsp;&default-python-api&nbsp; &nbsp; build:&nbsp; &nbsp; &nbsp; context: /Users/AjayB/Desktop/python-api/&nbsp; &nbsp; networks:&nbsp; &nbsp; &nbsp; - app-tier&nbsp; &nbsp; environment:&nbsp; &nbsp; &nbsp; - PYTHON_API_ENV=development&nbsp; &nbsp; volumes:&nbsp; &nbsp; &nbsp; - .:/python_api/services:&nbsp; python-api:&nbsp; &nbsp; <<: *default-python-api&nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; - "8000:8000"&nbsp; &nbsp; depends_on:&nbsp; &nbsp; &nbsp; - python-model&nbsp; &nbsp; command: >&nbsp; &nbsp; &nbsp; sh -c "ls /python-api/ &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;python_api_setup.sh development&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;python manage.py migrate &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;python manage.py runserver 0.0.0.0:8000"&nbsp; python-model: &python-model&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.&nbsp; python-celery:&nbsp; &nbsp; <<: *default-python-api&nbsp; &nbsp; links:&nbsp; &nbsp; &nbsp; - redis:redis&nbsp; &nbsp; depends_on:&nbsp; &nbsp; &nbsp; - redis&nbsp; &nbsp; command: >&nbsp; &nbsp; &nbsp; sh -c "celery -A server worker -l info"&nbsp; redis:&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.

湖上湖

该文件中有很多内容,但其中大部分是不必要的。 在泊坞文件中几乎什么都不做; 当前网络系统不需要;撰写为您提供网络;您尝试将代码注入到应该已存在于映像中的容器中。如果清理所有这些内容,则您真正想要从一个容器重用到另一个容器的唯一部分是其(或),此时 YAML 锚点语法是不必要的。docker-compose.ymlexpose:links:defaultvolumes:build:image:这应该在功能上等效于您在问题中显示的内容:docker-compose.ymlversion: '3'services:&nbsp; python-api:&nbsp; &nbsp; build:&nbsp; &nbsp; &nbsp; context: /Users/AjayB/Desktop/python-api/&nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; - "8000:8000"&nbsp; &nbsp; # No networks:, use `default`&nbsp; &nbsp; # No expose:, use what's in the Dockerfile (or nothing)&nbsp; &nbsp; depends_on:&nbsp; &nbsp; &nbsp; - python-model&nbsp; &nbsp; # No volumes:, use what's in the Dockerfile&nbsp; &nbsp; # No environment:, this seems to be a required setting in the Dockerfile&nbsp; &nbsp; # No command:, use what's in the Dockerfile&nbsp; python-model:&nbsp; &nbsp; build:&nbsp; &nbsp; &nbsp; context: /Users/AjayB/Desktop/Python/python/&nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; - "8001:8001"&nbsp; python-celery:&nbsp; &nbsp; build: # copied from python-api&nbsp; &nbsp; &nbsp; context: /Users/AjayB/Desktop/python-api/&nbsp; &nbsp; depends_on:&nbsp; &nbsp; &nbsp; - redis&nbsp; &nbsp; command: celery -A server worker -l info # one line, no sh -c wrapper&nbsp; redis:&nbsp; &nbsp; image: redis:5.0.8-alpine&nbsp; &nbsp; # No hostname:, it doesn't do anything&nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; - "6379:6379"&nbsp; &nbsp; # No command:, use what's in the image同样,请注意,我们实际上从容器复制到容器的唯一内容是块;将在两个容器之间共享的所有其他设置(代码、公开的端口)都包含在 描述如何生成映像的 中。python-apipython-celerybuild:Dockerfile另一方面,您需要确保所有这些设置实际上都包含在您的:Dockerfile# Copy the application code inCOPY . .# Set the "development" environment variableENV PYTHON_API_ENV=development# Document which port you'll use by defaultEXPOSE 8000# Specify the default command to run# (Consider writing a shell script with this content instead)CMD python_api_setup.sh development && \&nbsp; &nbsp; python manage.py migrate && \&nbsp; &nbsp; python manage.py runserver 0.0.0.0:8000
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python