Docker Disederhanakan: Panduan Mudah untuk Pemula Mutlak

Sama ada anda merancang untuk memulakan karier anda di DevOps, atau anda sudah mengalaminya, jika anda tidak mempunyai Docker yang disenaraikan dalam resume anda, sudah pasti anda perlu memikirkannya, kerana Docker adalah salah satu kemahiran penting bagi sesiapa masuk ke arena DevOps.

Dalam catatan ini, saya akan cuba sedaya upaya untuk menerangkan Docker dengan kaedah termudah.

Sebelum kita menyelam secara mendalam dan mula meneroka Docker, mari kita lihat topik apa yang akan kita bahas sebagai sebahagian daripada panduan pemula ini.

  • Apa itu Docker?
  • Masalah yang diselesaikan oleh Docker
  • Kelebihan dan kekurangan penggunaan Docker
  • Komponen teras Docker
  • Terminologi Docker
  • Apa itu Docker Hub?
  • Edisi Docker
  • Memasang Docker
  • Beberapa arahan penting Docker untuk memulakan anda
  • Selesaikan

Mari kita mulakan dengan memahami, Apa itu Docker?

Secara sederhana, Docker adalah platform perisian yang mempermudah proses membina, menjalankan, mengurus dan mengedarkan aplikasi. Ia melakukan ini dengan memvisualisasikan sistem operasi komputer di mana ia dipasang dan dijalankan.

Edisi pertama Docker dikeluarkan pada tahun 2013.

Docker dikembangkan menggunakan bahasa pengaturcaraan GO.

Melihat sekumpulan fungsi yang ditawarkan Docker, ia diterima secara meluas oleh beberapa organisasi dan universiti terkemuka di dunia, seperti Visa, PayPal, Cornell University dan Indiana University (hanya untuk beberapa nama) untuk menjalankan dan menguruskan mereka aplikasi menggunakan Docker.

Sekarang mari kita cuba memahami masalahnya, dan penyelesaian yang ditawarkan oleh Docker

Masalah

Katakan anda mempunyai tiga aplikasi berasaskan Python yang anda rencanakan untuk dihoskan pada satu pelayan (yang boleh menjadi mesin fizikal atau mesin maya).

Setiap aplikasi ini menggunakan versi Python yang berbeza, serta perpustakaan dan dependensi yang berkaitan, berbeza dari satu aplikasi ke aplikasi yang lain.

Oleh kerana kami tidak dapat memasang versi Python yang berlainan pada mesin yang sama, ini menghalang kami daripada mengehos ketiga-tiga aplikasi pada komputer yang sama.

Penyelesaian

Mari lihat bagaimana kita dapat menyelesaikan masalah ini tanpa menggunakan Docker. Dalam senario seperti itu, kita dapat menyelesaikan masalah ini baik dengan memiliki tiga mesin fizikal, atau satu mesin fizikal, yang cukup kuat untuk menampung dan menjalankan tiga mesin maya di atasnya.

Kedua-dua pilihan ini membolehkan kami memasang versi Python yang berlainan pada setiap mesin ini, bersama dengan pergantungan yang berkaitan.

Tidak kira penyelesaian mana yang kami pilih, kos yang berkaitan dengan pemerolehan dan penyelenggaraan perkakasan agak mahal.

Sekarang, mari kita periksa bagaimana Docker boleh menjadi penyelesaian yang berkesan dan menjimatkan kos untuk masalah ini.

Untuk memahami perkara ini, kita perlu melihat bagaimana sebenarnya fungsi Docker.

Mesin di mana Docker dipasang dan dijalankan biasanya disebut sebagai Docker Host atau Host dalam istilah sederhana.

Jadi, setiap kali anda merancang untuk menggunakan aplikasi di host, ia akan membuat entiti logik di atasnya untuk menjadi tuan rumah aplikasi tersebut. Dalam terminologi Docker, kami memanggil entiti logik ini sebagai Container atau Docker Container agar lebih tepat.

