在學習 **Hands-on Machine Learning with Scikit-Learn and TensorFlow: ** 的過程中, 想創建一個可以隨意變動的環境,在不同電腦上切換使用,不用每次安裝, 於是將環境都裝在 docker 中, 以供使用。 本博客只共記錄自己的學習查找使用, 大神請勿在此浪費時間。
https://github.com/ageron/handson-ml
配置環境
- 生成 ubuntu/jupyter 鏡像
$ docker pull ubuntu
$ docker run -i -t --name ubuntu_jupyter /bin/bash
root@fa066f33e235:/# apt update
root@fa066f33e235:/# apt install jupyter # 安裝 jupyter
root@fa066f33e235:/# ctrl + c # 退出
- 保存 ubuntu/jupyter 鏡像
$ docker commit ubuntu_jupyter ubuntu/jupyter # 將剛生成的container 儲存爲鏡像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu/jupyter latest 65de65424d23 34 minutes ago 641MB
learn/ping latest 82b6c745aa80 12 days ago 140MB
ubuntu latest 94e814e2efa8 6 weeks ago 88.9MB
learn/tutorial latest a7876479f1aa 6 years ago 128MB
- 下載handson_ml 官方歷程
Hands-on Machine Learning with Scikit-Learn and TensorFlow 書中的歷程在這裏 https://github.com/ageron/handson-ml
$ git clone https://github.com/ageron/handson-ml.git
$ docker run -i -t -p 8888:8888 -v /home/moi/Documents/handson-ml:/handson-ml --name handson_ml_jupyter ubuntu/jupyter /bin/bash
# -i 交互式; -t 打開終端; -p 將容器的8888 端口和 本機的8888 端口映射;
# -v 將本機的 handson-ml 映射到容器的 /handson-ml 目錄下; --name 本容器的名字
root@fa066f33e235:/# ls
bin dev handson-ml lib media opt root sbin sys usr
boot etc home lib64 mnt proc run srv tmp var
- 運行jupyter notebook 並運行 handson-ml 示例
root@fa066f33e235:/# jupyter notebook --allow-root --ip 0.0.0.0 --port=8888
本機打開
發現文件夾 handson-ml , 運行其中的文件
運行成功, 不過缺少一些依賴
- 新打開一個非同步窗口,安裝必要的依賴
docker attach 進去的終端都是同步的, 我們需要新打開一個容器的終端
$ docker exec -i -t handson_ml_jupyter /bin/bash
root@fa066f33e235:/# pip3 install numpy
root@fa066f33e235:/# pip3 install matplotlib
運行成功,開啓愉快的學習之旅
寫於上海 至武漢的 G1720 高鐵上!