Docker镜像介绍和常用命令

Kai Docker, 工作 3,443 次浏览 没有评论

Docker 运行容器前需要本地存在对应的镜像, 如果镜像不存在本地, Docker 会尝试先从默认镜像仓库下载(默认使用 Docker Hub 公共注册服务器中的仓库), 用户也可以通过配置 使用自定义的镜像仓库。

 
 

 
 

# 基本信息查看

docker version # 查看docker的版本号,包括客户端、服务端、依赖的Go等

docker info # 查看系统(docker)层面信息,包括管理的images, containers数等

docker pull centos 下载

docker images [ centos ] 查看

docker run -i -t centos /bin/bash

 
 

镜像的获取与容器的使用

# 搜索镜像

docker search mysql # 在docker index中搜索mysql

 
 

可以看到返回了很多包含关键字的镜像, 其中包括镜像名字、 描述、 星级(表示 该镜像的受欢迎程度)、 是否官方创建、 是否自动创建等。

默认的输出结果将按照星级评价进行排序。官方的镜像说明是官方项目组创建和维护的, automated资源则允许用户验证镜像的来源和内容。

 
 

 
 

# 下载镜像

docker pull mysql # 从docker registry server 中下拉mysql

 
 

# 查看镜像

docker images: # 列出images

docker images -a # 列出所有的images(包含历史)

使用 docker inspect 命令可以获取该镜像的详细信息。

[root@localhost ~]# docker inspect f49eec89601e

 
 

 
 

# 创建镜像

创建镜像的方法有三种:基于已有镜像的容器创建、 基于本地模板导入、 基于 Dockerfile 创建。

基于己再镜像的容器创建

该方法主要是使用 docker commit 命令, 其命令格式为 docker commit [OPTIONS] CONTAINER [REPOSITORY [:TAG]], 主要选项包括:

-a,–author=””作者信息。

-m, –message=”” 提交消息。

-p, –pause=true 提交时暂停容器运行。

用命令创建新镜像

先创建一个test的文件夹来测试下,创建好exit退出,另外记住ID号7f3934e930e5

[root@localhost ~]# docker run -t -i ubuntu:14.04 /bin/bash

[root@localhost ~]# docker commit -m “Added a new file” -a “Docker Newbee” 7f3934e930e5 test

顺利的话会返回新的镜像ID的数值

sha256:25f66c1207e0c3813fa5bbcc443f670e6e94ce7619b95946e791586d393b3038

在查看下发现新的镜像已经有了

[root@localhost ~]# docker images

 
 

基于本地模板导入

在openvz网站下个ubuntu14.04的模板

[root@localhost ~]# wget https://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz

然后在导入进去

[root@localhost ~]# cat ubuntu-14.04-x86 64-minimal.tar.gz Jdocker import – ubuntu:14.04

然后在查看下

[root@localhost ~]# docker images

 
 

# 删除镜像

命令格式为docker rmi IMAGE

假如删除ubuntu 14.04 首先查看下镜像的ID

[root@localhost ~]# docker images

 
 

[root@localhost ~]# docker rmi 25f66c1207e0

 
 

存出和载入镜像

存出镜像

如果要存出镜像到本地文件, 可以使用docker save命令。例如, 存出本地的 ubuntu: 14.04镜像为文件 ubuntu 14.04.tar:

[root@localhost ~]#
docker save -o ubuntu 14.04.tar ubuntu:14.04

或者

[root@localhost ~]#
docker load< ubuntu 14.04.tar

 
 

上传镜像

可以使用 docker push 命令上传镜像到仓库, 默认上传到 DockerHub 官方仓库(需要登录), 命令格式为 docker push NAME [ : TAG J 。

用户在 DockerHub 网站注册后, 即可上传自制的镜像。 例如用户 user 上传本地的 test:latest 镜像, 可以先添加新的标签 user/test:latest, 然后用 docker push命令上传镜像:

[root@localhost ~]#
docker tag test:latest user/test:latest

[root@localhost ~]#
docker push user/test:latest

The push refers to a repository [base/163] (len: 1) Sending image list

Please login prior to push:

Username:

Password:

Email: xxx@xxx.com

第一次使用时, 会提示输入登录信息或进行注册

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

发表回复

Go