Docker Compose: Basics da Amfani

Docker Compose kayan aiki ne mai ƙarfi kuma sanannen da ake amfani dashi don sarrafawa da tura aikace-aikace bisa Docker. Yana ba ku damar ayyana, daidaitawa, da gudanar da Docker kwantena da yawa azaman aikin guda ɗaya, sauƙaƙe ƙaddamar da aikace-aikacen da tabbatar da daidaito tsakanin yanayin haɓakawa da samarwa.

A ƙasa akwai wasu ra'ayoyi da misalai na Docker Compose:

Ƙayyade aikin ta amfani da fayil ɗin docker-compose.yml

A cikin docker-compose.yml fayil ɗin, zaku iya ayyana ayyukan da ake buƙata don aikace-aikacen ku. Misali, don tura aikace-aikacen gidan yanar gizo na PHP tare da bayanan MySQL, zaku iya ayyana ayyuka guda biyu kamar haka:

version: "3"  
services:  
  web:  
    image: php:7.4-apache  
    ports:  
   - "80:80"  
    volumes:  
   - ./app:/var/www/html  
  
  db:  
    image: mysql:5.7  
    environment:  
      MYSQL_ROOT_PASSWORD: password  
      MYSQL_DATABASE: my_database  

A cikin snippet lambar da ke sama, mun ayyana ayyuka guda biyu: web da db. Sabis ɗin web zai yi amfani da PHP 7.4 image tare da Apache, saurara a tashar jiragen ruwa 80, kuma ya hau ./app kundin adireshi daga mai watsa shiri zuwa cikin /var/www/html directory a cikin container. Sabis db ɗin zai yi amfani da MySQL 5.7 image kuma ya saita wasu masu canjin yanayi da ake buƙata don bayanan bayanai.

 

Amfani da Docker Compose umarni

Da zarar kun ayyana aikin a cikin docker-compose.yml fayil ɗin, zaku iya amfani da Docker Compose umarni don sarrafa ayyukan.

  • Fara aikin: docker-compose up

    Wannan umarnin zai fara kwantena don ayyukan da aka ayyana a cikin docker-compose.yml fayil ɗin.

  • Tsaya kuma cire kwantena: docker-compose down

    Wannan umarnin yana tsayawa kuma yana cire duk kwantena masu alaƙa da aikin.

  • Jerin kwantena masu gudana: docker-compose ps

    Wannan umarnin zai nuna matsayin kwantena a cikin aikin.

  • Duba rajistan ayyukan sabis: docker-compose logs

    Wannan umarnin yana nuna rajistan ayyukan sabis a cikin aikin.

 

Matsalolin muhalli da gyare-gyare

Docker Compose yana ba ku damar amfani da masu canjin yanayi don keɓance saiti don mahalli daban-daban, kamar haɓakawa da samarwa. Kuna iya amfani da masu canjin yanayi a cikin docker-compose.yml fayil ɗin kuma ayyana ƙimar su a cikin .env fayiloli masu dacewa.

Misali, idan kuna son ayyana canjin yanayi don tashar sabis ɗin web, zaku iya ƙara layi zuwa .env fayil ɗin kamar haka:

WEB_PORT=8080

Sannan, a cikin docker-compose.yml fayil ɗin, zaku iya amfani da wannan canjin yanayi kamar haka:

version: "3"  
services:  
  web:  
    image: php:7.4-apache  
    ports:  
   - "${WEB_PORT}:80"  
    volumes:  
   - ./app:/var/www/html  

Lokacin gudanar da docker-compose up umarni, web sabis ɗin zai saurari tashar 8080 maimakon tashar jiragen ruwa 80.

 

Haɗin kai tare da Docker Swam

Idan kuna son tura aikace-aikacenku akan yanayin da aka rarraba tare da nodes masu yawa, Docker Compose na iya haɗawa da Docker Swarm. Wannan yana ba ku damar sarrafa ayyuka a fadin nodes da yawa a cikin Docker tari.

Don amfani da wannan haɗin kai, kawai kuna buƙatar ƙara zaɓuɓɓuka --orchestrate ko --with-registry-auth zaɓuɓɓuka lokacin aiki docker stack deploy ko docker-compose up umarni a cikin Swarm yanayi.

 

Docker Compose kayan aiki ne mai amfani don haɓaka aikace-aikacen sauƙi da inganci, gwaji, da turawa. Yana rage bambance-bambance tsakanin yanayin haɓakawa da samarwa, yana tabbatar da daidaito a cikin tsarin haɓaka software, da haɓaka haɓakar ƙungiyoyin haɓakawa.