Docker相关docker

Docker入门之一(镜像)

2019-02-25  本文已影响0人  平头哥2

安装docker:

[root@langzi01 ~]# yum install -y docker

启动docker服务:

[root@langzi01 ~]# service docker start
Redirecting to /bin/systemctl start docker.service

获取镜像:

[root@langzi01 ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
6cf436f81810: Pull complete 
987088a85b96: Pull complete 
b4624b3efe06: Pull complete 
d42beb8ded59: Pull complete 
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Downloaded newer image for docker.io/ubuntu:latest

以上命令实际是相当于:

[root@langzi01 ~]# docker pull docker.io/library/ubuntu:latest

即:默认注册服务器为:docker.io

获取指定版本的镜像:

[root@langzi01 ~]# docker pull ubuntu:18.04

其中:18.04是指的ubuntu的版本

运行进入Ubuntu系统命令

[root@langzi01 ~]# docker run -ti ubuntu bash
root@9120f0d8f395:/# pwd
/

查看镜像信息:

[root@langzi01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    18.04               47b19964fb50        2 weeks ago         88.1 MB
docker.io/ubuntu    latest              47b19964fb50        2 weeks ago         88.1 MB
docker.io/mysql     5.7                 ae6b78bedf88        3 months ago        372 MB

REPOSITORY: 表示来自哪个仓库

TAG:标签,相当于别名,同一个镜像可以有多个别名

IMAGE ID:镜像ID,唯一

CREATED:镜像创建时间

SIZE:镜像大小

查看镜像的详细信息 :docker inspect 镜像ID

[root@langzi01 ~]# docker inspect 47b19964fb50
[
    {
        "Id": "sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541",
        "RepoTags": [
            "docker.io/ubuntu:18.04",
            "docker.io/ubuntu:latest"
        ],
        "RepoDigests": [
            "docker.io/ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-02-06T03:37:51.599085524Z",
        "Container": "4fcabd24a4d0668c251f05105af1d986b1841713e7af90bf51df172ecefae2fa",
        "ContainerConfig": {
            "Hostname": "4fcabd24a4d0",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 88140808,
        "VirtualSize": 88140808,
        "GraphDriver": {
            "Name": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/23aff4d2336551f4ba626a2f0e143a8ebe4df37c043a4f11a8e000af7ce730fa/diff:/var/lib/docker/overlay2/f7a8ed06520360cc2a06b97263b6eaaa6fc1820ab95c4ac26520a6d9677d80c3/diff:/var/lib/docker/overlay2/052cd29c13faff8cad7a469e796417b93cf47fd489ff510ff43ce84c8619c834/diff",
                "MergedDir": "/var/lib/docker/overlay2/ec1cd894fec16faf33b2d492da81b3c22294cfb64235e554abba1d6f8d1bd08a/merged",
                "UpperDir": "/var/lib/docker/overlay2/ec1cd894fec16faf33b2d492da81b3c22294cfb64235e554abba1d6f8d1bd08a/diff",
                "WorkDir": "/var/lib/docker/overlay2/ec1cd894fec16faf33b2d492da81b3c22294cfb64235e554abba1d6f8d1bd08a/work"
            }
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41",
                "sha256:283fb404ea9415ab48456fd8a82b153b1a719491cdf7b806d1853b047d00f27f",
                "sha256:663e8522d78b5b767f15b2e43885da5975068e3195bbbfa8fc3a082297a361c1",
                "sha256:4b7d93055d8781d27259ba5780938e6a78d8ef691c94ee9abc3616c1b009ec4a"
            ]
        }
    }
]

可以查看镜像的特定信息:

[root@langzi01 ~]# docker inspect -f {{.Architecture}} 47b19964fb50
amd64

搜索镜像:

[root@langzi01 ~]# docker search elasticsearch
1551073204(1).jpg

INDEX :镜像地址 NAME :镜像名字 DESCRIPTION :镜像描述 STARS :镜像受欢迎程度 OFFICIAL:是否是官方的 AUTOMATED:

查看本机上存在的所有容器:

[root@langzi01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
9120f0d8f395        ubuntu              "bash"                   About an hour ago   Up About an hour                              flamboyant_bell
c947e8ad159b        ubuntu              "bash"                   About an hour ago   Up About an hour                              brave_feynman
edc1796b7019        mysql:5.7           "docker-entrypoint..."   2 months ago        Exited (0) 2 months ago                       JY_mysql
d68e00314e56        mysql:5.7           "docker-entrypoint..."   2 months ago        Exited (0) 2 months ago                       pwc-mysql

可以看到 :有ubuntu,mysql在运行,删除mysql镜像,不会成功:

[root@langzi01 ~]# docker rmi mysql:5.7
Error response from daemon: conflict: unable to remove repository reference "mysql:5.7" (must force) - container d68e00314e56 is using its referenced image ae6b78bedf88

强制删除(不推荐,会有遗留问题):

[root@langzi01 ~]# docker rmi -f mysql:5.7

应该先停止容器,再移除容器,再删除镜像

停止容器: docker stop 容器ID

[root@langzi01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9120f0d8f395        ubuntu              "bash"              About an hour ago   Up About an hour                        flamboyant_bell
c947e8ad159b        ubuntu              "bash"              2 hours ago         Up 2 hours                              brave_feynman
[root@langzi01 ~]# docker stop 912
912

删除容器:docker rm 容器ID:

[root@langzi01 ~]# docker rm 912
912

删除镜像:略去

创建镜像:

1.基于已有的容器的镜像创建:

运行ubuntu镜像:

[root@langzi01 ~]# docker run -ti ubuntu bash  ##运行
root@6fa335302a6b:/# touch test  ## 创建一个文件
root@6fa335302a6b:/# exit  ##推出

查看镜像运行的容器ID:

[root@langzi01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6fa335302a6b        ubuntu              "bash"              26 seconds ago      Exited (0) 13 seconds ago                       jovial_yalow

创建镜像的命令:

[root@langzi01 ~]# docker commit -m "add a new file" -a "Docker NewBee" 6fa335302a6b test
sha256:39f3c794c54d5404cd07749bd276caa73c043cf289b0efea90ed431e04729683

说明:这里的6fa335302a6b是原来ubuntu镜像运行的容器ID

docker commit用来创建镜像

-m , --message="" 提交信息

-a,--author="" 作者信息

-p, --pause=true 提交时暂停容器

再次查看镜像,可以发现自己的镜像:

[root@langzi01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              39f3c794c54d        17 seconds ago      88.1 MB
docker.io/ubuntu    latest              47b19964fb50        2 weeks ago         88.1 MB

基于本地模板导入创建镜像:

[root@langzi01 ~]# cat nginx-1.14.2.tar.gz |docker import - nginx:1.14.2
sha256:3982eec1262978d6c21e750e0b1a53dfc5ec305d584c83c6e3965cf16bd65f2e
[root@langzi01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14.2              3982eec12629        4 seconds ago       6.07 MB
test                latest              39f3c794c54d        16 minutes ago      88.1 MB
docker.io/ubuntu    latest              47b19964fb50        2 weeks ago         88.1 MB

存出镜像:

[root@langzi01 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14.2              3982eec12629        8 minutes ago       6.07 MB
test                latest              39f3c794c54d        24 minutes ago      88.1 MB
docker.io/ubuntu    latest              47b19964fb50        2 weeks ago         88.1 MB
[root@langzi01 docker]# pwd
/root/docker
[root@langzi01 docker]# docker save -o nginx_xxxxx nginx:1.14.2
[root@langzi01 docker]# ls
nginx_xxxxx

载入镜像到容器:

[root@langzi01 docker]# docker load --input nginx_xxxxx 
bc51f1908ab3: Loading layer [==================================================>] 6.389 MB/6.389 MB
Loaded image: nginx:1.14.2
上一篇 下一篇

猜你喜欢

热点阅读