docker-swarm-tutorial icon indicating copy to clipboard operation
docker-swarm-tutorial copied to clipboard

為什麼web這樣的config 能知道要連到哪一個db

Open hcgprague opened this issue 3 years ago • 3 comments

swarm 時 為什麼web這樣的config 能知道要連到哪一個db (ip:port) , web 連線db 的設定值並沒有看見?

web: image: twtrubiks/my_swarm_secrets_demo ports: - "8000:8000" networks: - backend deploy: replicas: 4 update_config: parallelism: 2 restart_policy: condition: on-failure secrets: - my_password depends_on: - db

hcgprague avatar Dec 09 '20 01:12 hcgprague

因為 docker 可以透過容器的 service 連線到對應的 db, 我這邊剛好定義為 "db". 可參考 https://github.com/twtrubiks/docker-tutorial#user-defined-networks

透過內建的 DNS 伺服器,可以直接使用容器名稱,就可解析出 IP,不需要再使用 IP 讓容器互相 溝通,我們只需要知道容器的名稱就可以連接到容器。

twtrubiks avatar Dec 11 '20 03:12 twtrubiks

那 web 裡 有支program code 要query 某個db , 要怎麼寫連線字串 ? 沒有db 的 ip ?

twtrubiks [email protected] 於 2020年12月11日 週五 上午11:50寫道:

因為 docker 可以透過容器的 service 連線到對應的 db, 我這邊剛好定義為 "db". 可參考 https://github.com/twtrubiks/docker-tutorial#user-defined-networks

透過內建的 DNS 伺服器,可以直接使用容器名稱,就可解析出 IP,不需要再使用 IP 讓容器互相 溝通,我們只需要知道容器的名稱就可以連接到容器。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/twtrubiks/docker-swarm-tutorial/issues/4#issuecomment-742949700, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWJVNZHRMK2EYEHL7B7U7DSUGJGTANCNFSM4USZLKPQ .

-- Huang Jie Qi _ JKHCG

hcgprague avatar Dec 11 '20 04:12 hcgprague

我在 https://github.com/twtrubiks/docker-swarm-tutorial/blob/master/docker-swarm-secrets/api/django_rest_framework_tutorial/settings.py 這邊也是直接填入容器的名稱 (db).

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': secret('my_password'),
        'HOST': 'db',
        'PORT': 5432,
    }
}

不知道你問的是不是這個?

假如你有很多db, 理論上應該也會透過一些方法同步每一顆的db, 所以應該隨便拿一顆(應該 host 直接填入 db 即可). 再深入一點的我就沒有玩過了, 可能要 google 一下QQ

twtrubiks avatar Dec 12 '20 07:12 twtrubiks