Doker入门一条龙:Docker四要素,容器与虚拟机,Win10中设置容器与Windows文件夹共享,Docker上运行的含Ubuntu内核的容器中搭建Python数据分析环境,导出容器到win10

    科技2025-08-17  22

    前言


       这两天正好在学习Docker,于是把学习心得记录在这里,同为初学,若有表述错误的地方,还望不吝指出。对于Docker,其官方文档上有一段这样的描述:

    借助Docker,您可以以与管理应用程序相同的方式来管理基础架构。通过利用Docker的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。

       回想一下我们是怎样管理应用程序的,或许是push到github,然后你的伙伴可以从那上面git clone一份下来,然后把玩一下。不过他能够玩得起来的前提是,他的电脑上有和你相同的运行环境——或许这就是基础架构,如果没有,他可能要根据你的指示去install各种软件,使出多年来安装软件修炼的功力。那你可能会问,我可不可以把运行环境打包一起发给他呢,Docker似乎就做了这件事。

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130


    Docker四要素


    镜像(image)

       个人理解,这里的镜像就是一堆代码按照一定规则打的包,即代码包,不过这些代码不是寻常的应用程序,而是基础架构代码。比如ubuntu官方发布ubuntu镜像。

    仓库( registries)

       存放镜像的地方,一般指一些大的服务器。企业可以搭建自己的专用仓库,如果pull了很多镜像到你的本地,那你的电脑可能也是私人个小仓库。Docker中央大仓库是Docker Hub,由于它在国外,国内下载龟速,所以国内用的人少。国内主要是用,网易蜂巢,daoCloud,阿里云,中科大等的镜像源。

    容器(container)

       有人说容器是运行着的镜像,我觉得有些怪。我觉得应该是镜像在容器中运行,或者容器盛放运行着的镜像。任意运行一个相同或者不同的镜像,都会生成一个新的容器,如果运行镜像时没有为容器命名,那么Docker会给任意给一个很神奇的名字。对于容器,个人的理解它可能就是一种隔离,并且容器有让其中的镜像使用系统内核的统一的接口。

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130

    Docker

       Docker是运行在主机上的一个应用程序,它的内核好像也是一个linux。通过它才可以在在主机上运行与管理容器,所以笔者将其列为了第四要素。在Win10上,Docker可以用GUI对容器进行管理,也可以用命令行。

       四要素间的关系,如图所示,图源。


    Docker容器与虚拟机


       个人理解,容器没有自己独立的操作系统,相当于一个运行在主机系统内核上的,与其他容器和应用程序隔离的应用程序。而虚拟机必须有独立的操作系统,相当于是在硬件设备上运行与主机操作系统环境隔离的另一个操作系统环境。示意图如下,图源。虚拟机可以成为容器的主机,容器可以在虚拟机上运行。


    Win10下Docker安装、与加速


    系统

       Win10Pro

    下载

       Docker稳定版

    安装

       默认安装,如果你的操作系统是1803,安装完成后应该会弹出一个窗口,提示:

    Before you install the Docker Desktop WSL 2 backend, you must complete the following steps: 1、Install Windows 10, version 2004 or higher. The Docker Desktop Edge release also supports Windows 10, version 1903 or higher. 2、Enable WSL 2 feature on Windows. For detailed instructions, refer to the Microsoft documentation. 3、Download and install the Linux kernel update package.

       如果直接关掉,会导致Docker启动失败。按照他的提示Download and install the Linux kernel update package,安装WSL即可。

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130

    设置

         右击大海豚,选择设置,在设置里添加镜像源。

      我把General中的WSL2的√去掉了。因为在win10上使用wsl2引擎启动docker,笔者不知道下载镜像的保存路径,而且不知道怎么变更路径,因此就选择了性能要差一些的hyper-V。

      把General中的WSL2的√去掉后,Resource中就会出现如下内容。可以在这里设置Docker能够使用的资源大小,包括CPU,内存,交换分区,和硬盘大小。当然,最后一行还可以更改镜像存放的位置。

      在Docker可以添加仓库的镜像源,我用了一个Docker中国和一个中科大的镜像源。设置完关掉软件即可。

    “https://registry.docker-cn.com”, “https://docker.mirrors.ustc.edu.cn/”


    Win10下Docker测试与简单使用


      打开PowerShell。

    查看Docker版本

    docker --version

    查看Docker帮助

    docker -h

      此时会弹出如下的Docker命令,和命令解释,我只留下了一会儿可能会用到的命令。

    Management Commands: container Manage containers image Manage images volume Manage volumes Commands: cp Copy files/folders between a container and the local filesystem create Create a new container exec Run a command in a running container export Export a container's filesystem as a tar archive images List images kill Kill one or more running containers load Load an image from a tar archive or STDIN ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container version Show the Docker version information

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130

    运行Hellow-world镜像

      它在仓库的这个位置,先把它Pull下来。

    docker pull hello-world:latest #拉去hello-world最新版

      pull成功后,查看本地镜像,当然DockerGUI中也可以查看,并且可以直接在哪里run。

    docker images

      不过用命令来run它。如下表示在一个名为hello的容器中运行hello-world镜像。如果看到“Hello from Docker!”就说明运行成功了,并且Docker安装没有问题。

    docker run --name hello hello-world

      查看一下我们当前本地的容器

    docker ps -a # -a 表示显示所有容器,包括没有运行的

      输出,ID是容器的唯一标识。

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e708d8206fb3 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago hello

      测试完成,删除hello容器与hello镜像。

    docker rm hello docker ps -a docker rmi hello-world docker images

    Docker运行含ubuntu的容器,并设置其与Win10文件夹共享


    拉取镜像

    docker pull ubuntu:latest # 拉去ubuntu官方最新版内核

      建立容器中某个路径与win10中某个路径的映射,即共享文件夹,通过数据卷形式——volume。作用就相当于把win10中的某个文件夹给容器当U盘用。

    建立映射

      现在某个盘里建个文件夹来作为容器的共享文件夹,我是在docker镜像存放路径下建的,即:D:\docker\ubuntu_python3 —>/D/docker/ubuntu_python3。执行如下代码。

    docker run -itd --name ubuntu_python3 -v /D/docker/ubuntu_python3:/shared_data ubuntu # --name ubuntu_python3 容器名 # -itd 后台运行,可以交互 # -v volume数据卷 # /F/DockerDesktop/ubuntu_python3:/shared_data 即win10下的文件夹:容器里的文件夹 # ubuntu 待运行的镜像

      share it

    测试

    docker exec -it ubuntu_python3 bash # exec 进入运行着的容器,通过bash交互

      键入ls命令,会发现容器根目录下多了个shared_data目录,它就是新产生共享目录。它本质上就是那个U盘,只是在容器里换了个新名字。

      进入该文件夹,touch一个新文件,test.txt。

      此时查看windows中,共享目录下也出现了一个刚刚创建test.txt文件。到此证明windows下D:\docker\ubuntu_python3与ubuntu_python3容器中的shared_data之间的映射建立成功,即两个文件夹变成了同一个文件夹。现在可以通过该文件夹实现windows的文件系统与容器文件系统间的数据传输。

      再次打开docker GUI,双击容器名,再点inspect,可以看见win10下的共享文件夹的确是以一个储存设备(就像是U盘)的形式挂载在容器中的,只是在容器中改了个名字。

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130


    在含ubuntu的容器ubuntu_python3中,搭建Python数据分析环境


      先在ubuntu_python3中已经运行着ubuntu,它是作为一个应用程序在win10里运行的。但那基本上只是内核,连vi这种最最基本的文本编辑器都没有,所以要安装好多东西。先更换镜像源,不然慢死。

    更换ubuntu镜像源

      由于笔者前面容器关了,现在启动一下。

    docker start ubuntu_python3 docker ps # ps 查看启动的容器

      进入容器。

    docker exec -it ubuntu_python3 bash

      进入保存镜像源路径文件sources.list的文件夹,将其备一份,避免出错。

    cd etc/apt cp sources.list sources_init.list

      移动sources.list到共享文件夹shared_data,我们在win10上编辑这个文本,因为没有更换镜像源,下载速度太慢了。

    mv sources.list /shared_data

      查看一下ubuntu版本。

    cat /proc/version #查看ubuntu版本

      在win10该容器共享文件夹内,用记事本打开这个文件。然后找到阿里云的ubuntu镜像列表,清华源列表…。然后将sources.list中所有的deb http://archive.ubuntu.com/…用这些源列表替换掉,保存。替换时注意关键词,如下是ubuntu 20.04的sources.list被替换后的结果。

      sources.list

    # 阿里云 deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse # 清华源 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # 保留的原来的 deb http://security.ubuntu.com/ubuntu/ focal-security main restricted deb http://security.ubuntu.com/ubuntu/ focal-security universe deb http://security.ubuntu.com/ubuntu/ focal-security multiverse

      将更改后的sources.list移回原位 /etc/apt

    mv /shared_data/sources.list /etc/apt

      更新源,稍等片刻镜像源就更换好了,然后就可以享受飞一般的速度。

    apt-get update

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130

    搭建Python数据分析环境

      这里就是安装各种包,简单记录一下。肯定先装文本编辑器vim。

      vim

    apt-get install vim

      python3

    apt-get install python3

      pip3

    apt-get install python3-pip

      更换一下pip3下载源,不然也慢的可以。

    cd root # 如果找不到root文件夹,exit退出后再重新 docker exec -it ubuntu_python3 bash 进入容器 mkdir .pip # 创建一个叫pip的隐藏文件夹 cd .pip touch pip.conf vim pip.conf

      随便找个源路径复制进去,我找的清华源,如下:复制到pip.conf里。此后pip3下载将如飞一般。

    [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ [install] trusted-host = pypi.tuna.tsinghua.edu.cn

      numpy

    pip3 install numpy

      pandas

    pip3 install pandas

      matplotlib

    pip3 install matplotlib

      scipy

    pip3 install scipy

      git

    apt-get install git

    copyright ©意疏:https://blog.csdn.net/sinat_35907936/article/details/108963130


    导出容器到win10


      导出的容器就可以拷贝给你的朋友们,进行分享了。最原始的分享方式,还没有涉及仓库。用以下命令,将容器归档到win10中的某个目录下。

    docker export -o d:/docker/ubuntu_python3.tar ubuntu_python3

      用解压软件打开容器归档,我们可以发现,这其实就是一个linux内核,再加上我们安装的那些软件。虽然它也是一个操作系统环境,但是神奇的是,它是以应用程序的身份在主机操作系统win10上运行的,而不是独立的立足于硬件的操作系统环境。


    参考


      https://blog.csdn.net/sss_369/article/details/99177432   https://blog.csdn.net/xfxf0520/article/details/82975366

    Processed: 0.011, SQL: 9