本文介绍了docker-compose:为什么选择代理&应用服务因主机名而失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是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服务也会在下面的错误代码中失败

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)


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: dbrand 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:为什么选择代理&应用服务因主机名而失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:06