Docker 镜像

列举

列出本地主机上的镜像

[running]jihite@:~$ docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu                                                     v1                  80e674d9a004        18 minutes ago      64.2MB

参数:

repository: 镜像仓库源

TAG: 标签,版本概念,同一个镜像可以对应多个tag

REPOSITORY:TAG 来定义不同的镜像

获取一个镜像

docker pull ubuntu:13.10

下载到本地后,可以用这个镜像来创建容器

问题: 必须把镜像下载到本地才可以在本地执行容器?

查找

方式1:https://hub.docker.com/

方式2:命令

[running]jihite@:~$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   10712               [OK]                
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   411                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   245                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   212                                     [OK]
。。。

参数:STARTS 类似 Github 里面的 star, AUTOMATED: 自动构建

删除

docker rmi hello-world

创建

方式1:更新

使用已有镜像来创建一个容器

[running]jihite@:~$ docker run -t -i ubuntu:15.10 /bin/bash

root@8d4479f6e4b4:/#

上面的8d4479f6e4b4是容器的id

在运行的容器内使用 apt-get update 命令进行更新

docker commit -m="has update" -a="jihite" 8d4479f6e4b4 test/ubuntu:v2

此时在看镜像列表,已添加v2

[running]jihite@:~$ docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu                                                     v2                  c5535f23fb9e        5 minutes ago       137MB
test/ubuntu                                                     v1                  80e674d9a004        55 minutes ago      64.2MB

方式2:构建

创建一个Dockerfile文件,内容

FROM    centos:6.7
MAINTAINER      Fisher "fisher@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的

第一条FROM,指定使用哪个镜像源

RUN 指令告诉docker 在镜像内执行命令,安装了什么

CMD类似RUN命令,用于运行程序;RUN在docker build时执行,CMD在docker run时执行

 

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像

[running]jihite@:~/soft/DOCKER$ docker build -t jihite/centos:6.7 .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM    centos:6.7
6.7: Pulling from library/centos
cbddbc0189a0: Pull complete 
Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1
Status: Downloaded newer image for centos:6.7
 ---> 9f1de3c6ad53
Step 2/9 : MAINTAINER      Fisher "jihite@163.com"
 ---> Running in 89692984b2d9
Removing intermediate container 89692984b2d9
 ---> 7877c81fcad7
Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd
 ---> Running in 0045a6808289
Removing intermediate container 0045a6808289
 ---> e69b720625a7
Step 4/9 : RUN     useradd runoob
 ---> Running in d6922cef70ff
Removing intermediate container d6922cef70ff
 ---> 34126187e790
Step 5/9 : RUN     /bin/echo 'runoob:123456' |chpasswd
 ---> Running in 66ae11b70d74
Removing intermediate container 66ae11b70d74
 ---> 1910f1148a66
Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
 ---> Running in e3af343f786f
Removing intermediate container e3af343f786f
 ---> 1dde7508d315
Step 7/9 : EXPOSE  22
 ---> Running in bdb74d714320
Removing intermediate container bdb74d714320
 ---> cb9d1e155251
Step 8/9 : EXPOSE  80
 ---> Running in 5458130fc48a
Removing intermediate container 5458130fc48a
 ---> 8ed17c39c39e
Step 9/9 : CMD     /usr/sbin/sshd -D
 ---> Running in a8133ae92d3a
Removing intermediate container a8133ae92d3a
 ---> f9f4b6664e1d
Successfully built f9f4b6664e1d
Successfully tagged jihite/centos:6.7

设置标签

docker tag f9f4b6664e1d jihite/centos:dev

再看

[running]jihite@:~/soft/DOCKER$ docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
jihite/centos                                                   6.7                 f9f4b6664e1d        2 hours ago         191MB
jihite/centos                                                   dev                 f9f4b6664e1d        2 hours ago         191MB

 

posted @ 2020-04-04 14:20  jihite  阅读(311)  评论(0编辑  收藏  举报