Skip to content

Instantly share code, notes, and snippets.

@tranphuquy19
Last active February 9, 2020 16:22
Show Gist options
  • Save tranphuquy19/8a6b364ca491672425e0d8097659511d to your computer and use it in GitHub Desktop.
Save tranphuquy19/8a6b364ca491672425e0d8097659511d to your computer and use it in GitHub Desktop.
Docker Notes

Danh sách các lệnh thường sử dụng trong Docker CLI

Table of contents

  1. Docker Image
    Liệt kê các images có sẵn trong docker
    Tải images từ DockerHub
    Xóa image
    Tìm images từ DockerHub
    Run image

  2. Docker Container
    Tương tác ngoài container
    Tương tác trong container
    Xóa container
    Commit container

  3. Container commit
    Xuất image thành file
    Load image file vào docker
    Set image name và image tag

  4. Docker Volume
    Chia sẻ thư mục giữa host và container
    Chia sẻ thư muc giữa các containers
    Tạo volume mới
    Mapping thư mục trên Host vào volume khi tạo
    Duyệt các volumes
    Xem thông tin volume
    Xóa volumes
    Run và mount volume vào container



Liệt kê các images có sẵn trong docker

docker images


Tải images từ DockerHub

docker pull <image_name>:<tag_name>

  • tag_name: default lastest

Xóa image

docker image rm [IMAGE]


Tìm images từ DockerHub

docker search <image_name>


Run image

docker run {option} [IMAGE] {command}

Các options:

Short Option Meaning
t --tty mở một thiết bị đầu cuối (terminal)
i --interactive cho phép tương tác với container
d --detach chạy ngầm container
h --hostname <string> đặt hostname cho container
--name <string> set name cho container
-v --volume <pathDirHost>:<pathDirContainer> Mount thư mục từ host vào container
--rm tự động xóa container sau khi exist

Duyệt các container

docker ps {options}

Các options:

Option Meaning
a tìm tất các container running & stopped

Tương tác ngoài container

  • Chạy container: docker start [CONTAINER], use option -i to --interactive this container!
  • Đăng nhập lại running container: docker attach [CONTAINER]
  • Dừng container: docker stop [CONTAINER]
  • Chạy 1 lệnh: docker exec [CONTAINER] <string_command>, ex: docker exec ubuntu01 ls
  • Mở thêm 1 terminal mới (new session): docker exec -it [CONTAINER] bash

Tương tác trong container

  • Thoát container, stop container: exit
  • Thoát nhưng vẫn giữ container chạy: Ctrl+P Ctrl+Q

Xóa container

docker rm {options} [CONTAINER]

Các options:

Short Option Meaning
f --force Xóa kể cả khi container đang chạy

Commit container

  • Note: Nếu muốn commit một container thì container đó phải ở trạng thái stopped

docker commit [CONTAINER] <image_name>:<tag_string>


Xuất image thành file

docker save -o <filename>.tar [IMAGE]


Load image file vào docker

docker load -i <filepath>.tar

  • Note: Sau khi load image vào docker thì ta cần set name và tag cho image đó, chi tiết thực hiện mô tả trong bước tiếp theo

Set image name và image tag

docker tag [IMAGE_ID] <image-name>:<image_id>

  • Note: Có thể dùng cách này để đổi name và tag của image hiện có sang name và tag mới

Chia sẻ thư mục giữa host và container

docker run {options} -v <pathDirHost>:<pathDirContainer> [IMAGE]


Chia sẻ thư mục giữa các containers

  • Thực hiện chia sẻ thư mục giữa các CONTAINER1CONTAINER2

docker run -it {option} --name 'CONTAINER2' --volumes-from [CONTAINER1] [IMAGE]


Tạo volume mới

docker volume create <volume_name>


Mapping thư mục trên Host vào volume khi tạo

docker volume create --opt device=<path_host> --opt type=none --opt o=bind <volume_name>


Duyệt các volumes

docker volume ls


Xem thông tin volume

docker volume inspect <volume_name>


Xóa volumes

docker volume rm <volume_name>


Run và mount volume vào container

docker run {option} --mount source=<volume_name>,target=<dir_path_on_container> [IMAGE]

  • Note: loại ổ đĩa này không thể sử dụng option --mount khi gắn vào container khi chạy mà phải sử dụng option -v

Danh sách các câu hỏi về Docker

Table of contents







Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment