ЁЯПл The SchoolтА║ЁЯН▒ DockerтА║ЁЯН╜я╕ПЁЯН╜я╕П рдзрдбрд╛ 06 тАФ Docker Compose: рдПрдХрд╛ command рдиреЗ рд╕рдВрдкреВрд░реНрдг рдЯреЗрдмрд▓ рдорд╛рдВрдбрд╛
ЁЯЦ╝я╕П See the drawing + lab ЁЯПа Course home ЁЯМ┐ Branch on GitHub тЬПя╕П View source
ЁЯЦ╝я╕П рдЖрдХреГрддреА рдЖрдгрд┐ labThe drawing + lab рдкреВрд░реНрдг рдкрд╛рдирд╛рд╡рд░ рдЙрдШрдбрд╛ тЖЧOpen full page тЖЧ

ЁЯН╜я╕ПЁЯН╜я╕П рдзрдбрд╛ 06 тАФ Docker Compose: рдПрдХрд╛ command рдиреЗ рд╕рдВрдкреВрд░реНрдг рдЯреЗрдмрд▓ рдорд╛рдВрдбрд╛

ЁЯУН рддреБрдореНрд╣реА рдЗрдереЗ рдЖрд╣рд╛рдд: 12 рдкреИрдХреА рдзрдбрд╛ 06 ┬╖ рдорд╛рдЧреЗ: lesson-05-volumes-networks ┬╖ рдкреБрдвреЗ: lesson-07-multi-stage


ЁЯУж рдпрд╛ рдмреНрд░рдБрдЪрдордзреНрдпреЗ рдХрд╛рдп рдЖрд╣реЗ

рдзрдбреЗ 01тАУ05, рдЖрдгрд┐ рддреНрдпрд╛рд╢рд┐рд╡рд╛рдп: Compose тАФ рдзрдбрд╛ 05 рдордзрд▓реЗ рд╕рдЧрд│реЗ, flags рдореНрд╣рдгреВрди рдЯрд╛рдЗрдк рдХрд░рдгреНрдпрд╛рдРрд╡рдЬреА рдПрдХрд╛ file рдордзреНрдпреЗ рдЬрд╛рд╣реАрд░ рдХреЗрд▓реЗрд▓реЗ. рдЦрд▒реНрдпрд╛ files:

ЁЯзТ 5 рд╡рд░реНрд╖рд╛рдВрдЪреНрдпрд╛ рдореБрд▓рд╛рд▓рд╛ рд╕рдордЬрд╛рд╡рд▓реНрдпрд╛рд╕рд╛рд░рдЦреЗ

рдХрд╛рд▓ рддреБрдореНрд╣реА рдЬреЗрд╡рдгрд╛рдЪреЗ рдЯреЗрдмрд▓ рд╣рд╛рддрд╛рдиреЗ рдорд╛рдВрдбрд▓реЗ: network рдмрдирд╡рд╛, рдбрдмрд╛ 1 рд╣реЗ flags рджреЗрдКрди рдЪрд╛рд▓рд╡рд╛, рдбрдмрд╛ 2 рддреЗ flags рджреЗрдКрди рдЪрд╛рд▓рд╡рд╛, fridge рдЬреЛрдбрд╛тАж 15 рдорд┐рдирд┐рдЯреЗ flags рдЯрд╛рдЗрдк рдХрд░рдгреЗ, рдЖрдгрд┐ рддреБрдордЪреА рд╕рд╣рдХрд╛рд░реА рддреЗ рдереЛрдбреЗ рд╡реЗрдЧрд│реЗ рдХрд░рддреЗ рдЖрдгрд┐ рдХрд╛рд╣реАрдЪ рдЪрд╛рд▓рдд рдирд╛рд╣реА. ЁЯШй

Compose рдореНрд╣рдгрдЬреЗ laminate рдХреЗрд▓реЗрд▓рд╛ рдЯреЗрдмрд▓рдЪрд╛ рдЖрд░рд╛рдЦрдбрд╛ ЁЯН╜я╕П: рдПрдХ рдХрд╛рдЧрдж рдЬреЛ рд╕рд╛рдВрдЧрддреЛ "рдпрд╛ рдЯреЗрдмрд▓рд╡рд░ рдПрдХ web рдбрдмрд╛ рдЖрдгрд┐ рдПрдХ proxy рдбрдмрд╛ рдЖрд╣реЗ, рддреЗ intercom рд╡рд╛рдЯреВрди рдШреЗрддрд╛рдд, рдлрдХреНрдд proxy рд▓рд╛ рд╡рд╛рдврд╛рдпрдЪреА рдЦрд┐рдбрдХреА рдЖрд╣реЗ, рд╣рд╛ fridge рдЖрд╣реЗ." рдордЧ:

