从不同的 docker 主机访问 docker 主机中的 api 运行
Accessing an api running in a docker host from a different docker host
我有两个 docker 项目,每个项目都有自己的 docker-compose.yml 文件。一个项目拥有一个 django api 和 docker 主机,而另一个项目用于 open edx 运行 在另一个 docker 主机上。如果我尝试使用 docker exec -it edx.devstack.studio curl http://localhost:8000/number/
从第二个 docker 主机的这些 docker 容器之一访问我的 django api,我会收到错误 curl: (7) Failed to connect to localhost port 8000: Connection refused
。我绝不是 docker 专家,所以我可能做错了什么。我尝试将两个项目中的所有 docker 服务添加到同一个网络,但仍然失败,可能是我做错了。任何帮助都感激不尽。下面是两个 docker 撰写文件。
Django 项目 docker 编写文件
version: "3"
services:
db:
image: mysql:5.7
container_name: db
env_file:
- .env
ports:
- '3307:3307'
expose:
- '3307'
volumes:
- my-db:/var/lib/mysql
django:
build:
context: .
container_name: web
env_file:
- .env
command: python src/manage.py runserver 0.0.0.0:8000
depends_on:
- db
ports:
- "8000:8000"
volumes:
- .:/app
networks:
- okay
networks:
okay:
external: true
volumes:
my-db:
打开 edx docker 编写文件
# This file contains all of the services for an edX installation. See https://docs.docker.com/compose/compose-file/
# for the appropriate syntax and definitions.
#
# Housekeeping Rules:
# - Group third-party and edX services separately
# - Alphabetize services in the groups
# - Alphabetize individual configuration options for each service
# - Every service's container name should be prefixed with "edx.devstack." to avoid conflicts with other containers
# that might be running for the same service.
version: "2.1"
services:
# Third-party services
chrome:
container_name: edx.devstack.chrome
image: edxops/chrome:${OPENEDX_RELEASE:-latest}
shm_size: 2g
ports:
- "15900:5900"
volumes: # for file uploads
- ../edx-e2e-tests/upload_files:/edx/app/e2e/edx-e2e-tests/upload_files
- ../edx-platform/common/test/data:/edx/app/edxapp/edx-platform/common/test/data
networks:
- okay
elasticsearch:
container_name: edx.devstack.elasticsearch
image: edxops/elasticsearch:devstack
# TODO: What to do about these forwarded ports? They'll conflict with ports forwarded by the Vagrant VM.
# ports:
# - "9200:9200"
# - "9300:9300"
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
- elasticsearch_data:/usr/share/elasticsearch/logs
networks:
- okay
firefox:
container_name: edx.devstack.firefox
image: edxops/firefox:${OPENEDX_RELEASE:-latest}
shm_size: 2g
ports:
- "25900:5900"
volumes: # for file uploads
- ../edx-e2e-tests/upload_files:/edx/app/e2e/edx-e2e-tests/upload_files
- ../edx-platform/common/test/data:/edx/app/edxapp/edx-platform/common/test/data
networks:
- okay
memcached:
container_name: edx.devstack.memcached
image: memcached:1.4.24
# ports:
# - "11211:11211"
networks:
- okay
mongo:
# We use WiredTiger in all environments. In development environments we use small files
# to conserve disk space, and disable the journal for a minor performance gain.
# See https://docs.mongodb.com/v3.0/reference/program/mongod/#options for complete details.
command: mongod --smallfiles --nojournal --storageEngine wiredTiger
container_name: edx.devstack.mongo
image: mongo:3.2.16
# ports:
# - "27017:27017"
volumes:
- mongo_data:/data/db
networks:
- okay
mysql:
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
container_name: edx.devstack.mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
image: mysql:5.6
# ports:
# - "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- okay
# edX services
credentials:
command: bash -c 'source /edx/app/credentials/credentials_env && while true; do python /edx/app/credentials/credentials/manage.py runserver 0.0.0.0:18150; sleep 2; done'
container_name: edx.devstack.credentials
depends_on:
- mysql
- memcached
# Allows attachment to the credentials service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
CACHE_LOCATION: edx.devstack.memcached:12211
DB_HOST: edx.devstack.mysql
SOCIAL_AUTH_EDX_OIDC_URL_ROOT: http://edx.devstack.lms:18000/oauth2
ENABLE_DJANGO_TOOLBAR: 1
image: edxops/credentials:${OPENEDX_RELEASE:-latest}
ports:
- "18150:18150"
networks:
- okay
discovery:
command: bash -c 'source /edx/app/discovery/discovery_env && while true; do python /edx/app/discovery/discovery/manage.py runserver 0.0.0.0:18381; sleep 2; done'
container_name: edx.devstack.discovery
depends_on:
- mysql
- elasticsearch
- memcached
# Allows attachment to the discovery service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
TEST_ELASTICSEARCH_URL: "http://edx.devstack.elasticsearch:9200"
ENABLE_DJANGO_TOOLBAR: 1
image: edxops/discovery:${OPENEDX_RELEASE:-latest}
ports:
- "18381:18381"
volumes:
- discovery_assets:/edx/var/discovery/
networks:
- okay
ecommerce:
command: bash -c 'source /edx/app/ecommerce/ecommerce_env && while true; do python /edx/app/ecommerce/ecommerce/manage.py runserver 0.0.0.0:18130; sleep 2; done'
container_name: edx.devstack.ecommerce
depends_on:
- mysql
- memcached
# Allows attachment to the ecommerce service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
ENABLE_DJANGO_TOOLBAR: 0
image: edxops/ecommerce:${OPENEDX_RELEASE:-latest}
ports:
- "18130:18130"
networks:
- okay
lms:
command: bash -c 'source /edx/app/edxapp/edxapp_env && while true; do python /edx/app/edxapp/edx-platform/manage.py lms runserver 0.0.0.0:18000 --settings devstack_docker; sleep 2; done'
container_name: edx.devstack.lms
depends_on:
- devpi
- mysql
- memcached
- mongo
# Allows attachment to the LMS service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
BOK_CHOY_HOSTNAME: edx.devstack.lms
BOK_CHOY_LMS_PORT: 18003
BOK_CHOY_CMS_PORT: 18031
EDXAPP_TEST_MONGO_HOST: edx.devstack.mongo
NO_PYTHON_UNINSTALL: 1
image: edxops/edxapp:${OPENEDX_RELEASE:-latest}
ports:
- "18000:18000"
- "19876:19876" # JS test debugging
# - "18003:18003"
# - "18031:18031"
volumes:
- edxapp_lms_assets:/edx/var/edxapp/staticfiles/
networks:
- okay
edx_notes_api:
command: bash -c 'source /edx/app/edx_notes_api/edx_notes_api_env && while true; do python /edx/app/edx_notes_api/edx_notes_api/manage.py runserver 0.0.0.0:18120 --settings notesserver.settings.devstack; sleep 2; done'
container_name: edx.devstack.edx_notes_api
depends_on:
- devpi
- elasticsearch
- mysql
image: edxops/notes:${OPENEDX_RELEASE:-latest}
ports:
- "18120:18120"
environment:
DB_ENGINE: "django.db.backends.mysql"
DB_HOST: "edx.devstack.mysql"
DB_NAME: "notes"
DB_PASSWORD: "password"
DB_PORT: "3306"
DB_USER: "notes001"
ENABLE_DJANGO_TOOLBAR: 1
ELASTICSEARCH_URL: "http://edx.devstack.elasticsearch:9200"
networks:
- okay
studio:
command: bash -c 'source /edx/app/edxapp/edxapp_env && while true; do python /edx/app/edxapp/edx-platform/manage.py cms runserver 0.0.0.0:18010 --settings devstack_docker; sleep 2; done'
container_name: edx.devstack.studio
depends_on:
- devpi
- mysql
- memcached
- mongo
# Allows attachment to the Studio service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
BOK_CHOY_HOSTNAME: edx.devstack.studio
BOK_CHOY_LMS_PORT: 18103
BOK_CHOY_CMS_PORT: 18131
EDXAPP_TEST_MONGO_HOST: edx.devstack.mongo
NO_PYTHON_UNINSTALL: 1
image: edxops/edxapp:${OPENEDX_RELEASE:-latest}
ports:
- "18010:18010"
- "19877:19877" # JS test debugging
# - "18103:18103"
# - "18131:18131"
volumes:
- edxapp_studio_assets:/edx/var/edxapp/staticfiles/
networks:
- okay
forum:
command: bash -c 'source /edx/app/forum/ruby_env && source /edx/app/forum/devstack_forum_env && cd /edx/app/forum/cs_comments_service && bundle install && while true; do ruby app.rb -o 0.0.0.0 ; sleep 2; done'
container_name: edx.devstack.forum
depends_on:
- mongo
- memcached
- elasticsearch
image: edxops/forum:${OPENEDX_RELEASE:-latest}
ports:
- "44567:4567"
networks:
- okay
devpi:
container_name: edx.devstack.devpi
image: edxops/devpi:${OPENEDX_RELEASE:-latest}
ports:
- "3141:3141"
volumes:
- devpi_data:/data
networks:
- okay
networks:
okay:
external: true
volumes:
discovery_assets:
edxapp_lms_assets:
edxapp_studio_assets:
elasticsearch_data:
mongo_data:
mysql_data:
devpi_data:
首先检查 "db" 容器并检查其网络,因为它没有明确加入 "okay" 网络。
感谢这个 answer I was able to get a solution to this, you can use host.docker.internal 解析到您的 IP 地址,而不是使用 localhost
来引用该容器。这将允许您向不同容器上的 api 运行 发出请求。例如使用 http://host.docker.internal:8000/number/
而不是 http://localhost:8000/number/
。
我有两个 docker 项目,每个项目都有自己的 docker-compose.yml 文件。一个项目拥有一个 django api 和 docker 主机,而另一个项目用于 open edx 运行 在另一个 docker 主机上。如果我尝试使用 docker exec -it edx.devstack.studio curl http://localhost:8000/number/
从第二个 docker 主机的这些 docker 容器之一访问我的 django api,我会收到错误 curl: (7) Failed to connect to localhost port 8000: Connection refused
。我绝不是 docker 专家,所以我可能做错了什么。我尝试将两个项目中的所有 docker 服务添加到同一个网络,但仍然失败,可能是我做错了。任何帮助都感激不尽。下面是两个 docker 撰写文件。
Django 项目 docker 编写文件
version: "3"
services:
db:
image: mysql:5.7
container_name: db
env_file:
- .env
ports:
- '3307:3307'
expose:
- '3307'
volumes:
- my-db:/var/lib/mysql
django:
build:
context: .
container_name: web
env_file:
- .env
command: python src/manage.py runserver 0.0.0.0:8000
depends_on:
- db
ports:
- "8000:8000"
volumes:
- .:/app
networks:
- okay
networks:
okay:
external: true
volumes:
my-db:
打开 edx docker 编写文件
# This file contains all of the services for an edX installation. See https://docs.docker.com/compose/compose-file/
# for the appropriate syntax and definitions.
#
# Housekeeping Rules:
# - Group third-party and edX services separately
# - Alphabetize services in the groups
# - Alphabetize individual configuration options for each service
# - Every service's container name should be prefixed with "edx.devstack." to avoid conflicts with other containers
# that might be running for the same service.
version: "2.1"
services:
# Third-party services
chrome:
container_name: edx.devstack.chrome
image: edxops/chrome:${OPENEDX_RELEASE:-latest}
shm_size: 2g
ports:
- "15900:5900"
volumes: # for file uploads
- ../edx-e2e-tests/upload_files:/edx/app/e2e/edx-e2e-tests/upload_files
- ../edx-platform/common/test/data:/edx/app/edxapp/edx-platform/common/test/data
networks:
- okay
elasticsearch:
container_name: edx.devstack.elasticsearch
image: edxops/elasticsearch:devstack
# TODO: What to do about these forwarded ports? They'll conflict with ports forwarded by the Vagrant VM.
# ports:
# - "9200:9200"
# - "9300:9300"
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
- elasticsearch_data:/usr/share/elasticsearch/logs
networks:
- okay
firefox:
container_name: edx.devstack.firefox
image: edxops/firefox:${OPENEDX_RELEASE:-latest}
shm_size: 2g
ports:
- "25900:5900"
volumes: # for file uploads
- ../edx-e2e-tests/upload_files:/edx/app/e2e/edx-e2e-tests/upload_files
- ../edx-platform/common/test/data:/edx/app/edxapp/edx-platform/common/test/data
networks:
- okay
memcached:
container_name: edx.devstack.memcached
image: memcached:1.4.24
# ports:
# - "11211:11211"
networks:
- okay
mongo:
# We use WiredTiger in all environments. In development environments we use small files
# to conserve disk space, and disable the journal for a minor performance gain.
# See https://docs.mongodb.com/v3.0/reference/program/mongod/#options for complete details.
command: mongod --smallfiles --nojournal --storageEngine wiredTiger
container_name: edx.devstack.mongo
image: mongo:3.2.16
# ports:
# - "27017:27017"
volumes:
- mongo_data:/data/db
networks:
- okay
mysql:
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
container_name: edx.devstack.mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
image: mysql:5.6
# ports:
# - "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- okay
# edX services
credentials:
command: bash -c 'source /edx/app/credentials/credentials_env && while true; do python /edx/app/credentials/credentials/manage.py runserver 0.0.0.0:18150; sleep 2; done'
container_name: edx.devstack.credentials
depends_on:
- mysql
- memcached
# Allows attachment to the credentials service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
CACHE_LOCATION: edx.devstack.memcached:12211
DB_HOST: edx.devstack.mysql
SOCIAL_AUTH_EDX_OIDC_URL_ROOT: http://edx.devstack.lms:18000/oauth2
ENABLE_DJANGO_TOOLBAR: 1
image: edxops/credentials:${OPENEDX_RELEASE:-latest}
ports:
- "18150:18150"
networks:
- okay
discovery:
command: bash -c 'source /edx/app/discovery/discovery_env && while true; do python /edx/app/discovery/discovery/manage.py runserver 0.0.0.0:18381; sleep 2; done'
container_name: edx.devstack.discovery
depends_on:
- mysql
- elasticsearch
- memcached
# Allows attachment to the discovery service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
TEST_ELASTICSEARCH_URL: "http://edx.devstack.elasticsearch:9200"
ENABLE_DJANGO_TOOLBAR: 1
image: edxops/discovery:${OPENEDX_RELEASE:-latest}
ports:
- "18381:18381"
volumes:
- discovery_assets:/edx/var/discovery/
networks:
- okay
ecommerce:
command: bash -c 'source /edx/app/ecommerce/ecommerce_env && while true; do python /edx/app/ecommerce/ecommerce/manage.py runserver 0.0.0.0:18130; sleep 2; done'
container_name: edx.devstack.ecommerce
depends_on:
- mysql
- memcached
# Allows attachment to the ecommerce service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
ENABLE_DJANGO_TOOLBAR: 0
image: edxops/ecommerce:${OPENEDX_RELEASE:-latest}
ports:
- "18130:18130"
networks:
- okay
lms:
command: bash -c 'source /edx/app/edxapp/edxapp_env && while true; do python /edx/app/edxapp/edx-platform/manage.py lms runserver 0.0.0.0:18000 --settings devstack_docker; sleep 2; done'
container_name: edx.devstack.lms
depends_on:
- devpi
- mysql
- memcached
- mongo
# Allows attachment to the LMS service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
BOK_CHOY_HOSTNAME: edx.devstack.lms
BOK_CHOY_LMS_PORT: 18003
BOK_CHOY_CMS_PORT: 18031
EDXAPP_TEST_MONGO_HOST: edx.devstack.mongo
NO_PYTHON_UNINSTALL: 1
image: edxops/edxapp:${OPENEDX_RELEASE:-latest}
ports:
- "18000:18000"
- "19876:19876" # JS test debugging
# - "18003:18003"
# - "18031:18031"
volumes:
- edxapp_lms_assets:/edx/var/edxapp/staticfiles/
networks:
- okay
edx_notes_api:
command: bash -c 'source /edx/app/edx_notes_api/edx_notes_api_env && while true; do python /edx/app/edx_notes_api/edx_notes_api/manage.py runserver 0.0.0.0:18120 --settings notesserver.settings.devstack; sleep 2; done'
container_name: edx.devstack.edx_notes_api
depends_on:
- devpi
- elasticsearch
- mysql
image: edxops/notes:${OPENEDX_RELEASE:-latest}
ports:
- "18120:18120"
environment:
DB_ENGINE: "django.db.backends.mysql"
DB_HOST: "edx.devstack.mysql"
DB_NAME: "notes"
DB_PASSWORD: "password"
DB_PORT: "3306"
DB_USER: "notes001"
ENABLE_DJANGO_TOOLBAR: 1
ELASTICSEARCH_URL: "http://edx.devstack.elasticsearch:9200"
networks:
- okay
studio:
command: bash -c 'source /edx/app/edxapp/edxapp_env && while true; do python /edx/app/edxapp/edx-platform/manage.py cms runserver 0.0.0.0:18010 --settings devstack_docker; sleep 2; done'
container_name: edx.devstack.studio
depends_on:
- devpi
- mysql
- memcached
- mongo
# Allows attachment to the Studio service using 'docker attach <containerID>'.
stdin_open: true
tty: true
environment:
BOK_CHOY_HOSTNAME: edx.devstack.studio
BOK_CHOY_LMS_PORT: 18103
BOK_CHOY_CMS_PORT: 18131
EDXAPP_TEST_MONGO_HOST: edx.devstack.mongo
NO_PYTHON_UNINSTALL: 1
image: edxops/edxapp:${OPENEDX_RELEASE:-latest}
ports:
- "18010:18010"
- "19877:19877" # JS test debugging
# - "18103:18103"
# - "18131:18131"
volumes:
- edxapp_studio_assets:/edx/var/edxapp/staticfiles/
networks:
- okay
forum:
command: bash -c 'source /edx/app/forum/ruby_env && source /edx/app/forum/devstack_forum_env && cd /edx/app/forum/cs_comments_service && bundle install && while true; do ruby app.rb -o 0.0.0.0 ; sleep 2; done'
container_name: edx.devstack.forum
depends_on:
- mongo
- memcached
- elasticsearch
image: edxops/forum:${OPENEDX_RELEASE:-latest}
ports:
- "44567:4567"
networks:
- okay
devpi:
container_name: edx.devstack.devpi
image: edxops/devpi:${OPENEDX_RELEASE:-latest}
ports:
- "3141:3141"
volumes:
- devpi_data:/data
networks:
- okay
networks:
okay:
external: true
volumes:
discovery_assets:
edxapp_lms_assets:
edxapp_studio_assets:
elasticsearch_data:
mongo_data:
mysql_data:
devpi_data:
首先检查 "db" 容器并检查其网络,因为它没有明确加入 "okay" 网络。
感谢这个 answer I was able to get a solution to this, you can use host.docker.internal 解析到您的 IP 地址,而不是使用 localhost
来引用该容器。这将允许您向不同容器上的 api 运行 发出请求。例如使用 http://host.docker.internal:8000/number/
而不是 http://localhost:8000/number/
。