《AI开发者的docker实践》之环境篇

简介: 本节介绍docker 安装以及学习docker基础命令。

docker 安装

这里主要介绍 linuxWindows 10macOS 上的安装。

Linux

$ sudo curl -sS https://get.docker.com/ | sh

测试

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/


*如果机器有支持深度学习的Gpu,可以继续执行如下命令以支持容器对gpu的调用[目前仅支持linux]:

# Add the package repositoriesdistribution=$(. /etc/os-release;echo $ID$VERSION_ID)curl-s-L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -curl-s-L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudotee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker

点击下方课程链接即可直达Linux下安装Docker视频教程。

https://tianchi.aliyun.com/course/351/4127


windows


手动下载安装

点击以下链接下载 Stable (opens new window)Edge (opens new window)版本的 Docker Desktop for Windows。

下载好之后双击 Docker Desktop Installer.exe 开始安装。

使用 winget (opens new window)安装

# stable
$ winget install Docker.DockerDesktop
# edge
$ winget install Docker.DockerDesktopEdge

运行

在 Windows 搜索栏输入 Docker 点击 Docker Desktop 开始运行。

Docker 启动之后会在 Windows 任务栏出现鲸鱼图标。

等待片刻,点击 Got it 开始使用 Docker。


macOS

如果你已经安装了 Homebrew ,使用brew 安装非常方便

Homebrew (opens new window)Cask (opens new window)已经支持 Docker Desktop for Mac,因此可以很方便的使用 Homebrew Cask 来进行安装:

$ brew cask install docker


没有安装brew也可以手动下载安装

点击下载 Stable (opens new window)Edge (opens new window)版本的 Docker Desktop for Mac。

如同 macOS 其它软件一样,安装也非常简单,双击下载的 .dmg 文件,然后将那只叫 Moby (opens new window)的鲸鱼图标拖拽到 Application 文件夹即可(其间需要输入用户密码)。

运行

从应用中找到 Docker 并点击运行。

运行之后,会在右上角菜单栏看到多了一个鲸鱼图标,这个图标表明了 Docker 的运行状态。

image.png

100%随

2月26日周五

A

15:13:38

Sio

第一次点击图标,可能会看到这个安装成功的界面,点击 "Got it!" 可以关闭这个窗口。

以后每次点击鲸鱼图标会弹出操作菜单。

启动终端后,通过命令可以检查安装后的 Docker 版本。

$ docker --version
Docker version 19.03.8, build afacb8b
$ docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6

测试

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/


点击下方课程链接即可直达macOS下安装Docker视频教程。

https://tianchi.aliyun.com/course/351/4128


docker 基础命令学习

拉取镜像

docker pull [选项] [docker镜像地址:标签]


如:

docker pull hello-world:latest


运行镜像

$ docker run hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

运行镜像并进入容器

$ docker run -it --rm ubuntu:18.04 bash
root@e7009c6ce357:/#  uname -a
Linux bff9f261bab2 4.15.0-106-generic #107-Ubuntu SMP Thu Jun 4 11:27:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@e7009c6ce357:/# exit

docker run 就是运行容器的命令,后面如果只跟镜像,那么就执行镜像的默认命令然后退出。

  • -it:这是两个参数,一个是 -i:交互式操作,一个是 -t 终端。我们这里打算进入 bash 执行一些命令并查看返回结果,因此我们需要交互式终端。
  • --rm:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 docker rm。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 --rm 可以避免浪费空间。
  • ubuntu:18.04:这是指用 ubuntu:18.04 镜像为基础来启动容器。
  • bash:放在镜像名后的是 命令,这里我们希望有个交互式 Shell,因此用的是 bash

进入容器后,我们可以在 Shell 下操作,执行任何所需的命令。通过exit 退出。

查看本地镜像(list 镜像)

$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
redis                latest              5f515359c7f8        5 days ago          183 MB
nginx                latest              05a60462f8ba        5 days ago          181 MB

IMAGE ID 是镜像的唯一标识。

查看运行中的容器