рдЖрдкрд▓реЗ рдЯреЗрдмрд▓: proxy рд╣рд╛ рдПрдХрдореЗрд╡ рдЦрд┐рдбрдХреА рдЕрд╕рд▓реЗрд▓рд╛ рдбрдмрд╛ рдЖрд╣реЗ (8080:80); рддреЛ рд╕рдЧрд│реНрдпрд╛рдВрдирд╛ рдирд╛рд╡рд╛рдиреЗ web рдХрдбреЗ рдкрд╛рдард╡рддреЛ. web рд▓рд╛ рд╕реНрд╡рддрдГрд▓рд╛ рдХреЛрдгрддреАрд╣реА рдЦрд┐рдбрдХреА рдирд╛рд╣реА тАФ рддреЛ рдлрдХреНрдд proxy рдорд╛рд░реНрдлрддрдЪ рдкреЛрд╣реЛрдЪрддрд╛ рдпреЗрддреЛ, рдЬрд╕реЗ рдХрд╛рдЙрдВрдЯрд░рдорд╛рдЧрдЪреЗ рд╕реНрд╡рдпрдВрдкрд╛рдХрдШрд░.

ЁЯЧ║я╕П рдЖрдХреГрддреА

flowchart LR
    yml["ЁЯУД compose.yml<br/>the table plan"]
    subgraph table["ЁЯН╜я╕П one shared network, auto-created"]
        proxy["ЁЯУж proxy - nginx<br/>ЁЯкЯ the ONLY window :8080"]
        web["ЁЯУж web - our app<br/>no window, name-only"]
        proxy -->|"http://web:3000<br/>by service NAME"| web
    end
    you["ЁЯзСтАНЁЯТ╗ you"]
    yml -->|"1 docker compose up --build"| table
    you -->|"2 localhost:8080"| proxy

тЭУ рдХрд╛рдп

ЁЯза Compose рд╣рд╡реА рдЕрд╕рд▓реЗрд▓реА application рд░рдЪрдирд╛ рд╡рд░реНрдгрди рдХрд░рддреЗ

compose.yml
web
 тФЬтФАтФА depends_on тЖТ api
 тФВ                 тФФтФАтФА db
 тФФтФАтФА network: app-net (everyone by name)

рддреБрдореНрд╣реА рд░реЛрдЬ рдЯрд╛рдЗрдк рдХрд░рд╛рд▓ рдЕрд╢рд╛ рдкрд╛рдЪ commands:

docker compose up -d      # create + start the whole table, detached
docker compose ps         # who's up
docker compose logs -f    # everyone's output, tailed
docker compose exec web sh   # a shell inside one service
docker compose down       # stop + remove (add -v to drop volumes)

depends_on рдлрдХреНрдд рд╕реБрд░реВ рд╣реЛрдгреНрдпрд╛рдЪрд╛ рдХреНрд░рдо рдард░рд╡рддреЗ. рддреНрдпрд╛рдЪрд╛ рдЕрд░реНрде dependency traffic рд╕реНрд╡реАрдХрд╛рд░рд╛рдпрд▓рд╛ рддрдпрд╛рд░ рдЖрд╣реЗ рдЕрд╕рд╛ рдирд╛рд╣реА тАФ db "рд╕реБрд░реВ" рдЭрд╛рд▓реЗрд▓рд╛ рдЕрд╕реВрди рдЕрдЬреВрди initialize рд╣реЛрдд рдЕрд╕реВ рд╢рдХрддреЛ. рддрдпрд╛рд░реАрд╕рд╛рдареА healthcheck + condition: service_healthy рд╡рд╛рдкрд░рд╛, рдХрд┐рдВрд╡рд╛ ре▓рдкрд▓рд╛ retry рдХрд░рд╛рдпрд▓рд╛ рд▓рд╛рд╡рд╛. (Kubernetes рд╣рд╛рдЪ рдкреНрд░рд╢реНрди readiness probes рдиреЗ рд╕реЛрдбрд╡рддреЗ.)

ЁЯдФ рдХрд╛

Compose рдордзреНрдпреЗ "рдорд╛рдЭрд╛ dev setup" git рдордзрд▓реА рдПрдХ file рдмрдирддреЛ тАФ review рдХрд░рддрд╛ рдпреЗрдгрд╛рд░реА, рд╢реЗрдЕрд░ рдХрд░рддрд╛ рдпреЗрдгрд╛рд░реА, versioned. Kubernetes рдХрдбреЗ рдЬрд╛рдгрд╛рд░рд╛ рд╣рд╛ рд╕рд░реНрд╡реЛрддреНрддрдо рдорд╛рдирд╕рд┐рдХ рдкреВрд▓рд╣реА рдЖрд╣реЗ: Compose service тЙИ Deployment + Service, рд╕рд╛рдорд╛рдпрд┐рдХ network тЙИ cluster DNS. рд▓рд╣рд╛рди рд╢рд╛рд│рд╛ рд╢рд┐рдХрд╛, рдореНрд╣рдгрдЬреЗ рдореЛрдареА рд╢рд╛рд│рд╛ рдУрд│рдЦреАрдЪреА рд╡рд╛рдЯреЗрд▓.

ЁЯзк рдХрд░реВрди рдкрд╛рд╣рд╛

docker compose up --build          # the whole table, one command
# open http://localhost:8080       # тЖТ proxy тЖТ web, by name ЁЯОЙ

