问题描述
以下是 v2 规范中的工作 docker-compose 文件:
Below is the working docker-compose file in v2 spec:
version: '2'
volumes:
webroot:
driver: local
services:
app: # Launch uwsgi application server
build:
context: ../../
dockerfile: docker/release/Dockerfile
links:
- dbc
volumes:
- webroot:/var/www/someapp
environment:
DJANGO_SETTINGS_MODULE: someapp.settings.release
MYSQL_HOST: dbc
MYSQL_USER: todo
MYSQL_PASSWORD: passwd
command:
- uwsgi
- "--socket /var/www/someapp/someapp.sock"
- "--chmod-socket=666"
- "--module someapp.wsgi"
- "--master"
- "--die-on-term"
test: # Run acceptance test cases
image: shamdockerhub/someapp-specs
links:
- nginx
environment:
URL: http://nginx:8000/todos
JUNIT_REPORT_PATH: /reports/acceptance.xml
JUNIT_REPORT_STACK: 1
command: --reporter mocha-jenkins-reporter
nginx: # Start nginx web server that forwards https packets to uwsgi server
build:
context: .
dockerfile: Dockerfile.nginx
ports:
- "8000:8000"
links:
- app
volumes:
- webroot:/var/www/someapp
dbc: # Launch MySQL server
image: mysql:5.6
hostname: dbr
expose:
- "3306"
environment:
MYSQL_DATABASE: someapp
MYSQL_USER: todo
MYSQL_PASSWORD: passwd
MYSQL_ROOT_PASSWORD: passwd
agent: # Ensure DB server is runnin
image: shamdockerhub/ansible
links:
- dbc
environment:
PROBE_HOST: "dbc"
PROBE_PORT: "3306"
command: ["probe.yml"]
在哪里输入
where entries
MYSQL_HOST: dbc
PROBE_HOST: "dbc"
看起来不直观,因为dbc
服务中的hostname
设置为dbr
does not look intuitive, because the hostname
is set to dbr
in dbc
service
1)
app
服务失败并在使用 MYSQL_HOST: dbr
app
service fails with below error on using MYSQL_HOST: dbr
django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'dbr' (0)")
2)
agent
服务也会在以下 ansible 代码中失败
agent
service also fails in below ansible code when PROBE_HOST: "dbr"
set_fact:
probe_host: "{{ lookup('env', 'PROBE_HOST') }}"
local_action: >
wait_for host={{ probe_host }}
1)
为什么这两个服务失败,值为 dbr
?
Why these two services are failing with value dbr
?
2)
如何让这两个服务与MYSQL_HOST: dbr
一起工作和 PROBE_HOST: "dbr"
?
How to make these two services work with MYSQL_HOST: dbr
and PROBE_HOST: "dbr"
?
推荐答案
这就是 Docker 的工作原理,因为 hostname
不是唯一的,如果你给两个容器提供相同的主机名,这将导致问题因此 compose 将始终使用服务名称进行 DNS 解析
that is how Docker works because the hostname
is not unique and that will lead to a problem if you give two containers the same hostname therefore compose will always use the service name for DNS resolution
这篇关于docker-compose :为什么要代理 &应用服务因主机名而失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!