在学习 **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 ubuntu /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
运行成功,开启愉快的学习之旅
设置密码登录模式
执行完下面的命令直接, 设置密码就可以
$ jupyter notebook --generate-config
$ jupyter notebook password
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c25bf700670c4cc0e0726328c3c4b0f2.png)
设置成功!
写于上海 至武汉的 G1720 高铁上!