$ docker ps
CONTAINER ID        IMAGE          COMMAND      CREATED             STATUS              PORTS               NAMES
9363b1b51118        testlog:1      "bash"       7 weeks ago         Up 7 weeks                              vigilant_bhaskara

CONTAINER ID 容器唯一id 可以通过指定这个ID操作exec shell进入容器、commit 这个容器的修改、tag 给这个容器打标签等

docker ps 罗列的是当前活跃的容器

要查看所有容器执行docker ps -a


$ docker ps -a


进入运行中/后台运行的容器


$ docker exec -it [CONTAINER ID] /bin/bash


进入运行中的容器后不仅可以调试镜像,还可以对镜像做修改如安装python包,如果想对修改做保留,可以执行3.7提交

保存修改

docker commit [CONTAINER ID] registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion

注意:通过commint的形式保存现场为一个新的镜像虽然也能直观的达到构建新镜像的目的,但是实际操作中,并不推荐这种形式,因为1.commit操作不仅会把有用的修改保存下来,对一些无关的修改也会保存下来(每一个命令行操作都会生成存储如ls操作)就会导致镜像比较臃肿;2.因为commit操作属于黑箱操作,后续如果有什么问题维护起来会比较麻烦。

建议commit仅作为保留现场的手段,然后通过修改dockerfile构建镜像。

打TAG

有时需要对临时版本,或者节点版本做一个标记保留,打TAG标签非常好用,并不会额外占用空间

docker tag registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion my_tmp_version:0.1

推送镜像到仓库

docker push registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion

使用dockerfile构建镜像

Dockerfile示例(注意一般文件名命名为Dockerfile 无后缀名,如果命名为其他名字,构建时需要额外指定文件名)

# Base Images
## 从天池基础镜像构建(from的base img 根据自己的需要更换,建议使用天池open list镜像链接:https://tianchi.aliyun.com/forum/postDetail?postId=67720)
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
##安装依赖包
RUN pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple 
##或者从requirements.txt安装
##RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple 
## 把当前文件夹里的文件构建到镜像的根目录下,并设置为默认工作目录
ADD . /
WORKDIR /
## 镜像启动后统一执行 sh run.sh
CMD ["sh", "run.sh"]


点击下方课程链接即可直达dockerdfile入门视频教程。

https://tianchi.aliyun.com/course/351/4129

image.png

构建镜像

docker build -t registry.cn-shanghai.aliyuncs.com/target:test .

如要指定dockerfile :

docker build -f ./dockerfile -t registry.cn-shanghai.aliyuncs.com/target:test .

删除镜像/容器

删除镜像:

docker rmi registry.cn-shanghai.aliyuncs.com/target:test

删除容器:

docker rm [CONTAINER ID]

如果容器还在运行,则会删除失败,应先结束掉容器:

docker kill [CONTAINER ID]

查看运行中的容器:

docker ps

查看所有容器:

docker ps-a


常规技巧

a.检查基础镜像软件源和pip源是否替换为国内源,如果非国内源后续每次构建镜像会比较浪费时间。

b.必备软件包可直接安装于基础镜像内,以减少每次构建镜像时都要安装一遍的等待时间。

c.镜像面临调试问题时,可交互式进入容器后直接调试修改,直到成功后退出再在dockerfile中修改。

d.养成使用Dockerfile的习惯,不要依赖于commit

e.每次镜像修改都给定新的版本号或标签,方便区分版本管理,有意义的版本最好使用有含义的字符作为版本号,如:frist_submit

深度学习常用镜像集合(包含国内源和海外源)

https://tianchi.aliyun.com/forum/postDetail?postId=67720

海外选手网速受限提交方案

a.使用github等代码托管(推荐code.aliyun.com)

b.在cr(阿里云容器服务)产品做代码源绑定,并开启自动构建

image.png

创建镜像仓库

2

仓库信息

代码源

云Code

本地仓库

私有GitLab

代码源

GitHub

Bitbucket

阿里云代码管理实现了私有并且安全的代码托管服务,提供目前最流行的

分布式版本控制系统Git来有效管理您的项目.