Docker Container tidak mempunyai sistem operasi yang terpasang dan berjalan di atasnya. Tetapi ia akan mempunyai salinan maya dari tabel proses, antara muka rangkaian, dan titik pemasangan sistem fail. Ini telah diwarisi dari sistem operasi host di mana wadah dihoskan dan dijalankan.

Manakala kernel sistem operasi host dikongsi di semua kontena yang berjalan di atasnya.

Ini membolehkan setiap bekas diasingkan dari yang lain pada hos yang sama. Oleh itu, ia menyokong pelbagai kontena dengan keperluan aplikasi dan kebergantungan yang berbeza untuk dijalankan pada host yang sama, asalkan mereka mempunyai keperluan sistem operasi yang sama.

Untuk memahami bagaimana Docker bermanfaat dalam menyelesaikan masalah ini, anda perlu merujuk pada bahagian seterusnya, yang membincangkan kelebihan dan kekurangan penggunaan Docker.

Ringkasnya, Docker akan memvisualisasikan sistem operasi host di mana ia dipasang dan dijalankan, dan bukannya memvisualisasikan komponen perkakasan.

Kelebihan dan Kekurangan menggunakan Docker

Kelebihan menggunakan Docker

Beberapa faedah utama menggunakan Docker disenaraikan di bawah:

  • Docker menyokong pelbagai aplikasi dengan keperluan dan kebergantungan aplikasi yang berbeza, untuk dihoskan bersama pada host yang sama, selagi mereka mempunyai keperluan sistem operasi yang sama.
  • Storan Dioptimumkan. Sebilangan besar aplikasi dapat dihosting pada host yang sama, kerana kontena biasanya berukuran beberapa megabait dan memakan ruang cakera yang sangat sedikit.
  • Kekukuhan. Bekas tidak mempunyai sistem operasi yang terpasang di dalamnya. Oleh itu, ia menggunakan memori yang sangat sedikit berbanding dengan mesin maya (yang mempunyai sistem operasi lengkap yang dipasang dan berjalan di atasnya). Ini juga mengurangkan masa boot menjadi hanya beberapa saat, berbanding dengan beberapa minit yang diperlukan untuk boot mesin virtual.
  • Mengurangkan kos. Docker kurang menuntut ketika datang ke perkakasan yang diperlukan untuk menjalankannya.

Kelemahan penggunaan Docker

  • Aplikasi dengan keperluan sistem operasi yang berbeza tidak dapat dihoskan bersama di Docker Host yang sama. Sebagai contoh, katakan kita mempunyai 4 aplikasi yang berbeza, di mana 3 aplikasi memerlukan sistem operasi berasaskan Linux dan aplikasi lain memerlukan sistem operasi berasaskan Windows. Dalam senario seperti itu, 3 aplikasi yang memerlukan sistem operasi berasaskan Linux dapat dihosting pada Docker Host tunggal, sedangkan aplikasi yang memerlukan sistem operasi berbasis Windows perlu dihosting pada Docker Host yang lain.

Komponen Teras Docker

Docker Engine adalah salah satu komponen teras Docker. Ia bertanggungjawab untuk keseluruhan fungsi platform Docker.

Docker Engine adalah aplikasi berasaskan pelayan pelanggan dan terdiri daripada 3 komponen utama.

  1. Pelayan
  2. API REST
  3. Pelanggan

The Server runs a daemon known as dockerd(Docker Daemon), which is nothing but a process. It is responsible for creating and managing Docker Images, Containers, Networks and Volumes on the Docker platform.

The REST API specifies how the applications can interact with the Server, and instruct it to get their job done.

The Client is nothing but a command line interface, that allows users to interact with Docker using the commands.

Docker Terminology

Let us take a quick look at some of the terminology associated with Docker.

Docker Images and Docker Containers are the two essential things that you will come across daily while working with Docker.

In simple terms, a Docker Image is a template that contains the application, and all the dependencies required to run that application on Docker.

On the other hand, as stated earlier, a Docker Container is a logical entity. In more precise terms, it is a running instance of the Docker Image.

What is Docker Hub?

Docker Hub is the official online repository where you could find all the Docker Images that are available for us to use.

