以下是v2规范中的工作docker-compose文件:
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"]
条目在哪里
MYSQL_HOST: dbc PROBE_HOST: "dbc"
看起来不直观,因为hostname设置为dbr在dbc使用中
hostname
dbr
dbc
1)
app 服务失败,并出现以下使用错误 MYSQL_HOST: dbr
app
MYSQL_HOST: dbr
django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'dbr' (0)")
2)
agent 当以下代码时,服务也会失败 PROBE_HOST: "dbr"
agent
PROBE_HOST: "dbr"
set_fact: probe_host: "{{ lookup('env', 'PROBE_HOST') }}" local_action: > wait_for host={{ probe_host }}
为什么这两项服务的价值都在下降dbr?
如何使这两个服务与MYSQL_HOST: dbr 和一起使用 PROBE_HOST: "dbr"?
这就是Docker的工作方式,因为hostname并不是唯一的,如果给两个容器提供相同的主机名,则会导致问题,因此compose将始终使用服务名称进行DNS解析