دليل Docker للمبتدئ. كيف تستخدم docker-compose.


docker-compose
الصورة

بعد مقدمة قصيرة عن docker-compose سوف تتمكن من إنشاء أول مشروع خادم/عميل باستخدام docker.

هذه المقالة تعتمد على المقالة الأولى التي نشرتها سابقا والتي تتحدث عن الأساسيات, يمكنك قراءتها من هنا.

في البداية, ما هو docker-compose؟

هي أداة من أدوات docker والتي صممت لتسهل العمل به ولتحل مشكلة هامة والتي هي حديث اليوم.

هل لاحظت في المقال السابق البرنامج الذي صممناه ليعمل باستخدام docker ويطبع "docker is magic" عندما يعمل.

حسنا هذا كان سهل جدا, ولكن نادرا جدا كمطور أن تصمم برنامج يعمل وحده (أي أنه لا يعتمد على أي خدمات خارجية كقاعدة البيانات مثلا).

حسنا كيف تعلم أنه لابد من استخدام docker-compose؟

إذا كان برنامجك يتطلب عدة خدمات للعمل معا. فمثلا إذا لديك موقع يحتاج للاتصال بقاعدة بيانات(وهنا لديك خدمتان الموقع, وقاعدة البيانات). فلابد من استخدام هذه الأداة.

حيت يوفر لك docker-compose تشغيل كل هذه الخدمات معا بأمر واحد.

والأن ما الفرق بين docker و docker-compose؟

Docker يستخدم لإدارة حاوية (خدمة) واحدة من تطبيقك. أما docker-compose فيستخدم لإدارة عدة حاويات في نفس الوقت لنفس التطبيق. وتمكنك الأداة من بناء تطبيقات أكثر تعقيدا وإدارتها بكل سهولة. 

docker-compose
Docker (individual container) VS Docker-Compose (several containers)

حسنا... دعني أوضح لك مدى أهمية هذه الأداة بمثال بسيط.

تخيل أن لديك موقعان. الأول يكن الناس من إنشاء متاجرهم على الإنترنت بكل سهولة, والثاني مخصص لدعم العملاء. وعلى كلا الموقعين استخدام نفس قاعدة البيانات.

والأن لقد بدأت تنجح وخادمك لم يعد كافيا. لذا قررت أن تنقل خدماتك إلى خادم آخر.

ولسوء الحظ لم تكن تستخدم docker-compose لذا ستقوم بنقل الخدمات واحدة بواحدة مع إعادة ضبط إعدادات كل منها وتأمل الا تنسى شيئا.

أما مع استخدام هذه الأداة سوف تتمكن من فعل كل هذا ببعض الأوامر.

والأن دعنا نبدأ ببناء أول تطبيق خادم/عميل لك باستخدام docker-compose.

الهدف الأن هو انشاء موقع (server) بسيط به جملة. وهذه الجملة سوف تجلب بواسطة العميل (client) باستخدام لغة بايثون.

أ- ابدأ ببناء مجلد المشروع.

قم بعمل مجلد على حاسوبك والذي يجب ان يحتوي على هذه الملفات:
  • ملف "docker-compose.yml" الملف الذي سيحتوي عل المعلومات الضرورية لإنشاء الخدمات.
  • مجلد "server" سيحتوي على الملفات الخاصة بالخادم.
  • مجلد "client" سيحتوي على الملفات الخاصة بالعميل.
ب- قم بإنشاء الخادم.

يجب أن يحتوي مجلد الخادم على الملفات الأتية:

  • ملف "server.py" ملف بايثون الذي سيحتوي على كود السيرفر.
  • ملف "index.html" ملف HTML الذي يحتوي على الجملة التي ستعرض.
  • ملف "dockerfile" الذي يحتوي على التعليمات الازمة لإنشاء الخادم.

ب.1- اكتب كود بايثون الخاص بالخادم.

سيبدو الكود بهذا الشكل:
#!/usr/bin/env python3
# Import of python system libraries.
# These libraries will be used to create the web server.
# You don't have to install anything special, these libraries are installed with Python.

import http.server
import socketserver

# This variable is going to handle the requests of our client on the server.

handler = http.server.SimpleHTTPRequestHandler

# Here we define that we want to start the server on port 1234.
# Try to remember this information it will be very useful to us later with docker-compose.

with socketserver.TCPServer(("", 1234), handler) as httpd:

# This instruction will keep the server running, waiting for requests from the client.

httpd.serve_forever()
هذا الكود يقو بإنشاء خادم ويب بسيط وسيقوم بعرض محتويات index.html.

ب.2- اكتب ملف html.

سيبدو بهذا الشكل. بسيط جدا, أليس كذلك؟
Docker-Compose is magic!
سوف يقوم الخادم بعرض هذه الجملة عنما يعمل.

ب.3- اكتب ملف dockerfile.

