ပထမဆုံး အနေဖြင့် docker compose အမည်ရှိ directory တစ်ခု တည်ဆောက်ပါမယ်။ ထို့နောက် web application သိမ်းဆည်းရန် webapp အမည်ရှိ directory တည်ဆောက်ပါမယ်။ webapp directory ထဲတွင် web application ကိုစမ်းရန် အတွက် index.html ကို တည်ဆောက်ပါမယ်။
$ mkdir dockercompose && cd dockercompose
$ mkdir webapp && echo "It Works"; webapp/index.html
ပြီးနောက် web application အတွက် လိုအပ်သော dockerfile ကို webapp directory ထဲမှာတည်ဆောက်ပါမယ်။ dockerfile ဟာဆိုရင် web application အတွက် လိုအပ်သော apache web server ပါ၀င်သည့် customized image တည်ဆောက်ရန်ဖြစ်ပါတယ်။
$ vim webapp/Dockerfile
ထို့နောက် အောက်ပါ code များကို ပေါင်းထည့်ပါ။
FROM tecadmin/ubuntu-ssh:16.04
RUN apt-get update \
&& apt-get install -y apache2
COPY index.html /var/www/html/
WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]
EXPOSE 80
ထို့နောက် လက်ရှိ directory ထဲတွင် docker-compose.yml အမည်ရှိ docker configuration ဖိုင် တစ်ခုကို တည်ဆောက်ပါမယ်။ ထို configuration ဖိုင်ဟာ အသုံးပြုမည့် containers အကုန်လုံးကို ကိုယ်စားပြုမှာဖြစ်ပါတယ်။
$ vim docker-compose.yml
ထို့နောက် အောက်ပါ code များကို ပေါင်းထည့်ပါ။
version: '3'
services:
db:
image: mysql
container_name: mysql_db
restart: always
environment:
- MYSQL_ROOT_PASSWORD="secret"
web:
image: apache
build: ./webapp
depends_on:
- db
container_name: apache_web
restart: always
ports:
- "8080:80"
အထက်ပါ ဖိုင်ဟာ containers နှစ်ခု အတွက်ဖြစ်ပါတယ်။ ပထမ container သည် mysql database server အတွက်ဖြစ်ပြီး ဒုတိယသည် web server အတွက်ဖြစ်ပါတယ်။ Web container ဟာ application များကို apache server တွင် အလုပ်လုပ်စေမှာဖြစ်ပါတယ်။ webapp directory ကို build directory အဖြစ် သတ်မှတ်ထားခြင်းဖြစ်ပါတယ်။
အောက်ပါ command ဖြင့် webapp directory အတွင်းရှိ contents များနှင့် Dockerfile ကို အသုံးပြု၍ apache အမည်ရှိ image တစ်ခုကို တည်ဆောက်ပါမယ်။
$ docker-compose build
db uses an image, skipping Building web Step 1/6 : FROM tecadmin/ubuntu-ssh:16.04 16.04: Pulling from tecadmin/ubuntu-ssh b3e1c725a85f: Pull complete 4daad8bdde31: Pull complete 63fe8c0068a8: Pull complete 4a70713c436f: Pull complete bd842a2105a8: Pull complete c41407f48fa7: Pull complete 1fcfeb9b5ef4: Pull complete 13195a7d2240: Pull complete b86be64bbda8: Pull complete 8c951fe917dc: Pull complete f74bc80103b6: Pull complete Digest: sha256:523d6fbc97954e9f77231bf54bfcfbbdd4805349887477fbac4a63dc735d777d Status: Downloaded newer image for tecadmin/ubuntu-ssh:16.04 ---> bb63b492da01 Step 2/6 : RUN apt-get update && apt-get install -y apache2 ---> Running in 00be0dd717ce [[[Removed long output from here]]] ---> 41c731590234 Removing intermediate container 00be0dd717ce Step 3/6 : COPY index.html /var/www/html/ ---> 42f84d4c2243 Removing intermediate container 945aaee6cbde Step 4/6 : WORKDIR /var/www/html ---> 40bebd21e352 Removing intermediate container e13f5f412906 Step 5/6 : CMD apachectl -D FOREGROUND ---> Running in ab0db1ef1c6e ---> 587bf2323289 Removing intermediate container ab0db1ef1c6e Step 6/6 : EXPOSE 80 ---> Running in 7bcbef52d585 ---> 8f03d4135394 Removing intermediate container 7bcbef52d585 Successfully built 8f03d4135394 Successfully tagged apache:latest
docker-compose up ကို အသုံးပြု၍ containers များကို စတင်စေပါမယ်။ Daemon mode ကို အသုံးပြုရန် -d option ကို အသုံးပြုနိုင်ပါတယ်။
$ docker-compose up -d
Web application တွင် ပြောင်းလဲမှု များပြုလုပ်လိုလျှင်
$ echo "Welcome to Docker Compose Tutorial" >> webapp/index.html
ပြီးလျှင် အောက်ပါ command များကို သုံး၍ webapp container ကို ပြန်လည် တည်ဆောက်ပြီး စတင် အလုပ်လုပ်စေနိုင်ပါတယ်။
$ docker-compose build
$ docker-compose up -d