《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

相关文章
|
1月前
|
SQL 数据库 Docker
OBCP实践 - OceanBase Docker 体验
开发者和技术爱好者可以在无需复杂安装配置的情况下迅速搭建OceanBase数据库环境,从而方便地进行功能测试、性能评估或者开发调试等实践活动。同时,由于Docker的便捷性,此方法也支持跨平台部署,使得OceanBase数据库能够在不同的操作系统(如Linux、Windows、macOS)上轻松体验。
79 2
|
1月前
|
搜索推荐 测试技术 数据安全/隐私保护
【Docker项目实战】Docker环境下部署NeonLink书签平台
【2月更文挑战第17天】Docker环境下部署NeonLink书签平台
92 0
|
1月前
|
测试技术 API 开发者
【Docker项目实战】在Docker环境下部署go-file文件分享工具
【2月更文挑战第15天】在Docker环境下部署go-file文件分享工具
67 1
|
1月前
|
安全 测试技术 数据安全/隐私保护
【好用的个人工具】Docker环境下部署FileDrop文件共享工具
【2月更文挑战第13天】Docker环境下部署FileDrop文件共享工具
65 2
|
20天前
|
jenkins 测试技术 持续交付
软件测试|docker搭建Jenkins+Python+allure自动化测试环境
通过以上步骤,你可以在Docker中搭建起Jenkins自动化测试环境,实现Python测试的自动化执行和Allure报告生成。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
39 6
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
216 0
|
1月前
|
Kubernetes Go 开发者
Go语言与Docker容器结合的实践应用与案例分析
【2月更文挑战第23天】本文通过分析实际案例,探讨了Go语言与Docker容器技术结合的实践应用。通过详细阐述Go语言在容器化环境中的开发优势,以及Docker容器技术在Go应用部署中的重要作用,本文旨在为读者提供Go语言与Docker容器结合的具体实现方法和实际应用场景。
|
1月前
|
存储 人工智能 数据库
生成式AI「人人可用」的时代,企业与开发者该怎么做?
有研究称:生成式AI正在从“第一幕”走向“第二幕”,“快速展示正被真正的价值和完整的产品体验所取代”,第二阶段中“生成式AI普惠”应该怎么做呢?
生成式AI「人人可用」的时代,企业与开发者该怎么做?
|
1月前
|
云安全 人工智能 安全
|
1月前
|
Kubernetes 云计算 开发者
云计算中的容器化技术:Docker与Kubernetes的实践
云计算中的容器化技术:Docker与Kubernetes的实践
105 0

热门文章

最新文章