Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
hcgprague opened this issue Dec 9, 2020 · 3 comments
Open

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

hcgprague opened this issue Dec 9, 2020 · 3 comments

Comments

@hcgprague
Copy link

hcgprague commented Dec 9, 2020

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

@twtrubiks
Copy link
Owner

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

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

@hcgprague
Copy link
Author

hcgprague commented Dec 11, 2020 via email

@twtrubiks
Copy link
Owner

我在 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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants