Docker学习之2——镜像

镜像(Images)

  镜像是Docker的三大核心之一,类似于虚拟机,作用和虚拟机是一样的,唯独是组成部分会有些区别。简单的说如果我们想启动一个容器就必须要有镜像。docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,docker会尝试先从默认镜像仓库下载(Docker Hub),用户也可以通过配置,使用自定义的镜像仓库。

 镜像文件存储结构?

docker相关文件存放在:/var/lib/docker目录下

/var/lib/docker
├── builder
├── buildkit
├── containerd
├── containers
├── image
├── network
├── overlay2
├── plugins
├── runtimes
├── swarm
├── tmp
├── trust
└── volumes

国内doceker仓库地址

vim /etc/docker/daemon.json 

{
  "registry-mirrors":[ "https://registry.docker-cn.com" ]
}
#获取镜像
docker pull 【镜像名称】

[root@controller ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
660c48dd555d: Pull complete 
4c7380416e78: Pull complete 
421e436b5f80: Pull complete 
e4ce6c3651b3: Pull complete 
be588e74bd34: Pull complete 
Digest: sha256:7c67a2206d3c04703e5c23518707bdd4916c057562dd51c74b99b2ba26af0f79

#查看镜像
[root@controller ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              20c44cd7596f        3 days ago          122.8 MB
字段解释:
REPOSITORY  来自哪个仓库
TAG         镜像标签信息 (别名的作用)
IMAGE ID    镜像ID号(唯一)
CREATED     创建时间
SIZE        大小
#获取一个ubuntu 14.04版本
[root@controller ~]# docker pull ubuntu: 14.04
[root@controller ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              20c44cd7596f        3 days ago          122.8 MB
docker.io/ubuntu    14.04               d6ed29ffda6b        3 days ago          221 MB
我们可以看到这两个images来自同一个仓库,TAG标签不一样标记不同的镜像,使用docker tag命令可以为本地镜像添加新的标签。

#查看镜像详细信息
docker inspect 【IMAGE ID】
[root@controller ~]# docker inspect 20c44cd7596f
[
    {
        "Id": "sha256:20c44cd7596ff4807aef84273c99588d22749e2a7e15a7545ac96347baa65eda",
        "RepoTags": [
            "docker.io/ubuntu:latest"
        ],
...
-f参数指定查询:
[root@controller ~]# docker inspect -f {{".RepoTags"}} 20c44cd7596f
[docker.io/ubuntu:latest]

#搜索镜像
[root@controller ~]# docker search nginx
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                                                  Official build of Nginx.                        7317      [OK]       
docker.io   docker.io/jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1174                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   475                  [OK]

#删除镜像
docker rmi 【标签或ID】
[root@controller ~]# docker rmi ubuntu:latest
Untagged: ubuntu:latest

#运行一个容器
[root@controller ~]# docker run ubuntu echo 'hello I am ubuntu

#查看存在的所有容器
[root@controller ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS               NAMES
2bb8a93f5d34        ubuntu              "echo 'hello I am ubu"   About a minute ago   Exited (0) About a minute ago                       gigantic_heyrovsky
#这时删除镜像出现错误
[root@controller ~]# docker rmi ubuntu
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 2bb8a93f5d34 is using its referenced image 20c44cd7596f
#强制删除加-f
[root@controller ~]# docker rmi -f ubuntu

#创建镜像
创建镜像有三种方法:
1.已有镜像创建
2.本地模板导入
3.Dockerfile创建

1.已有镜像创建
Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
  -a, --author string    
  -c, --change value     
      --help             
  -m, --message string   
  -p, --pause   
         
#首先启动一个镜像
[root@controller ~]# docker run -ti ubuntu:14.04 /bin/bash
root@c5439518dcbe:/# touch test
root@c5439518dcbe:/# exit
记住容器的ID:c5439518dcbe

#创建一个新的镜像
[root@controller ~]# docker commit -m "added a new file" -a "Docker newbee" c5439518dcbe test
sha256:4efd37e84d4998fbc7cff55d21fa360fdd60f1d7319050e70512754bf2851b7f
[root@controller ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              4efd37e84d49        18 seconds ago      221 MB

#模板下载:https://download.openvz.org/template/precreated/
[root@controller ~]# cat ubuntu-12.04-x86-minimal.tar.gz |docker import - ubuntu:12.04
sha256:8d6161cea96c6b2f2ef39be5b2370e38cb003e8f3b5c2d3f960e0e7d37733d41

#导出镜像到本地
[root@controller ~]# docker save -o ubuntu_12.04.tar ubuntu:12.04
#从本地导入镜像
[root@controller ~]# docker load --input ubuntu_12.04.tar 
或
[root@controller ~]# docker load < ubuntu_12.04.tar 

#上传镜像(默认上传到Docker hub)
[root@controller ~]# docker push --help
Usage:  docker push [OPTIONS] NAME[:TAG]
[root@controller ~]# docker tag test:latest user/test:latest
[root@controller ~]# docker push user/test:latest

 小结:

  镜像是使用Docker的前提,因此可以结合公司的生产环境制作好镜像,方便使用。

原文地址:https://www.cnblogs.com/Dev0ps/p/7872731.html