【问题标题】:Docker-compose not adding dependent container to /etc/hostsDocker-compose 未将依赖容器添加到 /etc/hosts
【发布时间】:2020-05-11 03:07:35
【问题描述】:

未添加依赖主机名,这是我的 Web 应用程序连接到我的其他服务 (postgres) 所必需的

这是我/etc/hosts的一个例子

root@bdd6f863aced:/myapp# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.21.0.3      bdd6f863aced

最后一个条目是当前容器,而不是 postgres 容器

重复步骤:

git clone repo git@github.com:tushartuteja/rails-docker-compose-example.git
cd rails-docker-compose-example
docker-compose up --build
docker ps # get the id for the web container
docker exec -it {host-container id} /bin/bash
rake db:create
cat /etc/hosts

当您尝试创建时,您会收到以下错误消息:

PG::ConnectionBad: could not translate host name "db" to address: Name or service not known
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `connect'

docer-compose

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    links:
      - db

此处的其他信息: https://github.com/docker/compose/issues/7442

【问题讨论】:

    标签: ruby-on-rails docker-compose


    【解决方案1】:

    我想你忘记了links 条目,这对你有帮助

    【讨论】:

    • 我有过,也有过。结果是一样的。
    • 所以,正如我在 github 上看到的问题,您的 docker-compose 文件看起来很奇怪。你能把它贴在这里吗?
    • 我添加了我的 docker-compose.yaml @Vasiliy Novikov
    • 首先 - 你的数据库容器没有启动(我可以重现)Database is uninitialized and superuser password is not specified,你可以通过 docker-compose logs docker-compose logs db 看到它。下一个 - 您可以使用 docker-compose 轻松进入 bash 控制台,而不是使用普通 docker docker-compose exec web bash
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多