簡體   English   中英

如何在Docker中設置服務主機?

[英]How to set host of service in Docker?

我正在嘗試在TeamCity Docker代理上測試我的應用程序。 應用程序在gradle中運行內部docker。

這是我docker-compose.yml

version: '3'
services:
  db:
    image:
        docker.int.address.com/postgres:9.6
    ports:
        - 5432:5432
    volumes:
        - ./init.sql:/docker-entrypoint-initdb.d/
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
    networks:
      my_net:
        ipv4_address: 172.19.0.3
networks:
  my_net:
    ipam:
      driver: default
      config:
        - subnet: 172.19.0.0/24

代理的輸出:

[13:13:59][docker] Successfully started process 'command 'docker-compose''
[13:14:00][docker] Some networks were defined but are not used by any service: my_net
[13:14:00][docker] db uses an image, skipping
[13:14:00][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml --version
[13:14:00][docker] Successfully started process 'command 'docker-compose''
[13:14:00][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml --version
[13:14:00][docker] Successfully started process 'command 'docker-compose''
[13:14:01][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml up -d
[13:14:01][docker] Successfully started process 'command 'docker-compose''
[13:14:01][docker] Some networks were defined but are not used by any service: my_net
[13:14:02][docker] Recreating checkout_db_1 ...
[13:14:02][docker] Recreating checkout_db_1
[13:14:03][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml --version
[13:14:03][docker] Successfully started process 'command 'docker-compose''
[13:14:04][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml config --services
[13:14:04][docker] Successfully started process 'command 'docker-compose''
[13:14:04][docker] Starting process 'command 'docker-compose''. Working directory: /checkout Command: docker-compose -f /checkout/docker-compose.yml ps -q db
[13:14:04][docker] Successfully started process 'command 'docker-compose''
[13:14:05][docker] [1A [2K
[13:14:05][docker] Recreating checkout_db_1 ... done
[13:14:05][docker] [1BSome networks were defined but are not used by any service: my_net
[13:14:05][docker] Container ID of service db is fc3d436189aa1e455a7f4e8524f99605e87ce72b214844097534cbf4a89a4d77
[13:14:05][docker] Starting process 'command 'docker''. Working directory: /checkout Command: docker inspect fc3d436189aa1e455a7f4e8524f99605e87ce72b214844097534cbf4a89a4d77
[13:14:05][docker] Successfully started process 'command 'docker''
**[13:14:06][docker] Will use 172.17.0.1 (network bridge) as host of db**
[13:14:06][docker] Will use com.address.gradle.dockercompose.ServiceHost(172.17.0.1, NetworkGateway) as host of service db
[13:14:06][docker] Exposed TCP port on service 'db:5432' will be available as 5432
[13:14:06][docker] Starting process 'command 'docker''. Working directory: /checkout Command: docker inspect fc3d436189aa1e455a7f4e8524f99605e87ce72b214844097534cbf4a89a4d77
[13:14:06][docker] Successfully started process 'command 'docker''
[13:14:06][docker] Probing TCP socket on 172.17.0.1:5432 of service 'db_1'
[13:16:14][docker] Waiting for TCP socket on 172.17.0.1:5432 of service 'db_1' (Connection timed out (Connection timed out))
[13:18:22][docker] Waiting for TCP socket on 172.17.0.1:5432 of service 'db_1' (Connection timed out (Connection timed out))
[13:20:30][docker] Waiting for TCP socket on 172.17.0.1:5432 of service 'db_1' (Connection timed out (Connection timed out))
[13:22:38][docker] Waiting for TCP socket on 172.17.0.1:5432 of service 'db_1' (Connection timed out (Connection timed out))
[13:24:47][docker] Waiting for TCP socket on 172.17.0.1:5432 of service 'db_1' (Connection timed out (Connection timed out))

問題是圖像在172.19.0.xx3:5432上,但是到達了172.19.0.1:5432

我應該在docker-compose.yml以便它可以獲取正確的主機?

在應用程序服務中,該服務與刪除主機聯系,放置遠程主機的名稱。 啟用這兩種服務,然后在控制台上運行docker ps 數據庫服務的名稱為somedirectory_db 將其作為連接字符串的一部分放在您的應用程序邏輯中; 例:

DSN=host:somdirectory_db;port=3306;schema=some_database

本質上,如果兩個服務都在同一網絡上,則可以將服務名稱用作可解析的DSN地址。

參考: 如何通過名稱而不是IP地址訪問Docker容器?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM