一文解決Docker安裝Anaconda環境問題(因爲坑我都踩完了) 直接放DEMO
如果對您有幫助或者解決了您的問題, 就幫我點個贊或者評論關注支持吧, 您的鼓勵是我寫博客的最大支持, 感謝!
最近有搞機器學習的同學找我裝環境,看着他各種環境配置.各種報錯,最後看不下去的我讓她用了Docker, 挽救了一下她的髮量
1. 搜索鏡像
搜索我們想要的anaconda鏡像:
docker search anaconda
2. pull鏡像
pull一個最高的
docker pull continuumio/anaconda3
3. 運行鏡像 image->容器
docker run -i -t -p 12345:8888 continuumio/anaconda3 /bin/bash
參數解釋
-i: 是 以交互模式運行容器,通常與 -t 同時使用;
-t: 爲容器重新分配一個僞輸入終端,通常與 -i 同時使用;
-p: 指定端口映射,格式爲:主機(宿主)端口:容器端口
然後可以看到進入了命令行
注意看目錄是 / 現在已經是容器裏了
4. 安裝xgboost包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xgboost
總安裝失敗的可以換成國內的鏡像源, 現在是清華的源
5. 運行jupyter notebook
在容器中啓動 jupyter notebook:
jupyter notebook --port 8888 --ip 0.0.0.0 --allow-root
把前面的127.0.0.1:8888(容器的ip和端口)改爲宿主機的ip和8888映射到宿主機的端口12345:
http://10.199.138.5:12345/?token=a6b3189e8f96802b6d193475f0e30908c3a2e16816e1a444
然後在本地瀏覽器中打開即可:
如果對您有幫助或者解決了您的問題, 就幫我點個贊或者評論關注支持吧, 您的鼓勵是我寫博客的最大支持, 感謝!
以下爲可選環節
6. 把當前容器打包爲新的鏡像
當前我們對原始的 anaconda3 鏡像做了一些改動:
- 安裝了xgboost
然後我們想把當前容器狀態打包爲新的鏡像,這樣以後我們就可以部署到其他地方了,而不用再安裝xgboost等等。
使用 docker commit 命令來將容器打包爲鏡像:
root@hz-sjfx-test-199-138-5:~# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <[email protected]>")
-c, --change list Apply Dockerfile instruction to the created image
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
docker commit
-a "nimendavid"
-m "nimendavid's first docker image based on Anaconda3 with xgboost and lightgbm"
dockername new_anaconda_xgboost
-a "nimendavid" 指的是作者
-m "nimendavid's......lightgbm" 指的是說明comment
dockername 指的是容器的短id,可以從命令行看出
new_anaconda_xgboost 指的是我們打包的新鏡像的名稱
7. 查看我們的新image
docker image ls
8. 上傳到 Docker Hub
docker tag new_anaconda_xgboost:latest nimendavid/machine_learning:v0.1