获取镜像主要分为一下三类,本文将逐一记述
-
pull from
registry
(online) 从registry拉取-
public(公有)
-
private(私有)
-
-
load from
file
(offline) 文件导入 (离线) - build from
Dockerfile
(online) 从Dockerfile构建
一.pull from registry
(online) 从registry拉取
主要从Dockerhub,Quay.io等获取
docker image pull nginx //默认获取最新版本
docker image pull nginx:1.20.0 //获取指定版本
docker image ls //显示当前本地已下载image
docker image rm ID //删除指定image 须确保镜像中无容器
docker image prune -a //清除没有容器的image
二.load from file
(offline) 文件导入 (离线)
docker image save nginx -o nginx.image //保存image
docker image load -i nginx.image //导入image
★三.build from Dockerfile
(online) 从Dockerfile构建
在本地安装python3环境,并编译hello.py,运行以下代码后
FROM ubuntu:20.04
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
ADD hello.py /
CMD ["python3", "/hello.py"]
出现ubuntu 20.04和hello的image,后通过run运行
docker image build -t hello:1.0 . //建立image
docker run -it hello //运行hello image
通过上传到dockerhub上实现云存储,必须有账号名前缀
docker image build -t madapangname/hello:1.0 . //建一个名称 同ID
docker image tag hello madapangname/hello:1.0 //建立一个Tag 同ID
docker login //登录docker
docker image push madapangname/hello:1.0 //推送到hub
docker pull madapangname/hello:1.0 //拉取
最后删除原有image再次拉取进行测试运行,正常显示hello.py的内容
4.使用commit将存在的container转为image
进行交互式模式时,想将修改后的container存为image使用如下命令
docker container commit 571 test