Docker Hub also allows us to store and distribute our custom images as well if we wish to do so. We could also make them either public or private, based on our requirements.

Please Note: Free users are only allowed to keep one Docker Image as private. If we wish to keep more than one Docker Image as private, we need to subscribe to a paid subscription plan.

Docker Editions

Docker is available in 2 different editions, as listed below:

  • Community Edition (CE)
  • Enterprise Edition (EE)

The Community Edition is suitable for individual developers and small teams. It offers limited functionality, in comparison to the Enterprise Edition.

The Enterprise Edition, on the other hand, is suitable for large teams and for using Docker in production environments.

The Enterprise Edition is further categorized into three different editions, as listed below:

  • Basic Edition
  • Standard Edition
  • Advanced Edition

Installing Docker

One last thing that we need to know before we go ahead and get our hands dirty with Docker is actually to have Docker installed.

Below are the links to the official Docker CE installation guides. You can follow these guides to install Docker on your machine, as they are simple and straightforward.

  • CentOS Linux
  • Debian Linux
  • Fedora Linux
  • Ubuntu Linux
  • Microsoft Windows
  • MacOS

Want to skip installation and head off straight to practicing Docker?

Just in case you are feeling too lazy to install Docker, or you don’t have enough resources available on your computer, you need not have to worry — here’s the solution to your problem.

You can head over to Play with Docker, which is an online playground for Docker. It allows users to practice Docker commands immediately, without having to install anything on your machine. The best part is it’s simple to use and available free of cost.

Docker Commands

Now it’s time to get our hands dirty with Docker commands, for which we all have been waiting till now.

docker create

The first command which we will be looking at is the docker create command.

This command allows us to create a new container.

The syntax for this command is as shown below:

docker create [options] IMAGE [commands] [arguments]

Please Note: Anything enclosed within the square brackets is optional. This is applicable to all the commands that you would see on this guide.

Some of the examples of using this command are shown below:

$ docker create fedora
02576e880a2ccbb4ce5c51032ea3b3bb8316e5b626861fc87d28627c810af03

In the above example, the docker create command would create a new container using the latest Fedora image.

Before creating the container, it will check if the latest official image of the Fedora is available on the Docker Host or not. If the latest image isn’t available on the Docker Host, it will then go ahead and download the Fedora image from the Docker Hub before creating the container. If the Fedora image is already present on the Docker Host, it will make use of that image and create the container.

If the container was created successfully, Docker will return the container ID. For instance, in the above example 02576e880a2ccbb4ce5c51032ea3b3bb8316e5b626861fc87d28627c810af03 is the container ID returned by Docker.

Each container has a unique container ID. We refer to the container using its container ID for performing various operations on the container, such as starting, stopping, restarting, and so on.

Now, let us refer to another example of docker create command, which has options and commands being passed to it.

$ docker create -t -i ubuntu bash
30986b73dc0022dbba81648d9e35e6e866b4356f026e75660460c3474f1ca005

In the above example, the docker create command creates a container using the Ubuntu image (As stated earlier, if the image isn’t available on the Docker Host, it will go ahead and download the latest image from the Docker Hub before creating the container).

The options -t and -i instruct Docker to allocate a terminal to the container so that the user can interact with the container. It also instructs Docker to execute the bash command whenever the container is started.

docker ps

The next command we will look at is the docker ps command.

The docker ps command allows us to view all the containers that are running on the Docker Host.

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES30986b73dc00 ubuntu "bash" 45 minutes ago Up About a minute elated_franklin

It only displays the containers that are presently running on the Docker Host.

If you want to view all the containers that were created on this Docker Host, irrespective of their current status, such as whether they are running or exited, then you would need to include the option -a, which in turn would display all the containers that were created on this Docker Host.

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES30986b73dc00 ubuntu “bash” About an hour ago Up 29 minutes elated_franklin02576e880a2c fedora “/bin/bash” About an hour ago Created hungry_sinoussi

Before we proceed further, let’s try to decode and understand the output of the docker ps command.

CONTAINER ID: A unique string consisting of alpha-numeric characters, associated with each container.

IMAGE: Name of the Docker Image used to create this container.

