当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
使用docker images
来列出本地镜像
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/centos v1 c44bca37b140 22 hours ago 220MB
centos latest 0f3e07c0138f 8 weeks ago 220MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
ubuntu 13.10 7f020f7bf345 5 years ago 185MB
各个选项说明:
如果我们要使用某个版本的系统镜像来运行容器,可用命令
root@ubuntu:~# docker run -t -i ubuntu:13.10 /bin/bash
root@8d46377aa303:/#
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。
当我们在本机上使用一个不存在的镜像时,Docker就会自动下载这个镜像
我们也可以使用docker pull
来预先下载镜像
root@ubuntu:~# docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
Image docker.io/library/ubuntu:13.10 uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
a3ed95caeb02: Already exists
0d8710fc57fd: Already exists
5037c5cd623d: Already exists
83b53423b49f: Already exists
e9e8bd3b94ab: Already exists
7db00e6b6e5e: Already exists
Digest: sha256:403105e61e2d540187da20d837b6a6e92efc3eb4337da9c04c191fb5e28c44dc
Status: Image is up to date for ubuntu:13.10
docker.io/library/ubuntu:13.10
我们可以使用docker search
命令来搜索镜像,如需要一个CentOS镜像
root@ubuntu:~# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
- NAME:镜像仓库源的名称
- DESCRIPTION:镜像描述
- STARS:点赞
- OFFICIAL:是否为Docker官方发布
- AUTOMATED:自动构建
root@ubuntu:~# docker rmi hello-world
当下载的镜像,不能满足我们的需求的时候,可以通过一下方法:
1、在容器中更新镜像,并提交镜像
2、使用dockerfile
命令创建自己的镜像
在更新镜像之前,我们需要使用镜像来创建一个容器
root@ubuntu:~# docker run -ti 9b9cb95443b5 /bin/bash
root@32370d98f202:/# apt-get update
在运行的容器内使用apt-get update
命令进行更新,然后通过命令docker commit
来提交容器副本
root@ubuntu:~# docker commit -m="ubuntu docker image update test" -a="ene" 32370d98f202 ene/ubuntu:v2
sha256:ef7902a09bcbea98e4b8c964a2151163688a0563bff5f8deb769f24976e909c4
参数解析:
-m
:提交的描述信息-a
:指定镜像作者32370d98f202
:容器IDene/ubuntu:v2
:指定要创建的目标镜像名我们可以使用docker bulid
,从零开始创建一个新的镜像,为此我们需要创建一个dockerfile文件,其中包含告诉Docker如何构建我们镜像的指令组
root@ubuntu:~# touch Dockerfile
root@ubuntu:~# vim Dockerfile
## docker build test
FROM ubuntu:15.10
MAINTAINER Fisher "DockerbuildTest@ene"
RUN /bin/bash 'root:123' | chpasswd
RUN useradd ene
RUN /bin/echo 'root:123' | 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在镜像内执行的命令然后通过docker build
命令来构建一个镜像
root@ubuntu:~# docker build -t ene/ubuntu:15.10 .
Sending build context to Docker daemon 227.5MB
参数解析:
-t
:指定要创建的目标镜像名.
:Dockerfile文件所在目录,可写绝对路径使用我们建立的镜像创建容器
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ene/ubuntu 15.10 467735c8da48 2 minutes ago 138MB
root@ubuntu:~# docker run -it ene/ubuntu:15.10 /bin/bash
root@3a7dc60167da:/#
使用doacker tag
命令为镜像添加一个标签
root@ubuntu:~# docker tag 467735c8da48 ene/ubuntu:test
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ene/ubuntu 15.10 467735c8da48 8 minutes ago 138MB
ene/ubuntu test 467735c8da48 8 minutes ago 138MB