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

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

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

问题

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

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

以下是示例的Docker Compose文件:

version: "3.7"

services:
  backend:
    image: golang:1.16
    build: ./
    working_dir: /app
    volumes:
      - ./backend/:/app
    environment: 
      HOST: db
    command: go run main.go
    ports:
      - 8080:8080
    depends_on: 
      - db

  db:
    image: postgres
    restart: always
    environment: 
      POSTGRES_USER: gorm
      POSTGRES_PASSWORD: gorm
      POSTGRES_DB: gorm
    ports:
      - 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.

version: "3.7"

services:

backend:
  image: golang:1.16
  build: ./
  working_dir: /app
  volumes:
    - ./backend/:/app
  environment: 
    HOST: db
  command: go run main.go
  ports:
    - 8080:8080
  depends_on: 
    - db

db:
  image: postgres
  restart: always
  environment: 
    POSTGRES_USER: gorm
    POSTGRES_PASSWORD: gorm
    POSTGRES_DB: gorm
  ports:
    - 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解析。您应该能够通过名称访问您的数据库。

移除:

environment: 
  HOST: db

将Postgres端口更正为5432:

db:
  ...
  ports:
    - 5432:5432

您必须能够像这样连接:

db := pg.Connect(&pg.Options{
	Addr:     "db:5432",
	User:     "gorm",
	Database: "gorm",
	Password: "gorm",
})

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

backend:
  environment:
    POSTGRES_USER: gorm
    ...
os.Getenv("POSTGRES_USER")
英文:

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

Remove:

environment: 
  HOST: db

Correct the postgres port to 5432:

db:
  ...
  ports:
    - 5432:5432

You must be able to connect like so:

db := pg.Connect(&pg.Options{
	Addr:     "db:5432",
	User:     "gorm",
	Database: "gorm",
	Password: "gorm",
})

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

backend:
  environment:
    POSTGRES_USER: gorm
    ...
os.Getenv("POSTGRES_USER")

答案2

得分: 0

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

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

version: "3.7"

services:

  backend:
    image: golang:1.16
    build: ./
    container_name: backend
    working_dir: /app
    volumes:
      - ./backend/:/app
    command: go run main.go
    ports:
      - "8080:8080"
    depends_on: 
      - db

  db:
    image: postgres
    restart: always
    container_name: db
    environment: 
      POSTGRES_USER: gorm
      POSTGRES_PASSWORD: gorm
      POSTGRES_DB: gorm
    ports:
      - "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.

version: "3.7"

services:

  backend:
    image: golang:1.16
    build: ./
    container_name: backend
    working_dir: /app
    volumes:
      - ./backend/:/app
    command: go run main.go
    ports:
      - "8080:8080"
    depends_on: 
      - db

  db:
    image: postgres
    restart: always
    container_name: db
    environment: 
      POSTGRES_USER: gorm
      POSTGRES_PASSWORD: gorm
      POSTGRES_DB: gorm
    ports:
      - "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:

确定