Module 4 – Docker Compose

Docker Containers කිහිපයක් පහසුවෙන් Manage කරන විදිහ

Docker Compose කියන්නේ මොකක්ද?

Docker භාවිතා කරන විට සාමාන්‍යයෙන් application එකක් run වෙන්න containers කිහිපයක් අවශ්‍ය වෙනවා. උදාහරණයක් ලෙස modern web application එකක් run වෙන්න මෙවැනි services තිබෙන්න පුළුවන්.

  • Frontend container
  • Backend container
  • Database container
  • Cache container

මෙවැනි containers එකින් එක docker run command එකෙන් run කිරීම ටිකක් සංකීර්ණ සහ අසමර්ථ ක්‍රමයක් වෙන්න පුළුවන්.

මේ ගැටලුවට විසඳුමක් ලෙස Docker ecosystem එකේ භාවිතා කරන tool එකක් තමයි Docker Compose.

Docker Compose භාවිතා කරලා containers කිහිපයක් එකම configuration file එකක් භාවිතා කරලා run කරන්න, manage කරන්න සහ configure කරන්න පුළුවන්.

Docker Compose කියන්නේ containers කිහිපයක් run වෙන application එකක් single configuration file එකක් භාවිතා කරලා manage කරන tool එකක්. Docker Compose භාවිතා කරද්දී application configuration එක YAML format එකෙන් ලියන file එකක් තුළ define කරනවා.

මෙම file එක තුළ define කරනවා:

  • containers
  • networks
  • volumes
  • environment variables

ඉන්පසු single command එකක් භාවිතා කරලා සියලු containers run කරන්න පුළුවන්.


Docker Compose භාවිතා කරන ප්‍රධාන හේතු

Docker Compose developers අතර popular වෙලා තියෙන්නේ මේ හේතු නිසා.

1. Containers කිහිපයක් පහසුවෙන් run කිරීම

Suppose ඔබට run කරන්න ඕන web application එකක් තියෙනවා කියලා හිතමු.

එය run වෙන්න අවශ්‍ය services:

  • Web server
  • Backend API
  • Database

Docker Compose නැත්නම් commands තුනක් run කරන්න වෙනවා.

Docker Compose භාවිතා කළාම:

docker compose up

එක command එකෙන් සියලු containers run වෙනවා.

2. Configuration එක එකම file එකක තබාගැනීම

Docker Compose භාවිතා කරනකොට application configuration එක docker-compose.yml file එක තුළ define කරනවා.

එහි define කරනවා:

  • container names
  • ports
  • environment variables
  • volumes
  • networks

මෙම approach එක application setup කිරීම consistent කරයි.

3. Development environment එක පහසුවෙන් recreate කිරීම

Developer team එකක් තුළ development environment setup කිරීම sometimes difficult වෙනවා.

Docker Compose file එක share කරලා docker compose up run කරනකොට same environment එක create වෙනවා.


Docker Compose Architecture

Docker Compose architecture එක සරලව මෙහෙම explain කරන්න පුළුවන්.

                            docker-compose.yml
                                    ↓
                            Docker Compose CLI
                                    ↓
                            Multiple Containers
                                    ↓
                            Application Services
                

Docker Compose file එක තුළ define කරන services run වෙන්නේ separate containers ලෙසයි.


docker-compose.yml file එක

Docker Compose භාවිතා කරන විට docker-compose.yml කියන configuration file එක භාවිතා කරනවා. මෙය YAML format එකෙන් ලියන file එකක්.

Example එකක් බලමු.


                    version: "3"

                    services:

                    web:
                        image: nginx
                        ports:
                        - "80:80"

                    database:
                        image: mysql
                        environment:
                        MYSQL_ROOT_PASSWORD: example  
                

මෙහි define කරලා තියෙන්නේ services දෙකක්.

  • web container
  • database container

Docker Compose run කරනකොට containers දෙකම automatically start වෙනවා.


Docker Compose භාවිතා කරන ප්‍රධාන අවස්ථා

Docker Compose developer community එකේ widely භාවිතා වෙනවා.

Development environments

Developers ලා local environment එකක් create කරන්න Docker Compose භාවිතා කරනවා.

Microservices applications

Microservices architecture එකක් තුළ services කිහිපයක් run වෙනවා. Docker Compose මේ services manage කරන්න භාවිතා කරනවා.

Testing environments

Testing purposes සඳහා consistent and reproducible environments create කරන්න Docker Compose භාවිතා කරනවා. Automated testing pipelines වල temporary environments create කරන්න Docker Compose භාවිතා කරනවා.