Cài đặt Docker

Table of Contents

Lưu ý trên Ubuntu (Cập nhật 28/12/2018)

$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable edge" https://unix.stackexchange.com/questions/363048/unable-to-locate-package-docker-ce-on-a-64bit-ubuntu

Noticing "edge" word is added. If you already ran this command before without "edge". You can edit the source.list file at /etc/apt/sources.list. After that, refresh and install docker-ce as usual:

sudo apt-get update
sudo apt-get install docker-ce

I. Chuẩn bị một chút:

Update the apt package index:

$ sudo apt-get update

Install packages to allow apt to use a repository over HTTPS:

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

Add Docker’s official GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

$ sudo apt-key fingerprint 0EBFCD88

pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) [email protected]
sub 4096R/F273FCD8 2017-02-22

Use the following command to set up the stable repository

$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

II. Cài đặt docker CE:

Update the apt package index:

$ sudo apt-get update

Install the latest version of Docker CE, or go to the next step to install a specific version. Any existing installation of Docker is replaced.

$ sudo apt-get install docker-ce

Verify that Docker CE is installed correctly by running the hello-world image.

$ sudo docker run hello-world

4. Hoạt động như thế nào ?

  • Docker image là nền tảng của container, có thể hiểu Docker image như khung xươnggiúp định hình cho container, nó sẽ tạo ra container khi thực hiện câu lệnh chạy image đó. Nếu nói với phong cách lập trình hướng đối tượng, Docker image là class, còn container là thực thể (instance, thể hiện) của class đó.

Docker có hai khái niệm chính cần hiểu, đó là image và container:

  • Container: Tương tự như một máy ảo, xuất hiện khi mình khởi chạy image.Tốc độ khởi chạy container nhanh hơn tốc độ khởi chạy máy ảo rất nhiều và bạn có thể thoải mái chạy 4,5 container mà không sợ treo máy.Các files và settings được sử dụng trong container được lưu, sử dụng lại, gọi chung là images của docker.
  • Image: Tương tự như file .gho để ghost win mà mấy ông cài win dạo hay dùng.Image này không phải là một file vật lý mà nó chỉ được chứa trong Docker.Một image bao gồm hệ điều hành (Windows, CentOS, Ubuntu, …) và các môi trường lập trình được cài sẵn (httpd, mysqld, nginx, python, git, …).Docker hub là nơi lưu giữ và chia sẻ các file images này (hiện có khoảng 300.000 images)Bạn có thể tìm tải các image mọi người chia sẻ sẵn trên mạng hoặc có thể tự tạo cho mình một cái image như ý.

5. Các câu lệnh trong Docker

  • Chuẩn chỉnh & đầy đủ nhất thì bạn cứ tham khảo trên trang chủ của docker docs. Còn ở bài viết này sẽ trích dẫn những câu lệnh cơ bản nhất giúp các bạn nhanh chóng nắm bắt:
  1. Pull một image từ Docker Hub

sudo docker pull image_name

  1. Tạo mới container bằng cách chạy image, kèm theo các tùy chọn:

sudo docker run -v <forder_in_computer>:<forder_in_container> -p <port_in_computer>:<port_in_container> -it <image_name> /bin/bash

Ví dụ:

sudo docker pull ubuntu:16.04

sudo docker run -it ubuntu:16.04 /bin/bash

  • Bây giờ bạn đã dựng thành công một môi trường ubuntu ảo rồi đó.

Câu lệnh

uname -a

sẽ hiển thị thông tin của Kernel ubuntu, cùng so sánh nhé:

Kết quả của dòng uname-a thứ nhất là thông tin Kernel của máy ảo (tức là của container)

Linux 5ed7d9f282fe 4.15.0-36-generic #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Kết quả của dòng uname-a thứ hai là thông tin Kernel của máy "thật" (Linux) bạn đang dùng.

Linux hoanki-Nitro-AN515-51 4.15.0-36-generic #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

-> Thông tin nhân Kernel như nhau nhé ! Do container sử dụng chung tài nguyên với host OS mà.

  • Một vài câu lệnh khác:

docker images: Liệt kê các images hiện có

docker rmi {image_id/name}: Xóa một image

docker ps: Liệt kê các container đang chạy

docker ps -a: Liệt kê các container đã tắt

docker rm -f {container_id/name}: Xóa một container

docker start {new_container_name}: Khởi động một container

docker exec -it {new_container_name} /bin/bash: Truy cập vào container đang chạy

Leave a Reply

Your email address will not be published. Required fields are marked *