Docker训练营第三天

本笔记为阿里云天池龙珠计划Docker训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/activity/promotion/aicampdocker

拉取镜像

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

如:

docker pull hello-world:latest

运行镜像

$ 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/

运行镜像并进入容器

$ 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

查看运行中的容器

$ 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包,如果想对修改做保留,可以执行提交。

保存修改

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"]

构建镜像

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]

常规技巧

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

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

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

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值