Docker là một giải pháp ảo hóa mã nguồn mở có thể đóng gói và chạy bất kỳ phần mềm nào dưới dạng một Container gọn nhẹ.
Ảo hóa là giải pháp giúp doanh nghiệp tiết kiệm chi phí vận hành và tận dụng tối đa phần cứng. Mặc dù với nguyên lý hoạt động khác nhau, chạy trên nền hệ điều hành hay không qua hệ điều hành, các phương pháp ảo hóa cũ vốn ảo hóa mọi thứ của máy con từ OS cho đến bộ nhớ và lưu trữ, vì vậy mà bất kỳ một máy ảo nào cũng chiếm một khối lượng nhất định tài nguyên hệ thống.
Docker đưa ra một giải pháp mới cho vấn đề ảo hóa, thay vì tạo ra các máy ảo con chạy độc lập kiểu hypervisors (tạo phần cứng ảo và cài đặt hệ điều hành lên đó), các ứng dụng sẽ được đóng gói lại thành các Container (Công-ten-nơ) riêng lẻ. Các Container này chạy chung trên nhân hệ điều hành qua LXC (Linux Containers), chia sẻ chung tài nguyên của máy mẹ, do đó, hoạt động nhẹ và nhanh hơn các máy ảo dạng hypervisors.
Vậy cài đặt Docker, sử dụng Docker như thế nào cho “đúng chuẩn”, bài viết này sẽ giúp bạn “How to use Docker for Dummies” 😀
Bước 1 – Cài đặt Docker
Update package database
1
|
$ sudo apt-get update
|
Add the GPG key Docker repository
1
|
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
|
Add the Docker repository to APT sources
1
|
$ sudo apt-add-repository 'deb https://apt.dockerproject.org/repo ubuntu-xenial main'
|
Update the package database with the Docker packages from the newly added repo
1
|
$ sudo apt-get update
|
Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo
1
|
$ apt-cache policy docker-engine
|
1
2
3
4
5
6
7
8
9
10
|
Output
docker-engine:
Installed: (none)
Candidate: 1.11.1-0~xenial
Version table:
1.11.1-0~xenial 500
500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
1.11.0-0~xenial 500
500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
|
Cài đặt Docker Engine
1
|
$ sudo apt-get install -y docker-engine
|
Kiểm tra cài đặt thành công, daemon started
1
|
$ sudo systemctl status docker
|
1
2
3
4
5
6
7
|
Output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago
Docs: https://docs.docker.com
Main PID: 749 (docker)
|
Khi cài đặt, Docker không chỉ cài đặt dịch vụ daemon mà còn thêm bộ công cụ command line.
Bước 2 – Sử dụng Docker Command với quyền not-root
Mặt định Docker sẽ run dưới quyền root (sudo), nhưng user thường cũng có thể run docker nếu được add vào docker group. Nếu user chưa được add vào docker, khi run sẽ hiện lỗi
1
2
|
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
|
Add user vào docker group
1
|
$ sudo usermod -aG docker $(whoami)
|
Apply permission
1
|
$ newgrp docker
|
Bước 3 – Sử dụng Docker Command
To view all available subcommands, type
1
|
$ docker
|
List command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
toanalien@idn:~$ docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/toanalien/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/toanalien/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/toanalien/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/toanalien/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
|
Hello World with Docker
1
|
$ docker run hello-world
|
1
2
3
4
5
|
Output
Hello from Docker.
This message shows that your installation appears to be working correctly.
...
|
Ghost with Docker
1
|
$ docker run --name hello-ghost -p 8080:2368 -d ghost
|
–name: tên của container
-p: bind port từ docker-engine ra host machine, trong vd là từ port 2368 ra port 8080 host machine
-d: tên image trên Docker Hub

Chạy được rồi !

Như vậy với những bước cơ bản chúng ta đã có thể sử dụng được Docker. Những bài viết tiếp theo mình sẽ đi sâu vào các chức năng nâng cao của Docker. Cảm ơn các bạn đã đón đọc!

Không có nhận xét nào:
Đăng nhận xét