我的个人博客:https://lixiaohui.live
最近了解到Docker也有Windows的版本,于是就想到在windows上利用Docker运行一个Ubuntu镜像,在容器里搭建Python开发环境,这样既解决了安装库的问题,也解决了VMWare虚拟机的不方便之处。
- 0、安装须知
- 1、安装Docker for Windows
- 2、初体验
- 2.1、查看Docker版本
- 2.2、下载镜像
- 2.3、运行下载的镜像
- 2.4、安装必要工具
- 2.4.1、安装vim以及其他工具并更换apt源
- 2.4.2、安装python以及必要开发库
- 2.5、打包镜像
- 2.6、使用容器
0、安装须知
需要注意的是,Windows上安装Docker要求系统需要支持Hyper-V,Hyper-V目前仅在Windows 10之后的版本支持,BIOS里需要启用Virtualization(虚拟化)。
1、安装Docker for Windows
安装程序可以从这里Docker for Windows
安装完之后,运行Docker for Windows。
Docker运行后可以在状态栏里看到有一个小鲸鱼的图标,如下所示
Note:需要注意的是,假如没有启动Hyper-V,Docker在尝试启动的时候会提示:
Hyper-V feature is not enabled.
Do you want to enable it for Docker to be able to work properly?
Your computer will restart automatically.
Note: VirtualBox will no longer work.
根据提示点击确定,电脑自动重启后就解决了,可能重启多次。
2、初体验
2.1、查看Docker版本
打开命令行窗口,运行下面的命令:
docker --version
输出
Docker version 18.09.0, build 4d60db4
2.2、下载镜像
docker hub上提供了很多docker镜像,我们以ubuntu:18.04为基础,打造我们的python开发环境。运行
docker pull ubuntu:18.04
- 此时电脑报错:
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
第一感觉是公司内网代理服务器的问题,在电脑右下角的鲸鱼图标右键,选择settings
,在左边栏选择proxies
,输入代理服务器地址以及端口,保存后Docker自动重启。
- 再次运行
docker pull ubuntu:18.04
提示:
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/alpine/manifests/latest: unauthorized: incorrect username or password.
Hmmm...这是因为没有登录Docker的缘故,输入命令:
docker login
cmder提示:
Authenticating with existing credentials...
Stored credentials invalid or expired
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (lixiaohui.co@gmail.com): leexiaohui
Password:
Login Succeeded
需要注意的是输入的应当是你的Docker ID,而不是邮箱地址。
- 再次运行
docker pull ubuntu:18.04
提示:
18.04: Pulling from library/ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
fa83472a3562: Pull complete
f85999a86bef: Pull complete
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:18.04
显示成功下载了Ubuntu 18.04。
2.3、运行下载的镜像
下载完后,我们来运行一下本地的镜像并进入到bash命令窗口
docker run -it ubuntu:18.04 bash
cmder显示:
docker run -it ubuntu:18.04 bash
root@f10d3bc7b9c0:/#
此时已经以root身份登录进来了。
2.4、安装必要工具
我们的这个镜像现在只是一个最基础的ubuntu的系统,我们需要安装许多工具,比如vim, ping, wget, tree等。
首先输入命令apt update
更新一下,但是发现提示:
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Could not connect to archive.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused)
Err:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Unable to connect to archive.ubuntu.com:http:
Err:3 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Unable to connect to archive.ubuntu.com:http:
Err:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Could not connect to security.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.26). - connect (111: Connection refused)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Could not connect to archive.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to archive.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused)
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease Could not connect to security.ubuntu.com:80 (91.189.88.161). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.23). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.149). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.152). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.88.162). - connect (111: Connection refused) Could not connect to security.ubuntu.com:80 (91.189.91.26). - connect (111: Connection refused)
W: Some index files failed to download. They have been ignored, or old ones used instead.
这是因为在代理服务器背后使用Docker也需要要给Docker Client配置代理服务器。
在Ubuntu中配置代理服务器十分简单,在命令窗口输入:
export http_proxy=http://proxyServer:port
export https_proxy=http://proxyServer:port
其中proxyServer是代理服务器,port是代理服务器的端口。
再次运行apt update
可以看见:
root@5cf711352182:/etc/apt# apt update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1364 B]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [119 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [264 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.7 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6954 B]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [574 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [735 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [2975 B]
Fetched 15.1 MB in 1min 16s (199 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
说明更新成功。
2.4.1、安装vim以及其他工具并更换apt源
apt install vim
安装完后打开sources.list,修改其中的ubuntu源:
vim /etc/apt/sources.list
将下列内容粘贴到其中:
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
使用apt update
更新后安装其他的工具(wget\ping\tree):
apt install iputils-ping wget net-tools
反应太迟钝了,不体验了byebye。
2.4.2、安装python以及必要开发库
apt install python3.6
# 生成软连接
ln -s /usr/bin/python3.6 /usr/bin/python
安装pip
apt install python3-pip
配置pip源
在.pip/pip.conf
中写入(若无则新建)
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com
安装必要的库:
在/root
下面生成一个文件re.txt,在re.txt中写入常用的库:
pandas
numpy
scikit-learn
matplotlib
bs4
requests
sqlalchemy
xlrd
pyodbc
运行命令:
pip install -r re.txt
等待安装完成即可。至此,我们的开发环境配置好了。
2.5、打包镜像
为方便以后的使用,我们把这个配置好的容器打成一个新的镜像。在容器中执行exit退出,我们现在来到了windows的命令行窗口。我们查看一下我们刚刚配置好的容器,运行docker ps
可以看到我们刚刚配置好的容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2d37f4a28e2 d55117059123 "bash" 23 minutes ago Up 23 minutes dazzling_visvesvaraya
运行下面的命令,提交更改,将容器打包成一个新的镜像
docker commit b2d37f4a28e2 leexiaohui/python_dev:ver1
我们再来查看一下本地的镜像
docker images
输出
REPOSITORY TAG IMAGE ID CREATED SIZE
leexiaohui/python_dev ver1 21fec1175d00 11 minutes ago 927MB
ubuntu 18.04 93fd78260bd1 7 days ago 86.2MB
可以看到,现在我们本地有2个镜像了。TAG为18.04的是原始的镜像,TAG为ver1的是我们刚打包好的镜像。
2.6、使用容器
之后我们就可以基于我们定制的镜像,启动容器做开发了。启动容器很简单,只要运行
docker run -it 21fec1175d00 bash
需要注意的是,容器如果被删除了,其中的更改也会丢失。要保存容器中的更改,需要像上面这样把更改commit到镜像中。
当不小心退出了容器的时候,执行docker exec -it 21fec1175d00 bash
就可以重新进入到对应的容器中。
Enjoy.