2 回答

TA貢獻(xiàn)1744條經(jīng)驗 獲得超4個贊
這是因為您重用了包含將公開端口8000的部分的完整配置(順便說一句,這是多余的,因為您的部分已經(jīng)公開了端口)。python-apiportsexposeports
我會創(chuàng)建一個可供任何服務(wù)使用的通用部分。在你的情況下,它會是這樣的:
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貢獻(xiàn)2003條經(jīng)驗 獲得超2個贊
該文件中有很多內(nèi)容,但其中大部分是不必要的。 在泊塢文件中幾乎什么都不做; 當(dāng)前網(wǎng)絡(luò)系統(tǒng)不需要;撰寫為您提供網(wǎng)絡(luò);您嘗試將代碼注入到應(yīng)該已存在于映像中的容器中。如果清理所有這些內(nèi)容,則您真正想要從一個容器重用到另一個容器的唯一部分是其(或),此時 YAML 錨點語法是不必要的。docker-compose.ymlexpose:links:defaultvolumes:build:image:
這應(yīng)該在功能上等效于您在問題中顯示的內(nèi)容: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
同樣,請注意,我們實際上從容器復(fù)制到容器的唯一內(nèi)容是塊;將在兩個容器之間共享的所有其他設(shè)置(代碼、公開的端口)都包含在 描述如何生成映像的 中。python-apipython-celerybuild:Dockerfile
另一方面,您需要確保所有這些設(shè)置實際上都包含在您的: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
添加回答
舉報