COMMAND: Any application specific command(s) that needs to be executed when the container is started.

CREATED: This shows the time elapsed since this container has been created.

STATUS: This shows the current status of the container, along with the time elapsed, in its present state.

If the container is running, it will display as Up along with the time period elapsed (for example, Up About an hour or Up 3 minutes).

If the container is stopped, then it will display as Exited followed by the exit status code within round brackets, along with the time period elapsed (for example, Exited (0) 3 weeks ago or Exited (137) 15 seconds ago, where 0 and 137 are the exit codes).

PORTS: This displays any port mappings defined for the container.

NAMES: Apart from the CONTAINER ID, each container is also assigned a unique name. We can refer to a container either using its container ID or its unique name. Docker automatically assigns a unique silly name to each container it creates. But if you want to specify your own name to the container, you can do that by including the — — name (double hyphen name) option to the docker create or the docker run (we will look at the docker run command later) command.

I hope this gives you a better understanding of the output of the docker ps command.

docker start

The next command we will look at, is the docker start command.

This command starts any stopped container(s).

The syntax for this command is as shown below:

docker start [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

We can start a container either by specifying the first few unique characters of its container ID or by specifying its name.

Some of the examples of using this command are shown below:

$ docker start 30986

In the above example, Docker starts the container beginning with the container ID 30986.

$ docker start elated_franklin

Whereas in this example, Docker starts the container named elated_franklin.

docker stop

The next command on the list is the docker stop command.

This command stops any running container(s).

The syntax for this command is as shown below:

docker stop [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

It is similar to the docker start command.

We can stop the container either by specifying the first few unique characters of its container ID or by specifying its name.

Some of the examples of using this command are shown below:

$ docker stop 30986

In the above example, Docker will stop the container beginning with the container ID 30986.

$ docker stop elated_franklin

Whereas in this example, Docker will stop the container named elated_franklin.

docker restart

The next command we will look at is the docker restart command.

This command restarts any running container(s).

The syntax for this command is as shown below:

docker restart [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

We can restart the container either by specifying the first few unique characters of its container ID or by specifying its name.

Some of the examples of using this command are shown below:

$ docker restart 30986

In the above example, Docker will restart the container beginning with the container ID 30986.

$ docker restart elated_franklin

Whereas in this example, Docker will restart the container named elated_franklin.

docker run

The next command we will be looking at is the docker run command.

This command first creates the container, and then it starts the container. In short, this command is a combination of the docker create and the docker start command.

The syntax for this command is as shown below:

docker run [options] IMAGE [commands] [arguments]

It has a syntax similar to that of the docker create command.

Some of the examples of using this command are shown below:

$ docker run ubuntu
30fa018c72682d78cf168626b5e6138bb3b3ae23015c5ec4bbcc2a088e67520

In the above example, Docker will create the container using the latest Ubuntu image and then immediately start the container.

If we execute the above command, it would start the container and immediately stop it — we wouldn’t get any chance to interact with the container at all.

If we want to interact with the container, then we need to specify the options: -it (hyphen followed by i and t) to the docker run command presents us with the terminal, using which we could interact with the container by typing in appropriate commands. Below is an example of the same.

$ docker run -it ubuntu
[email protected]:/#

In order to come out of the container, you need to type exit in the terminal.

docker rm

Moving on to the next command — if we want to delete a container, we use the docker rm command.

The syntax for this command is as shown below:

docker rm [options] CONTAINER ID/NAME [CONTAINER ID/NAME...]

Some of the examples of using this command are shown below:

$ docker rm 30fa elated_franklin

In the above example, we are instructing Docker to delete 2 containers within a single command. The first container to be deleted is specified using its container ID, and the second container to be deleted is specified using its name.

Please Note: The containers need to be in a stopped state in order to be deleted.

docker images

docker images is the next command on the list.

This command lists out all the Docker Images that are present on your Docker Host.

$ docker images
REPOSITORY TAG IMAGE CREATED SIZEmysql latest 7bb2586065cd 38 hours ago 477MBhttpd latest 5eace252f2f2 38 hours ago 132MBubuntu 16.04 9361ce633ff1 2 weeks ago 118MBubuntu trusty 390582d83ead 2 weeks ago 188MBfedora latest d09302f77cfc 2 weeks ago 275MBubuntu latest 94e814e2efa8 2 weeks ago 88.9MB

Let us decode the output of the docker images command.

REPOSITORY: This represents the unique name of the Docker Image.

TAG: Each image is associated with a unique tag. A tag basically represents a version of the image.

A tag is usually represented either using a word or set of numbers or a combination of alphanumeric characters.

IMAGE ID: A unique string consisting of alpha-numeric characters, associated with each image.

CREATED: This shows the time elapsed since this image has been created.

SIZE: This shows the size of the image.

docker rmi

The next command on the list is the docker rmi command.

The docker rmi command allows us to remove an image(s) from the Docker Host.

The syntax for this command is as shown below:

docker rmi [options] IMAGE NAME/ID [IMAGE NAME/ID...]

Some of the examples of using this command are shown below:

docker rmi mysql

The above command removes the image named mysql from the Docker Host.

docker rmi httpd fedora

The above command removes the images named httpd and fedora from the Docker Host.

docker rmi 94e81

The above command removes the image starting with the image ID 94e81 from the Docker Host.

docker rmi ubuntu:trusty

The above command removes the image named ubuntu, with the tag trusty from the Docker Host.

These were some of the basic Docker commands you will see. There are many more Docker commands to explore.

Wrap-Up

Kontainer baru-baru ini mendapat perhatian yang sewajarnya, walaupun sudah lama wujud. Sebilangan syarikat teknologi tinggi seperti Google, Amazon Web Services (AWS), Intel, Tesla, dan Juniper Networks mempunyai versi enjin kontena tersendiri. Mereka sangat bergantung pada mereka untuk membina, menjalankan, mengurus, dan menyebarkan aplikasi mereka.

Docker adalah mesin pengkontaineran yang sangat kuat, dan banyak yang ditawarkan ketika membina, menjalankan, mengurus dan menyebarkan aplikasi anda dengan cekap.

Anda baru sahaja melihat Docker pada tahap yang sangat tinggi. Terdapat banyak lagi yang perlu dipelajari mengenai Docker, seperti:

  • Perintah Docker (Perintah yang lebih kuat)
  • Imej Docker (Bina gambar tersuai anda sendiri)
  • Rangkaian Docker (Siapkan dan konfigurasikan rangkaian)
  • Perkhidmatan Docker (Mengelompokkan bekas yang menggunakan gambar yang sama)
  • Docker Stack (Grouping services required by an application)
  • Docker Compose (Tool for managing and running multiple containers)
  • Docker Swarm (Grouping and managing one or more machines on which docker is running)
  • And much more…

If you have found Docker to be fascinating, and are interested in learning more about it, then I would recommend that you enroll in the courses which are listed below. I found them to be very informative and straight to the point.

If you are an absolute beginner, then I would suggest you enroll in this course, which has been designed for beginners.

If you have some good knowledge about Docker, and are pretty much confident with the basic stuff and want to expand your knowledge, then I would suggest you should enroll into this course, which is aimed more towards advanced topics related to Docker.

Docker adalah kemahiran bukti masa depan dan hanya meningkatkan momentum.

Melaburkan masa dan wang anda untuk belajar Docker bukanlah sesuatu yang anda akan bertobat.

Harap anda dapati siaran ini memberi maklumat. jangan ragu untuk menyebarkannya. Ini sangat bermakna bagi saya.

Sebelum mengucapkan selamat tinggal ...

Mari berhubung, klik di sini untuk memasukkan alamat e-mel anda (Gunakan pautan ini jika widget di atas tidak muncul di skrin anda).

Terima kasih banyak kerana meluangkan masa berharga anda untuk membaca catatan ini.

Penafian: Semua nama produk dan syarikat adalah tanda dagang ™ atau tanda dagang® berdaftar dari pemegangnya masing-masing. Penggunaannya tidak menunjukkan sokongan oleh mereka. Mungkin ada pautan afiliasi dalam siaran ini.