如何使用Docker Compose从一个容器获取另一个容器的主机名?

huangapple go评论110阅读模式
英文:

How to get hostname from one container to another using docker compose?

问题

我有两个Docker容器,一个是后端,另一个是数据库(postgres)。它们之间是相互链接的。我该如何在golang的Docker容器中使用后端环境的HOST变量?

据我了解,这两个容器都有各自的IP地址。我不能在golang容器中使用'localhost',因为postgres不在本地主机上,而是在一个隔离的容器中。

以下是示例的Docker Compose文件:

  1. version: "3.7"
  2. services:
  3. backend:
  4. image: golang:1.16
  5. build: ./
  6. working_dir: /app
  7. volumes:
  8. - ./backend/:/app
  9. environment:
  10. HOST: db
  11. command: go run main.go
  12. ports:
  13. - 8080:8080
  14. depends_on:
  15. - db
  16. db:
  17. image: postgres
  18. restart: always
  19. environment:
  20. POSTGRES_USER: gorm
  21. POSTGRES_PASSWORD: gorm
  22. POSTGRES_DB: gorm
  23. ports:
  24. - 9920:9920

你可以在golang代码中使用os.Getenv("HOST")来获取后端环境的HOST变量的值。这样你就可以在golang容器中使用该值来连接到postgres容器。

希望对你有帮助!如果你还有其他问题,请随时提问。

英文:

I have two docker containers. One backend, and the other db (postgres). They both are linked. How do I utilise the backend environment HOST variable in the golang docker container?

From my understanding, both containers have their own IP addresses. I cannot use 'localhost' in the golang container because postgres isn't on localhost, but in an isolated container.

  1. version: "3.7"
  2. services:
  3. backend:
  4. image: golang:1.16
  5. build: ./
  6. working_dir: /app
  7. volumes:
  8. - ./backend/:/app
  9. environment:
  10. HOST: db
  11. command: go run main.go
  12. ports:
  13. - 8080:8080
  14. depends_on:
  15. - db
  16. db:
  17. image: postgres
  18. restart: always
  19. environment:
  20. POSTGRES_USER: gorm
  21. POSTGRES_PASSWORD: gorm
  22. POSTGRES_DB: gorm
  23. ports:
  24. - 9920:9920

I've tried researching how to access this variable as well as check Docker tutorials/documentation, but haven't found a solution.

答案1

得分: 0

Docker Compose会进行DNS解析。您应该能够通过名称访问您的数据库。

移除:

  1. environment:
  2. HOST: db

将Postgres端口更正为5432:

  1. db:
  2. ...
  3. ports:
  4. - 5432:5432

您必须能够像这样连接:

  1. db := pg.Connect(&pg.Options{
  2. Addr: "db:5432",
  3. User: "gorm",
  4. Database: "gorm",
  5. Password: "gorm",
  6. })

至于环境变量,您可以这样声明和访问它们:

  1. backend:
  2. environment:
  3. POSTGRES_USER: gorm
  4. ...
  1. os.Getenv("POSTGRES_USER")
英文:

Docker compose does DNS resolution. You should be able to access your database by name.

Remove:

  1. environment:
  2. HOST: db

Correct the postgres port to 5432:

  1. db:
  2. ...
  3. ports:
  4. - 5432:5432

You must be able to connect like so:

  1. db := pg.Connect(&pg.Options{
  2. Addr: "db:5432",
  3. User: "gorm",
  4. Database: "gorm",
  5. Password: "gorm",
  6. })

As for environment variables, you can declare and access them like this:

  1. backend:
  2. environment:
  3. POSTGRES_USER: gorm
  4. ...
  1. os.Getenv("POSTGRES_USER")

答案2

得分: 0

Docker Compose将为您的容器创建一个网络,使它们可以相互通信和访问。

您可以通过向docker compose文件的服务部分添加一个名称来对其进行简单更改,以确保它们每次都获得相同的名称。

  1. version: "3.7"
  2. services:
  3. backend:
  4. image: golang:1.16
  5. build: ./
  6. container_name: backend
  7. working_dir: /app
  8. volumes:
  9. - ./backend/:/app
  10. command: go run main.go
  11. ports:
  12. - "8080:8080"
  13. depends_on:
  14. - db
  15. db:
  16. image: postgres
  17. restart: always
  18. container_name: db
  19. environment:
  20. POSTGRES_USER: gorm
  21. POSTGRES_PASSWORD: gorm
  22. POSTGRES_DB: gorm
  23. ports:
  24. - "9920:5432"

Postgres的默认端口是5432,所以我将其映射到了9920。然后,您可以通过指定db:9920来从backend容器访问db容器。

英文:

Docker compose will create a network for your containers where they can communicate and reach each other.

You can make a simple change to your docker compose file by adding a name to your services which will make sure they get the same name each time.

  1. version: "3.7"
  2. services:
  3. backend:
  4. image: golang:1.16
  5. build: ./
  6. container_name: backend
  7. working_dir: /app
  8. volumes:
  9. - ./backend/:/app
  10. command: go run main.go
  11. ports:
  12. - "8080:8080"
  13. depends_on:
  14. - db
  15. db:
  16. image: postgres
  17. restart: always
  18. container_name: db
  19. environment:
  20. POSTGRES_USER: gorm
  21. POSTGRES_PASSWORD: gorm
  22. POSTGRES_DB: gorm
  23. ports:
  24. - "9920:5432"

The default port for Postgres is 5432, so I mapped it to your 9920. Then, you can access the db container from the backend container by specifying:

db:9920

huangapple
  • 本文由 发表于 2021年8月17日 07:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/68810174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定