容器运行无法从另一个容器进行ping通

时间:2020-01-16 13:27:21

标签: docker docker-compose

我有4个容器(A,B,C,D)要与docker-compose.yml文件一起运行

它们都使用相同的自定义网络intranet

  intranet:
    driver: bridge

我的容器A正在使用

depends_on: 
  - B
  - C
  - D

立即启动所有内容。

我做docker-compose run A,然后使用docker-compose exec进入容器B。

我可以从容器B中ping Cping D,但是当我ping A我得到

XXX@XXX:/$ ping A
    ping: A: No address associated with hostname 

我可以ping通A的唯一方法是执行docker ps,获取容器运行名称并使用它:ping projectname_A_run_1

有没有办法仅用ping A从容器B ping容器A?

version: '3.4'
services:
  A:
    image: "${DOCKER_REGISTRY_DOMAIN}/${DOCKER_BUILD_TAG_A}"
    hostname: "A"
    ports:
      - "${LISTEN:-3001}:3000"
    tty: true
    environment:
      - DEBUG
      - DOCKER_COMPOSE=1
      - MYSQL_DATABASE
      - MYSQL_PASSWORD
      - MYSQL_USER
      - NODE_VERSION
      - NVM_DIR
      - PROJECT_NAME=A
      - QT_QPA_PLATFORM=offscreen
      - RAILS_ENV
      - REDIS_URL=redis://redis:6379
      - SHELL=/bin/bash
    user: "${DOCKER_USER}"
    working_dir: "${A_WORKING_DIR}"
    volumes:
      - "./tmp/capybara:/tmp/capybara:rw"
    networks:
      - intranet
    depends_on:
      - redis
      - db
      - B
    entrypoint: "${A_ENTRYPOINT}"
  B:
    image: "${DOCKER_REGISTRY_DOMAIN}/${DOCKER_BUILD_TAG_B}"
    cap_add:
      - NET_ADMIN
      - NET_RAW
      - SETFCAP
      - SYS_PTRACE
    security_opt:
      - apparmor:unconfined
      - seccomp:unconfined
    hostname: "B"
    environment:
      - DEBUG
      - DOCKER_COMPOSE=1
      - PROJECT_NAME=B
      - REDIS_HOST=localhost
      - REDIS_PORT=6379
      - SHELL=/bin/bash
    user: "${DOCKER_USER}"
    working_dir: "${B_WORKING_DIR}"
    networks:
      - intranet
    depends_on:
      - redis
    entrypoint: "${B_ENTRYPOINT}"
  db:
    image: mysql:5.7
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
      MYSQL_USER: "${MYSQL_USER}"
    networks:
      - intranet
    volumes:
      - ./mysql/db:/var/lib/mysql:rw
      - ./mysql/initdb.d:/docker-entrypoint-initdb.d:ro
  redis:
    image: redis
    ports:
      - "${LISTEN:-6379}:6379"
    restart: unless-stopped
    networks:
      - intranet
networks:
  intranet:
    driver: bridge

1 个答案:

答案 0 :(得分:0)

找到了解决方案:

使用docker-compose run --name A A

可以使用其名称访问运行的容器