Docker训练营Docker安装及基本命令学习笔记

本笔记为阿里云天池龙珠计划Docker训练营的学习内容,学习链接为

https://tianchi.aliyun.com/specials/activity/promotion/aicampdocker

一.学习知识点概要

1.Dockerd的安装

(1).Linux上的安装

(2).MacOS上的安装

(3).Windows10上的安装

2.Dockerd的基本命令

二.学习内容

1.安装

(1).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 repositories
distribution=$(. /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 | sudo tee /etc/apt/sources.list.d/nvidia-docker.list


sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
# stable
$ winget install Docker.DockerDesktop
# edge
$ winget install Docker.DockerDesktopEdge

运行

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

(2).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 --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/

2.基本命令

(1).拉取镜像

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

(2).运行镜像

$ docker run hello-world

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/

(3).运行镜像并进入容器

$ 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

(4).查看本地镜像

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

(5).查看运行中的容器

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

(6).进入容器

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

(7).保存修改

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

(8).打TAG

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

(9).推送镜像到仓库

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

(10).使用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"]

(11). 构建镜像

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

如要指定dockerfile :

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

(12). 删除镜像/容器

删除镜像:

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

删除容器:

docker rm [CONTAINER ID]

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

docker kill [CONTAINER ID]

查看运行中的容器:

docker ps 

查看所有容器:

docker ps -a

(13). 常规技巧

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

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

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

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

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

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

【OPENLIST】Base Docker Image List-天池技术圈-天池技术讨论区

(15). 海外选手网速受限提交方案

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

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

3.Linux下安装docker(MacOS省略)

通过课时四的视频总结大致如下:

(1)#docker

(2)#apt install docker.io

(3)#docker --version

(4)#apt auto

(5)#apt autoremove docker.io

(6)docker

(7)#$ sudo curl -sS https://get.docker.com/ | sh

(8)#docker --version

4.dockerdfile入门

5.创建基础镜像

通过课时七的视频总结大致如下:

(1)#dccker pull 拉取的镜像仓库

(2)#docker run -itd 拉取的镜像仓库/bin/bash

(3)#docker ps

(4)#docker exec -it 容器的ID/bin/bash

(5)pip install numpy或pip install pandas可以任意下载

(6)exit

(7)#docker ps

(8)#docker commit 容器的ID 新的镜像名

(9)#docker images

(10)#docker run -it 新的镜像名/bin/bash

(11)pip list|grep pandas

三.学习问题与解答

在Linux下安装docker运行时出现了错误,向老师求助后成功解决了下载过程中遇到的问题,同时也加深了对docker镜像和容器的进一步认识。

四.学习思考与总结

本次任务使我系统的了解了docker的安装,并且掌握了镜像的拉取,镜像的运行等基本命令,明白如何创建基础镜像。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值