Docker là một nền tảng để cung cấp cách để building, deploying và running ứng dụng dễ dàng hơn bằng cách sử dụng các containers. Hướng dẫn này nhằm mục đích thiết lập và cài đặt Docker trên Ubuntu.
Ubuntu (phát âm IPA uːˈbuːntuː) là một hệ điều hành máy tính dựa trên Debian GNU/Linux, một bản phân phối Linux thông dụng. Ubuntu là phần mềm mã nguồn mở có nghĩa là người dùng được tự do chạy, sao chép, phân phối, nghiên cứu, thay đổi và cải tiến phần mềm theo điều khoản của giấy phép GNU GPL.
Docker là một nền tảng phần mềm được thiết kế để giúp tạo, triển khai và chạy các ứng dụng dễ dàng hơn.
Tài liệu tham khảo:
1. Hướng dẫn cài đặt Docker trên Ubuntu
Đầu tiên tiến hành cập nhật hệ thống
1 |
sudo apt update |
Tiếp theo cài đặt một vài gói cho phép apt sử dụng các gói qua HTTPS
1 |
sudo apt install apt-transport-https ca-certificates curl software-properties-common |
Thêm khóa GPG cho kho lưu trữ Docker chính thức vào hệ thống
1 |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
Thêm kho lưu trữ Docker vào các nguồn APT
1 |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" |
Sau khi thêm kho lưu trữ Docker thì bạn tiến hành cài đặt Docker trên Ubuntu
1 |
sudo apt update |
Đảm bảo rằng bạn cài đặt từ kho Docker thay vì kho mặc định của Ubuntu
1 |
apt-cache policy docker-ce |
Cuối cùng là thực hiện cài đặt Docker
1 |
sudo apt install docker-ce |
Khi quá trình cài đặt Docker trên Ubuntu hoàn tất thì dịch vụ Docker sẽ tự khởi động. Chúng ta sẽ kiểm tra việc cài đặt đã thành công hay chưa, số phiên bản cho Docker có thể khác nhau.
1 |
sudo systemctl status docker |
2. Thiết lập thực thi lệnh Docker mà không sử dụng SUDO
Nếu bạn muốn tránh nhập sudo khi chạy lệnh docker, hãy thêm tên người dùng của bạn vào nhóm docker
1 |
sudo usermod -aG docker ${USER} |
Để áp dụng tư cách thành viên nhóm mới, hãy đăng xuất khỏi máy chủ và đăng nhập lại hoặc nhập như sau
1 |
su - ${USER} |
Nếu bạn cần thêm người dùng vào nhóm docker mà bạn chưa đăng nhập, hãy khai báo tên người dùng đó một cách rõ ràng bằng cách sử dụng
1 |
sudo usermod -aG docker <username> |
Hãy thay <username> bằng USER của bạn muốn sử dụng.
3. Sử dụng lệnh Docker
Cú pháp lệnh Docker có dạng như sau
1 |
docker [option] [command] [arguments] |
Danh sách đầy đủ các lệnh Docker bao gồm
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 |
Output attach Attach local standard input, output, and error streams 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 to files or directories 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 |
Ví dụ sau đây sẽ khởi chạy Docker
1 |
docker run hello-world |
Kết quả sau khi chạy lệnh trên
Docker ban đầu không thể tìm thấy hình ảnh hello-world cục bộ vậy nên sẽ tải hình ảnh xuống từ Docker Hub.
Lời kết: Như vậy nội dung bài viết này mình đã hướng dẫn cài đặt Docker trên Ubuntu với các phiên bản như Ubuntu 18 hay Ubuntu 20. Ngoải ra, các bạn có thể tham khảo thêm các bài viết khác trong chuyên mục Docker.
Tác giả: Truong Duong