繁体   English   中英

无法使用 docker-compose 和 podman 解析主机名

[英]Unable to resolve hostname with docker-compose and podman

我正在尝试使用此项目部署乳齿象服务器: https://github.com/tootsuite/mastodon

我在 Fedora 33 服务器上运行 Docker-Compose 和 Podman。

$ docker-compose --version
docker-compose version 1.27.4, build unknown

$ docker --version
podman version 3.0.1

$ cat /etc/fedora-release
Fedora release 33 (Thirty Three)

我必须对 docker-compose.yml 进行一些更改才能使其与 Podman 一起使用。 你可以在下面看到我的整个配置文件。

version: '3'
services:

  db:
    restart: always
    image: postgres:9.6-alpine
    shm_size: 256mb
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      timeout: 45s
      interval: 10s
      retries: 10
    volumes:
      - ./postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

  redis:
    restart: always
    image: redis:6.0-alpine
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      timeout: 45s
      interval: 10s
      retries: 10
    volumes:
      - ./redis:/data

#  es:
#    restart: always
#    image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
#    environment:
#      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
#      - "cluster.name=es-mastodon"
#      - "discovery.type=single-node"
#      - "bootstrap.memory_lock=true"
#    networks:
#      - internal_network
#    healthcheck:
#      test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
#    volumes:
#      - ./elasticsearch:/usr/share/elasticsearch/data
#    ulimits:
#      memlock:
#        soft: -1
#        hard: -1

  web:
    #    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
    networks:
      - external_network
      - internal_network
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
      timeout: 45s
      interval: 10s
      retries: 10
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - db
      - redis
#      - es
    volumes:
      - ./public/system:/mastodon/public/system

  streaming:
    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: node ./streaming
    networks:
      - external_network
      - internal_network
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
      timeout: 45s
      interval: 10s
      retries: 10
    ports:
      - "127.0.0.1:4000:4000"
    depends_on:
      - db
      - redis

  sidekiq:
    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: bundle exec sidekiq
    depends_on:
      - db
      - redis
    networks:
      - external_network
      - internal_network
    volumes:
      - ./public/system:/mastodon/public/system
## Uncomment to enable federation with tor instances along with adding the following ENV variables
## http_proxy=http://privoxy:8118
## ALLOW_ACCESS_TO_HIDDEN_SERVICE=true
#  tor:
#    image: sirboops/tor
#    networks:
#      - external_network
#      - internal_network
#
#  privoxy:
#    image: sirboops/privoxy
#    volumes:
#      - ./priv-config:/opt/config
#    networks:
#      - external_network
#      - internal_network

networks:
  external_network:
  internal_network:
    internal: true

这是存储库上文件的远程版本的差异:

(tl;dr:我添加了健康检查选项和一个 env 变量以授权在没有密码的情况下运行 postgres,并评论了构建选项以使用来自 repo 的图像,因为构建也失败了)

$ git diff docker-compose.yml
diff --git a/docker-compose.yml b/docker-compose.yml
index 52eea7a74..a8e047ec7 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -9,8 +9,13 @@ services:
       - internal_network
     healthcheck:
       test: ["CMD", "pg_isready", "-U", "postgres"]
+      timeout: 45s
+      interval: 10s
+      retries: 10
     volumes:
       - ./postgres:/var/lib/postgresql/data
+    environment:
+      - POSTGRES_HOST_AUTH_METHOD=trust

   redis:
     restart: always
@@ -19,6 +24,9 @@ services:
       - internal_network
     healthcheck:
       test: ["CMD", "redis-cli", "ping"]
+      timeout: 45s
+      interval: 10s
+      retries: 10
     volumes:
       - ./redis:/data

@@ -42,7 +50,7 @@ services:
 #        hard: -1

   web:
-    build: .
+    #    build: .
     image: tootsuite/mastodon
     restart: always
     env_file: .env.production
@@ -52,6 +60,9 @@ services:
       - internal_network
     healthcheck:
       test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
+      timeout: 45s
+      interval: 10s
+      retries: 10
     ports:
       - "127.0.0.1:3000:3000"
     depends_on:
@@ -72,6 +83,9 @@ services:
       - internal_network
     healthcheck:
       test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
+      timeout: 45s
+      interval: 10s
+      retries: 10
     ports:
       - "127.0.0.1:4000:4000"
     depends_on:

生成秘密很好,但是在这个命令上失败了:

$ sudo docker-compose run --rm web bundle exec rails db:migrate
Creating network "mastodon_internal_network" with the default driver
Creating network "mastodon_external_network" with the default driver
Creating mastodon_db_1    ... done
Creating mastodon_redis_1 ... done
Creating mastodon_web_run ... done
rails aborted!
PG::ConnectionBad: could not translate host name "db" to address: Name or service not known

我已经在几个项目中使用了 Docker-Compose 和 Podman 3.0 的组合,并且在网络内部解析主机名时我从来没有遇到任何问题。 我想知道是否必须为这种情况指定驱动程序。

另外,我想要一种方法来测试我是否可以从 web 的容器中使用此主机名访问数据库服务,因此,如果问题出在代码中(我非常怀疑,但我想确定)。

EDIT1: db 服务日志显示该服务似乎运行良好并准备好接受连接

$ sudo docker logs -f mastodon_db_1

PostgreSQL Database directory appears to contain a database; Skipping initialization

LOG:  database system was shut down at 2021-04-01 07:02:04 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

我找到了一个解决方案:删除网络的定义。

这听起来很便宜,但它确实有效。

所以最终的docker-compose.yml看起来像这样:

version: '3'
services:

  db:
    restart: always
    image: postgres:9.6-alpine
    shm_size: 256mb
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      timeout: 45s
      interval: 10s
      retries: 10
    volumes:
      - ./postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

  redis:
    restart: always
    image: redis:6.0-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      timeout: 45s
      interval: 10s
      retries: 10
    volumes:
      - ./redis:/data

#  es:
#    restart: always
#    image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
#    environment:
#      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
#      - "cluster.name=es-mastodon"
#      - "discovery.type=single-node"
#      - "bootstrap.memory_lock=true"
#    networks:
#      - internal_network
#    healthcheck:
#      test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
#    volumes:
#      - ./elasticsearch:/usr/share/elasticsearch/data
#    ulimits:
#      memlock:
#        soft: -1
#        hard: -1

  web:
    #    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
      timeout: 45s
      interval: 10s
      retries: 10
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - db
      - redis
#      - es
    volumes:
      - ./public/system:/mastodon/public/system

  streaming:
    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: node ./streaming
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
      timeout: 45s
      interval: 10s
      retries: 10
    ports:
      - "127.0.0.1:4000:4000"
    depends_on:
      - db
      - redis

  sidekiq:
    build: .
    image: tootsuite/mastodon
    restart: always
    env_file: .env.production
    command: bundle exec sidekiq
    depends_on:
      - db
      - redis
    volumes:
      - ./public/system:/mastodon/public/system
## Uncomment to enable federation with tor instances along with adding the following ENV variables
## http_proxy=http://privoxy:8118
## ALLOW_ACCESS_TO_HIDDEN_SERVICE=true
#  tor:
#    image: sirboops/tor
#    networks:
#      - external_network
#      - internal_network
#
#  privoxy:
#    image: sirboops/privoxy
#    volumes:
#      - ./priv-config:/opt/config
#    networks:
#      - external_network
#      - internal_network

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM