Tạo ứng dụng Docker đầu tiên – Build Docker by soiqualang

Table of Contents

Tạo ứng dụng Docker đầu tiên - Build Docker by soiqualang

Sau khi tìm hiểu sơ về Docker (mọi người có thể xem ở dưới)

https://dothanhlong.org/tag/docker/

Mình muốn tự tạo Docker, để thực hành thì mình quyết định chuyển cái tool download video từ Youtube thành tool chạy bằng Docker để dùng. Về cơ bản thì tool này mình viết bằng Python. Mọi người có thể chạy bằng Google colab ở đây

https://colab.research.google.com/gist/soiqualang/164c8ec8f13253acca4a2e09788fdbae/-public-tool-python-youtube-by-soiqualang.ipynb

Để chạy được tool này thì người dùng càn cài python, sau đó là cài các packages cần thiết rồi mới chạy tool được. Rất cực. Chắc có người bảo sao mình không đóng gói ra file thực thi thì sẽ không cần phải cài lằn nhằn, như người dùng có thể dùng Window, Linux, Mac,.. mỗi nền tảng phải build ra file thực thi riêng => cách này không khả thi. Do vậy mình mới dự định build ra Docker, bất cứ máy nào cài Docker là có thể pull về dùng, không cần cài đặt, cấu hình thêm gì cả. Sẵn đây là cơ hội để mình vọc thử cách build Docker =))

Bắt tay làm thôi.


T-Youtube

A tools to download video, mp3, transcripts from Youtube

Repo này để mình thực hành tạo Docker, mình sẽ chuyển cái tool python của mình sang docker để ai cũng có thể chạy được mà ko cần cài đặt các python packages ^^

Hướng dẫn về Docker

https://dothanhlong.org/huong-dan-docker-toan-tap/

https://colab.research.google.com/drive/1E4B0xMXFpcX13gpPTx4scNP_hRulNxPG

https://github.com/quangvublog/Docker/blob/master/Dockerfile

Trong này mình vẫn còn vướng mắc:

  • Làm sao để ghi file ra ngoài môi trường Docker (khi download video, mp3)
  • Gọi lệnh vô môi trường Docker để chạy tool
  • Có cần tạo virtualenv để cài các packages không, nếu có thì có dùng lại virtualenv ở local được không?

Setup Local

Virtual Env

virtualenv tyoutube_env

pip install autosub
pip install pytube
pip install youtube_dl

pip freeze > requirements.txt

Build tool

# List video
python tyb.py -download_mode "list" -youtube_url "PLhPvsstp6O2QTcrY20ysXRbyRo30am87X" -download_path "keochanh_mp3"
# hoặc
python tyb.py -download_mode "list" -youtube_url "https://www.youtube.com/playlist?list=PLhPvsstp6O2QTcrY20ysXRbyRo30am87X" -download_path "keochanh_mp3"

# a video
python tyb.py -download_mode "video" -youtube_url "https://www.youtube.com/watch?v=tNfGBssfCmE" -download_path "keochanh_mp3"

# a mp3
python tyb.py -download_mode "mp3" -youtube_url "https://www.youtube.com/watch?v=tNfGBssfCmE" -download_path "keochanh_mp3"

Build Docker ra image

# Tên docker không được viết hoa
docker build -t tyoutube:1.0 .

Run Docker

# Tạo và start container
# Test local
docker run -it -d --name tyoutube -v D:/sync/websvr/docker/tmp:/data tyoutube:1.0
# Test sau khi push lên docker hub
docker run -it -d --name tyoutube -v D:/sync/websvr/docker/tmp:/data soiqualang/tyoutube:1.0

# Stop container
docker stop tyoutube

# Start container
docker start tyoutube

# Run tool
# cli to container
docker exec -it tyoutube /bin/sh

python tyb.py -download_mode "mp3" -youtube_url "https://www.youtube.com/watch?v=tNfGBssfCmE" -download_path "/data/keochanh_mp3"
python tyb.py -download_mode "video" -youtube_url "https://www.youtube.com/watch?v=tNfGBssfCmE" -download_path "/data/keochanh_mp3"

# Thoát cli
exit

# Hoặc chạy trực tiếp từ bên ngoài docker
docker exec -it tyoutube python tyb.py -download_mode "mp3" -youtube_url "https://www.youtube.com/watch?v=tNfGBssfCmE" -download_path "/data/keochanh_mp3"

# Remove container
docker rm tyoutube

Push to Dockerhub

docker login -u soiqualang
docker tag tyoutube:1.0 soiqualang/tyoutube:1.0
docker push soiqualang/tyoutube:1.0

Tổng kết

  • Làm sao để ghi file ra ngoài môi trường Docker (khi download video, mp3)
    • Dùng tham số -v D:/sync/websvr/docker/tmp:/data để mout thư mục data trên docker với thư mục tmp ở máy mình
  • Gọi lệnh vô môi trường Docker để chạy tool
    • Dùng tham số exec -it để truyền lệnh chạy trong môi trường docker
  • Có cần tạo virtualenv để cài các packages không, nếu có thì có dùng lại virtualenv ở local được không?
    • Cài này tùy bạn, nếu docker đơn giản, chỉ chạy 1 thứ thì không cần, còn phức tạp thì có thể tạo để dễ quản lý

Kết quả trên Docker hub

https://hub.docker.com/repository/docker/soiqualang/tyoutube

Mọi người có thể pull về dùng thử nha. Mình có note cách dùng ở đó luôn.

Preferences

https://viblo.asia/p/docker-run-vs-cmd-vs-entrypoint-Az45boVgKxY

https://blog.cloud365.vn/container/tim-hieu-docker-phan-4/

https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile

Leave a Reply

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