可以在Docker网络上使用什么名称(如果可能)作为容器?

时间:2019-01-06 01:42:14

标签: docker networking

将Docker与Rancher v1.6结合使用,并在容器内部使用phpMyAdmin,并使用其自身的堆栈创建一个名为db-admin的网络,甚至当我向每个MySQL容器添加别名时,能够访问,其别名不会解析为容器IP地址。

示例

docker-compose.yml(Gogs):

version: '2'

services:
  gogs:
    image: gogs/gogs:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      RUN_CROND: true
      SOCAT_LINK: false # Being used inside of Rancher
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      {{- if .Values.TRAEFIK_HOST}}
      traefik.enable: true
      ### Start SSH Segment
      traefik.ssh.frontend.entryPoints: ssh
      traefik.ssh.frontend.headers.forceSTSHeader: true
      traefik.ssh.frontend.headers.SSLRedirect: true
      traefik.ssh.frontend.headers.STSPreload: true
      traefik.ssh.frontend.headers.STSSeconds: 15552000
      traefik.ssh.frontend.rule: Host:${TRAEFIK_HOST}
      traefik.ssh.port: "22"
      ### End SSH Segment
      ### Start Web Segment
      traefik.web.frontend.entryPoints: http,https
      traefik.web.frontend.headers.forceSTSHeader: true
      traefik.web.frontend.headers.SSLRedirect: true
      traefik.web.frontend.headers.STSPreload: true
      traefik.web.frontend.headers.STSSeconds: 15552000
      traefik.web.frontend.rule: Host:${TRAEFIK_HOST}
      traefik.web.port: "3000"
      ### End Web Segment
      {{- else}}
      traefik.enable: false
      {{- end}}
    links:
      - mysql
    networks:
      - public-proxy
    ports:
      - "${SSH_PORT}:22"
      - "${WEB_PORT}:3000"
    restart: on-failure
    volumes:
      - /etc/localtime:/etc/localtime:ro # Syncronize time of container with the host system
      - /etc/timezone:/etc/timezone:ro # Syncronize timezone of container with the host system
      - /RancherCattle/${DATA_DIR}/Data:/data
  mysql:
    image: mysql:5
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      MYSQL_DATABASE: gogs # Will eventually rename this to "gogs_db"
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_USER: gogs_user
      MYSQL_PASSWORD: ${DB_USER_PASS}
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
    networks:
      db-admin:
        aliases:
          - gogs
    restart: on-failure
    volumes:
      - /etc/localtime:/etc/localtime:ro # Syncronize time of container with the host system
      - /etc/timezone:/etc/timezone:ro # Syncronize timezone of container with the host system
      - /RancherCattle/${DATA_DIR}/Database:/var/lib/mysql

networks:
  db-admin:
    external: true
  public-proxy:
    external: true

(SSH入口点无法正常工作,这是另一个问题XD)

docker-compose.yml(phpMyAdmin):

version: '2'

services:
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      PMA_ARBITRARY: 1 # Allows the user to specify any arbitrary server using address/hostname and port
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      {{- if .Values.TRAEFIK_HOST}}
      traefik.enable: true
      ### Start Web Segment
      traefik.web.frontend.entryPoints: http,https
      traefik.web.frontend.headers.forceSTSHeader: true
      traefik.web.frontend.headers.SSLRedirect: true
      traefik.web.frontend.headers.STSPreload: true
      traefik.web.frontend.headers.STSSeconds: 15552000
      traefik.web.frontend.redirect.entryPoint: https
      traefik.web.frontend.redirect.permanent: true
      traefik.web.frontend.rule: Host:${TRAEFIK_HOST}
      traefik.web.port: "80"
      ### End Web Segment
      {{- else}}
      traefik.enable: false
      {{- end}}
    networks:
      - db-admin # Used to be able to make secure, direct connections to other services in other stacks
      - public-proxy # Used for the connection to the Traefik container for public access
    ports:
      - "${WEB_PORT}:80"
    restart: on-failure

networks:
  db-admin:
  public-proxy:
    external: true

我希望我可以在phpMyAdmin的主机名字段中使用“ gogs”来引用Gogs的MySQL容器,但是phpMyAdmin显示错误,表明它无法解析该名称。我可以使用容器的IP地址(如Rancher v1.6界面所示),但如果可能的话,希望有一种“更简单”的引用方式。

谢谢!

1 个答案:

答案 0 :(得分:0)

服务名称由compose和swarm配置为网络别名。如果一个DNS查询有多个匹配项,您将看到的是每个查询的各种匹配项之间的轮循。

鉴于此,您具有gogs的服务名称,以及一个作为gogs的mysql服务的别名。结果是一半的DNS查询将首先解析为gogs服务,另一半解析为mysql服务。这将导致意外的行为。您应该更改gogs服务的名称,或为mysql别名使用其他名称。