docker镜像和仓库
docker镜像和仓库
docker镜像
Docker 镜像可以看作是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。
列出镜像
使用docker images命令列出docker主机上可用的镜像列表。本地镜像都保存在docker宿主机下的/var/lib/docker目录下。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest d131e0fa2585 8 days ago 102MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
[root@localhost ~]#
拉取镜像
docker pull [选项] [Docker Registry地址]<仓库名>:<标签>
查找镜像
docker search <name>
去仓库查找NAME带有<name>的镜像
构建镜像
构建docker镜像有以下两种方法:
- 使用docker commit命令
- 使用docker build命令和Dockerfile文件
docker默认的仓库是docker hub, 因为是外网,这里我使用阿里云私有镜像仓库,这样不至于网络不稳。
docker commit命令构建
- docker login
在阿里云容器镜像服务里设置好仓库和访问凭证之后使用docker login登录仓库。
[root@localhost ~]# docker login --username=xxxxxx registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
登录成功之后可以cat ~/.docker/config.json查看登录信息。
[root@localhost ~]# cat ~/.docker/config.json
{
"auths": {
"registry.cn-hangzhou.aliyuncs.com": {
"auth": "xxxxx"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.5 (linux)"
}
- 运行一个要进行修改定制的容器
[root@localhost ~]# docker run -i -t ubuntu /bin/bash
- 在容器内做定制操作
root@15a770260708:/# apt-get -yqq update
root@15a770260708:/# apt-get -y install apache2
- 退出容器并执行
docker commit
[root@localhost ~]# docker ps -l -q
15a770260708
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15a770260708 ubuntu "/bin/bash" 7 minutes ago Exited (0) 10 seconds ago vigorous_jennings
e9fcb8e85d41 hello-world "/hello" About an hour ago Exited (0) About an hour ago loving_chebyshev
[root@localhost ~]# docker commit 15a770260708 registry.cn-hangzhou.aliyuncs.com/liaolin_repo/apache2
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/liaolin_repo/apache2 latest fd381fe3da54 About a minute ago 223MB
ubuntu latest d131e0fa2585 8 days ago 102MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
[root@localhost ~]#
当这个容器又做了其他修改需要保存时,继续使用`docker commit`命令
docker commit -m "指定提交comment信息" -a "指定作者" [imageID] [仓库名]:[镜像版本号]
- 上传自定义镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/liaolin_repo/apache2 latest fd381fe3da54 About a minute ago 223MB
ubuntu latest d131e0fa2585 8 days ago 102MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
[root@localhost ~]# docker tag fd381fe3da54 registry.cn-hangzhou.aliyuncs.com/liaolin_repo/liaolin_repo:1.01
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/liaolin_repo/apache2 latest fd381fe3da54 5 minutes ago 223MB
registry.cn-hangzhou.aliyuncs.com/liaolin_repo/liaolin_repo 1.01 fd381fe3da54 5 minutes ago 223MB
ubuntu latest d131e0fa2585 8 days ago 102MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/liaolin_repo/liaolin_repo:1.01
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/liaolin_repo/liaolin_repo]
762b265fb305: Pushed
7660ded5319c: Pushed
94e5c4ea5da6: Pushed
5d74a98c48bc: Pushed
604cbde1a4c8: Pushed
1.01: digest: sha256:9540c6758bb102033b3c781f08786262f47dcfb74304d8e38c0bd1992bc4180c size: 1362
[root@localhost ~]#
Dockerfile构建镜像
不推荐使用docker commit方式构建镜像。推荐使用Dockerfile定义文件和docker build命令构建镜像。
- 创建Dockerfile
# version: 0.01
FROM ubuntu:18.04
RUN apt-get -yqq update
RUN apt-get install -y nginx
RUN echo 'Hi, I am in your container' \
> /usr/share/nginx/html/index.html
EXPOSE 80
该Dockerfile由一系列指令和参数组成。每条指令,如FROM,都必须为大写字母,Dockerfile中的指令会按照顺序从上到下执行。每条指令都会创建一个新的镜像层并对镜像进行体检。Docker大体上按照如下流程执行Dockefile中的指令。
- Docker从基础镜像运行一个容器
- 执行一条指令,对容器做出修改
- 执行类似docker commit的操作,提交一个新的镜像层
- Docker再基于刚提交的镜像运行一个新容器
- 执行Dockerfile中的下一条指令,直到所有指令都执行完毕
FROM指令是必须存在的,指定构建镜像的基础镜像,RUN指令会在shell里使用命令包装器/bin/sh -c 来执行。如果是在一个不支持shell的平台上运行或者不希望在shell中运行,也可以使用exec格式的RUN指令,例如:
RUN ["apt-get", "install", "-y" "nginx"]
EXPOSE指令告诉docker该容器内的应用程序将会使用容器的指定端口。这并不意味着可以自动访问任意容器运行中服务的端口。处于安全原因,Docker并不会自动打开该端口,而是需要用户在使用docker run运行容器时来指定需要打开哪些端口。EXPOSE可以指定向外公开多个端口。
- docker build指定Dockerfile构建
[root@localhost ~]# docker build -t="registry.cn-hangzhou.aliyuncs.com/liaolin_repo/nginx_web:v1" .
# 这里-t指定"[仓库名]/[镜像名]:[版本]"
也可以从Git仓库构建Dcoker镜像
docker build -t="xxx/xxx:xx" git@github.com:xxxx/xxxx
-
上传自定义镜像
上传自定义镜像和上面就一样了。
-
基于上面Dockefile构建的镜像运行一个容器
[root@localhost ~]# docker run -d -p 80 --name static_web 569a26304942 nginx -g "daemon off;"
e42490e1855ef48f0764fa15be384d471b81d751e56186250e2b071f0e1fcc3a
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e42490e1855e 569a26304942 "nginx -g 'daemon of…" 6 seconds ago Up 3 seconds 0.0.0.0:32771->80/tcp static_web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e42490e1855e 569a26304942 "nginx -g 'daemon of…" 9 seconds ago Up 6 seconds 0.0.0.0:32771->80/tcp static_web
[root@localhost ~]#
# 检查服务
[root@localhost ~]# curl -v http://127.0.0.1:32771
-
Dockerfile其它指令s
Dockerfile还有很多其它指令, 详情可以在https://docs.docker.com/engine/reference/builder/上查看。

浙公网安备 33010602011771号