curl localhost:8080                # ЁЯН▒ hello from <web's container id>
curl localhost:3000 || echo "web has NO window тАФ exactly as planned ЁЯЩЕ"

docker compose ps                  # the table's seating chart
docker compose logs -f web         # one box's diary (Ctrl+C)
docker compose down                # table vanishes, nothing left behind

тЪая╕П рдиреЗрд╣рдореАрдЪреНрдпрд╛ рдЪреБрдХрд╛

тПня╕П рдкреБрдвреЗ

рдЖрдкрд▓реА image рд▓рд╣рд╛рди рдЖрд╣реЗ рдХрд╛рд░рдг ре▓рдкрд▓рд╛ рд╢реВрдиреНрдп dependencies рдЖрд╣реЗрдд. рдЦрд▒реНрдпрд╛ ре▓рдкреНрд╕рдордзреНрдпреЗ build рдкрд╛рдпрд▒реНрдпрд╛ рдЖрдгрд┐ рдЬрдб tools рдЕрд╕рддрд╛рдд тАФ рдЖрдгрд┐ рддрд░реАрд╣реА images рдЫреЛрдЯреНрдпрд╛ рдареЗрд╡рдгреНрдпрд╛рдЪреА рдПрдХ рдпреБрдХреНрддреА рдЖрд╣реЗ: multi-stage builds.

git checkout lesson-07-multi-stage

ЁЯН╜я╕ПЁЯН╜я╕П Lesson 06 тАФ Docker Compose: set the whole table with one command

ЁЯУН You are here: Lesson 06 of 12 ┬╖ Previous: lesson-05-volumes-networks ┬╖ Next: lesson-07-multi-stage


ЁЯУж What's in this branch

Lessons 01тАУ05, plus: Compose тАФ everything from lesson 05, declared in one file instead of typed as flags. Real files:

ЁЯзТ Explain like I'm 5

Yesterday you set the lunch table by hand: make a network, run box 1 with these flags, run box 2 with those flags, connect the fridgeтАж 15 minutes of flag-typing, and your teammate does it slightly differently and nothing works. ЁЯШй

Compose is the laminated table plan ЁЯН╜я╕П: one sheet that says "this table has a web box and a proxy box, they share the intercom, only the proxy has a serving window, here's the fridge." Then:

Our table: the proxy is the only box with a window (8080:80); it forwards everyone to web by name. web itself has NO window тАФ reachable only through the proxy, like a kitchen behind the counter.

ЁЯЧ║я╕П Diagram

flowchart LR
    yml["ЁЯУД compose.yml<br/>the table plan"]
    subgraph table["ЁЯН╜я╕П one shared network, auto-created"]
        proxy["ЁЯУж proxy - nginx<br/>ЁЯкЯ the ONLY window :8080"]
        web["ЁЯУж web - our app<br/>no window, name-only"]
        proxy -->|"http://web:3000<br/>by service NAME"| web
    end
    you["ЁЯзСтАНЁЯТ╗ you"]
    yml -->|"1 docker compose up --build"| table
    you -->|"2 localhost:8080"| proxy

тЭУ What

ЁЯза Compose describes a desired application topology

compose.yml
web
 тФЬтФАтФА depends_on тЖТ api
 тФВ                 тФФтФАтФА db
 тФФтФАтФА network: app-net (everyone by name)

The five commands you'll type daily:

docker compose up -d      # create + start the whole table, detached
docker compose ps         # who's up
docker compose logs -f    # everyone's output, tailed
docker compose exec web sh   # a shell inside one service
docker compose down       # stop + remove (add -v to drop volumes)

depends_on controls start order only. It does not mean the dependency is ready to accept traffic тАФ db may be "started" and still initializing. For readiness use a healthcheck + condition: service_healthy, or make the app retry. (Kubernetes solves the same problem with readiness probes.)

ЁЯдФ Why

Compose is where "my dev setup" becomes a file in git тАФ reviewable, shareable, versioned. It's also the best mental bridge to Kubernetes: a Compose service тЙИ Deployment + Service, the shared network тЙИ cluster DNS. Learn the small school, and the big school feels familiar.

ЁЯзк Try it

docker compose up --build          # the whole table, one command
# open http://localhost:8080       # тЖТ proxy тЖТ web, by name ЁЯОЙ

curl localhost:8080                # ЁЯН▒ hello from <web's container id>
curl localhost:3000 || echo "web has NO window тАФ exactly as planned ЁЯЩЕ"

docker compose ps                  # the table's seating chart
docker compose logs -f web         # one box's diary (Ctrl+C)
docker compose down                # table vanishes, nothing left behind

тЪая╕П Common mistakes

тПня╕П Next

Our image is small because the app has zero dependencies. Real apps have build steps and heavy tools тАФ and there's a trick to keep images tiny anyway: multi-stage builds.

git checkout lesson-07-multi-stage
тЖР Previousvolumes networksNext тЖТmulti stage

This page is the lesson's README from the lesson-06-docker-compose branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.