سيبدو كالمرة السابقة مع بعض التعديلات.
# Just a remember, dockerfile must always start by importing the base image.
# We use the keyword 'FROM' to do that.
# In our example, we want to import the python image (from DockerHub).
# So, we write 'python' for the image name and 'latest' for the version.

FROM python:latest

# In order to launch our python code, we must import the 'server.py' and 'index.html' file.
# We use the keyword 'ADD' to do that.
# Just a remember, the first parameter 'server.py' is the name of the file on the host.
# The second parameter '/server/' is the path where to put the file on the image.
# Here we put files at the image '/server/' folder.

ADD server.py /server/

ADD index.html /server/

# I would like to introduce something new, the 'WORKDIR' command.
# This command changes the base directory of your image.
# Here we define '/server/' as base directory (where all commands will be executed).

WORKDIR /server/

ج- قم ببناء العميل.

يجب أن يحتوي مجلد العميل على الملفات الأتية:
  • ملف "client.py" الذ يحتوي على كود بايثون العميل.
  • ملف "dockerfile" حسنا... تعرف اهو.

ج.1- اكتب ملف بايثون.

سيبدو بهذا الشكل:
#!/usr/bin/env python3
# Import of python system library.
# This library is used to download the 'index.html' from server.
# You don't have to install anything special, this library is installed with Python.

import urllib.request

# This variable contain the request on 'http://localhost:1234/'.
# You must wondering what is 'http://localhost:1234'?
# localhost: This means that the server is local.
# 1234: Remember we define 1234 as the server port.

fp = urllib.request.urlopen("http://localhost:1234/")

# 'encodedContent' correspond to the server response encoded ('index.html').
# 'decodedContent' correspond to the server response decoded (what we want to display).

encodedContent = fp.read()
decodedContent = encodedContent.decode("utf8")

# Display the server file: 'index.html'.

print(decodedContent)

# Close the server connection.

fp.close()
سوف يحضر هذا الكود محتويات صفحة الويب من على السيرفر.

ج.2- اكتب ملف dockerfile.

سيبدو بهذا الشكل:
# Same thing than the 'server' Dockerfile.

FROM python:latest

# Same thing than the 'server' Dockerfile.
# We import 'client.py' in '/client/' folder.

ADD client.py /client/

# I would like to introduce something new, the 'WORKDIR' command.
# This command changes the base directory of your image.
# Here we define '/client/' as base directory.

WORKDIR /client/

د- عودة إلى ملف docker-compose.

كما تري فقد أنشأنا مشروعين مختلفين (server/client) مع ملف dockerfile لكلا منهما. لاشي مختلف عما فعلناه من قبل.

والأن سوف نبدأ بكتابة ملف docker-compose.yml.

ملاحظة: هذا الملف ليس كاملا.

# A docker-compose must always start by the version tag.
# We use "3" because it's the last version at this time.

version: "3"

# You should know that docker-composes works with services.
# 1 service = 1 container.
# For example, a service maybe, a server, a client, a database...
# We use the keyword 'services' to start to create services.

services:

# As we said at the beginning, we want to create: a server and a client.
# That is two services.
# First service (container): the server.
# Here you are free to choose the keyword.
# It will allow you to define what the service corresponds to.
# We use the keyword 'server' for the server.

server:

# The keyword "build" will allow you to define
# the path to the Dockerfile to use to create the image
# that will allow you to execute the service.
# Here 'server/' corresponds to the path to the server folder
# that contains the Dockerfile to use.

build: server/

# The command to execute once the image is created.
# The following command will execute "python ./server.py".

command: python ./server.py

# Remember that we defined in'server/server.py' 1234 as port.
# If we want to access the server from our computer (outside the container),
# we must share the content port with our computer's port.
# To do this, the keyword 'ports' will help us.
# Its syntax is as follows: [port we want on our machine]:[port we want to retrieve in the container]
# In our case, we want to use port 1234 on our machine and
# retrieve port 1234 from the container (because it is on this port that
# we broadcast the server).

ports:

- 1234:1234

هـ- بناء المشروع.

عنما تنتهي قم بكتابة هذا الأمر لبناء المشروع.
$ docker-compose build

و- تشغيل المشروع.

اكتب هذا الأمر لتشغيل المشروع.
$ docker-compose up
سوف تري عبارة "docker-compose is magic" مطبوعة أمامك في سطر الأوامر.

وإذا ذهبت إلى "http://localhost:1234/" في المتصفح سوف تري نفس العبارة.


الكود متاح ويمكنك رؤيته من هنا.


إلى اللقاء.

تعليقات

المشاركات الشائعة من هذه المدونة

30 شيء تمنيت لو عرفتها عندما بدأت في البرمجة(الجزء الأول).

ما هي مبادئ SOLID؟ ولما يجب أن يعرفها كل مطور؟

كيف تصبح مطور ويب وتحصل على وظيفة في أسرع وقت؟ (الجزء الثاني)