绑定账号

不使用缓存

构建设置

海外机器构建

代码变更自动构建镜像

请在仓库创建完成后前往构建页面设置

构建规则设置

取消

上一步

创建镜像仓库

c.构建完成设置hook自动提交(警惕次数消耗),或者谨慎起见构建完成后手动提交。

触发器设置方法:https://help.aliyun.com/document_detail/60949.html?spm=5176.8351553.0.dexternal.7f231991sUvASL


点击下方课程链接即可直达如何创建自己的基础镜像视频教程。

https://tianchi.aliyun.com/course/351/4131

相关文章
|
9天前
|
人工智能 自然语言处理 测试技术
从人工到AI驱动:天猫测试全流程自动化变革实践
天猫技术质量团队探索AI在测试全流程的落地应用,覆盖需求解析、用例生成、数据构造、执行验证等核心环节。通过AI+自然语言驱动,实现测试自动化、可溯化与可管理化,在用例生成、数据构造和执行校验中显著提效,推动测试体系从人工迈向AI全流程自动化,提升效率40%以上,用例覆盖超70%,并构建行业级知识资产沉淀平台。
从人工到AI驱动:天猫测试全流程自动化变革实践
|
12天前
|
消息中间件 人工智能 Kafka
AI 时代的数据通道:云消息队列 Kafka 的演进与实践
云消息队列 Kafka 版通过在架构创新、性能优化与生态融合等方面的突破性进展,为企业构建实时数据驱动的应用提供了坚实支撑,持续赋能客户业务创新。
135 21
|
6天前
|
人工智能 新制造
TsingtaoAI受邀参加宁波AI海曙科创训练营并分享技术落地实践
10月12日至15日,由宁波市海曙区组织部主办的AI海曙科创训练营在宁波成功举办。作为受邀企业代表,TsingtaoAI团队深入参与了多项活动,与政府领导、行业专家及科创企业代表围绕AI技术在制造业、成果转化等领域的实际应用展开交流,用真实案例诠释了“技术扎根产业”的价值逻辑。
26 2
|
15天前
|
人工智能 搜索推荐 算法
用AI提示词搞定基金定投:技术人的理财工具实践
本文将AI提示词工程应用于基金定投,为技术人打造一套系统化、可执行的理财方案。通过结构化指令,AI可生成个性化定投策略,覆盖目标设定、资产配置、风险控制与动态调整,帮助用户降低决策门槛,规避情绪干扰,实现科学理财。
185 13
|
16天前
|
存储 人工智能 NoSQL
AI大模型应用实践 八:如何通过RAG数据库实现大模型的私有化定制与优化
RAG技术通过融合外部知识库与大模型,实现知识动态更新与私有化定制,解决大模型知识固化、幻觉及数据安全难题。本文详解RAG原理、数据库选型(向量库、图库、知识图谱、混合架构)及应用场景,助力企业高效构建安全、可解释的智能系统。
|
13天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
本文介绍RAG(检索增强生成)技术,结合Spring AI与本地及云知识库实现学术分析AI应用,利用阿里云Qwen-Plus模型提升回答准确性与可信度。
397 90
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
|
5天前
|
人工智能 运维 关系型数据库
云栖大会|AI时代的数据库变革升级与实践:Data+AI驱动企业智能新范式
2025云栖大会“AI时代的数据库变革”专场,阿里云瑶池联合B站、小鹏、NVIDIA等分享Data+AI融合实践,发布PolarDB湖库一体化、ApsaraDB Agent等创新成果,全面展现数据库在多模态、智能体、具身智能等场景的技术演进与落地。
|
29天前
|
机器学习/深度学习 人工智能 资源调度
智能家居环境中的AI决策解释:实现以人为中心的可解释性——论文阅读
本文探讨智能家居中AI决策的可解释性,提出以人为中心的XAI框架。通过SHAP、DeepLIFT等技术提升模型透明度,结合用户认知与需求,构建三层解释体系,增强信任与交互效能。
148 19
智能家居环境中的AI决策解释:实现以人为中心的可解释性——论文阅读