Docker Compose: mai i te whanaketanga ki te hanga

I whakaritea te whakamāoritanga o te tuhinga o te podcast hei whakarite mō te whakarewatanga o te akoranga. "Kaiwhakahaere Linux"

Docker Compose: mai i te whanaketanga ki te hanga

He taputapu whakamiharo a Docker Compose mō te waihanga i tētahi taiao mahi.
te taiao mō te puranga e whakamahia ana i roto i tō tono. Mā tēnei ka taea e koe te tautuhi
ia wāhanga o tō tono, me te whai i tētahi wetereo mārama, ngāwari hoki i roto i YAML-
ngā kōnae
.

Tuhinga o mua Docker Compose v3 Ka taea te whakamahi tika i ēnei kōnae YAML i roto i te taiao whakaputa ina mahi tahi ana me
rōpū Pokai Docker.

Engari ko te tikanga o tēnei ka taea e koe te whakamahi i te kōnae docker-compose kotahi i roto i
te tukanga whakawhanake me te taiao whakaputa? Me whakamahi rānei i te kōnae kotahi mō
Te whakatakoto i te atamira? Āe, i te nuinga o te wā, engari mō tēnei mahi me whai tātou i ēnei e whai ake nei:

  • Te Whakauru Taurangi: Te Whakamahi i ngā Taurangi Taiao mō ētahi
    ngā uara e rerekē ana i roto i ia taiao.
  • te whakakorenga whirihoranga: te kaha ki te tautuhi i tētahi hēkona (tētahi atu rānei)
    tētahi atu kōnae docker-compose e whai ake nei ka huri i tētahi mea
    tuatahi, ā, mā te docker compose e whakakotahi ngā kōnae e rua.

Ngā rerekētanga i waenga i ngā kōnae whakawhanake me ngā kōnae whakaputa

I te wā whakawhanake, tera pea ka hiahia koe ki te tirotiro i ngā huringa waehere i roto
i te wā tūturu. Mō tēnei, ko te rōrahi waehere pūtake ka tāutahia ki roto
he ipu kei roto te wā whakahaere mō tō tono. Engari mō te taiao whakaputa
Kāore tēnei tikanga i te tika.

I roto i te whakaputanga, he tautau tōu me ngā pūnga maha, ā, ko te rōrahi he rohe nā
e pā ana ki te pūnga e whakahaerehia ana tō ipu (ratonga rānei), nō reira kāore koe e mahi
Ka taea e koe te tāuta i te waehere pūtake me te kore he mahi uaua e uru ana ki
te tukutahitanga waehere, ngā tohu, me ētahi atu

Engari, ko te tikanga he waihanga i tētahi ahua me tētahi putanga motuhake o tō waehere.
He tikanga kia tohua ki tētahi tohu tika (ka taea e koe te whakamahi i te tikanga
putanga, tētahi atu pūnaha rānei kei a koe te whakaaro).

Te whakakore i te whirihoranga

Whakaarohia ngā rerekētanga me te rerekētanga pea o ō whakawhirinakitanga i roto i ngā horopaki
te whakawhanaketanga me te whakaputanga, e mārama ana ka hiahiatia he kōnae whirihoranga rerekē.

Ka tautoko a Docker compose i te whakakotahi i ngā kōnae tito rerekē hei
te whiwhi i te whirihoranga whakamutunga. Ka kitea te mahi a tēnei i roto i te tauira:

$ cat docker-compose.yml
version: "3.2"

services:
  whale:
    image: docker/whalesay
    command: ["cowsay", "hello!"]
$ docker-compose up
Creating network "composeconfigs_default" with the default driver
Starting composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ________
whale_1  | < hello! >
whale_1  |  --------
whale_1  |     
whale_1  |      
whale_1  |       
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        ______ o          __/
whale_1  |                     __/
whale_1  |           __________/
composeconfigs_whale_1 exited with code 0

E ai ki te kōrero, ka tautoko a docker compose i te whakakotahi i ngā momo compose maha.
ngā kōnae, mā tēnei ka taea e koe te whakakore i ngā tawhā rerekē i roto i te kōnae tuarua. Hei tauira:

$ cat docker-compose.second.yml
version: "3.2"
services:
  whale:
    command: ["cowsay", "bye!"]

$ docker-compose -f docker-compose.yml -f docker-compose.second.yml up
Creating composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ______
whale_1  | < bye! >
whale_1  |  ------
whale_1  |     
whale_1  |      
whale_1  |       
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        ______ o          __/
whale_1  |                     __/
whale_1  |           __________/
composeconfigs_whale_1 exited with code 0

Kāore tēnei wetereo i te tino watea i te wā o te tukanga whakawhanake ina whakamahia te whakahau
me mahi kia maha ngā wā.

Waimarie, ka rapu aunoa a docker compose i tētahi kōnae motuhake e kiia nei ko
docker-compose.override.yml hei whakakore i ngā uara docker-compose.yml. Mena
Ki te whakaingoatia anō e koe te kōnae tuarua, ka whiwhi koe i te hua ōrite, mā te whakamahi noa i te whakahau taketake:

$ mv docker-compose.second.yml docker-compose.override.yml
$ docker-compose up
Starting composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ______
whale_1  | < bye! >
whale_1  |  ------
whale_1  |     
whale_1  |      
whale_1  |       
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        ______ o          __/
whale_1  |                     __/
whale_1  |           __________/
composeconfigs_whale_1 exited with code 0

Āe, mā tēnā ka māmā ake te maumahara.

Te whakauru i ngā taurangi

Tautoko kōnae whirihoranga whakaurunga
taurangi
me ngā uara taunoa. Arā, ka taea e koe te mahi i ēnei e whai ake nei:

services:
  my-service:
    build:
      context: .
    image: private.registry.mine/my-stack/my-service:${MY_SERVICE_VERSION:-latest}
...

Ā, ki te mahi koe hanga (tēnā rānei pana) te docker-compose kāore he taurangi taiao
$MY_SERVICE_PUtanga, ka whakamahia te uara hou, engari ki te tāuta koe
te uara o te taurangi taiao i mua i te hanganga, ka whakamahia i te wā o te hanganga, i te pana rānei
ki te rēhita tūmataiti.rēhita.mine.

Aku mātāpono

Tērā pea ka whai hua anō hoki ngā huarahi e pai ana ki ahau ki a koe. Ka whai ahau i ēnei
ngā ture māmā:

  • Ko aku puranga katoa mō te whakaputa, te whakawhanake (me ētahi atu taiao rānei) kua tautuhia mā roto i
    ngā kōnae tito-docker.
  • Ko ngā kōnae whirihoranga e hiahiatia ana hei hipoki i aku taiao katoa koia ēnei
    karohia te tāruarua.
  • Kotahi te whakahau māmā e hiahiatia ana e au hei mahi i roto i ngā taiao katoa.
  • Kua tautuhia te whirihoranga matua i roto i te kōnae docker-compose.yml.
  • Ka whakamahia ngā taurangi taiao hei tautuhi i ngā tūtohu whakaahua, i ētahi atu rānei
    ngā taurangi ka rerekē pea i waenga i te taiao ki te taiao (te whakarōpūtanga, te whakaurunga,
    te whakaputanga).
  • Ko ngā uara o ngā taurangi mō te whakaputanga ka whakamahia hei uara e
    mā te taunoa, ka whakaitihia ngā mōrearea mēnā ka whakahaerehia te puranga i te whakaputanga me te kore
    tautuhia te taurangi taiao.
  • Hei whakarewa i te ratonga i roto i te taiao whakaputa, whakamahia te whakahau te whakatakoto i te puranga docker - te kōnae-whakakotahi docker-compose.yml -me-te-rēhita-whakamana ingoa-tōku-pūranga.
  • Ka whakarewahia te taiao mahi mā te whakamahi i te whakahau te tautuhi-te hanga -d.

Me titiro tātou ki tētahi tauira māmā.

# docker-compose.yml
...
services:
  my-service:
    build:
      context: .
    image: private.registry.mine/my-stack/my-service:${MY_SERVICE_VERSION:-latest}
    environment:
      API_ENDPOINT: ${API_ENDPOINT:-https://production.my-api.com}
...

И

# docker-compose.override.yml
...
services:
  my-service:
    ports: # This is needed for development!
      - 80:80
    environment:
      API_ENDPOINT: https://devel.my-api.com
    volumes:
      - ./:/project/src
...

Ka taea e au te whakamahi tito-docker (tito-docker)hei tīmata i te puranga i roto i
aratau whanaketanga me te waehere pūtake kua whakauruhia ki roto /kaupapa/src.

Ka taea e au te whakamahi i ēnei kōnae i roto i te whakaputa! Ā, ka taea e au te whakamahi tika
te kōnae kotahi docker-compose.yml mō te whakaaturanga. Hei whakatinana i tēnei ki
te whakaputa, me hanga noa e au, me tuku atu hoki he whakaahua me tētahi tohu kua tautuhia
i te wāhanga CI:

export MY_SERVICE_VERSION=1.2.3
docker-compose -f docker-compose.yml build
docker-compose -f docker-compose.yml push

I te whakaputanga, ka taea te whakahaere i tēnei mā te whakamahi i ngā whakahau e whai ake nei:

export MY_SERVICE_VERSION=1.2.3
docker stack deploy my-stack --compose-file docker-compose.yml --with-registry-auth

Ā, ki te hiahia koe ki te mahi i taua mea anō i runga i te atamira, me tautuhi noa koe
Ngā taurangi taiao e tika ana mō te mahi i roto i te taiao whakaaturanga:

export MY_SERVICE_VERSION=1.2.3
export API_ENDPOINT=http://staging.my-api.com
docker stack deploy my-stack --compose-file docker-compose.yml --with-registry-auth

I mutu ake i whakamahia e mātou ētahi kōnae docker-compose e rua, kāore nei
Ka taea te whakamahi i ngā tāruarua whirihoranga mō tetahi o ō taiao!

Ako atu mo te akoranga "Kaiwhakahaere Linux"

Source: will.com

Tāpiri i te kōrero