背景:
日常开发中,经常会维护一个基础的docker镜像,里面已经做调试好了网络、ssh、端口映射、volume内外卷映射等,这时需要把它的状态给保存下来,这样后续就不用每次在重复执行上述操作,用于后续再复用这些基础层,并基于此进行更高级层的构建。
这时,commit 命令就派上用场了,使用该命令可以将当前版本的container打包成包含当前最新层的image,后续再根据这个image生成基础容器。
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
参数解读:
- -m:注释,类似于git中注释
- CONTAINER 部分可以为容器ID,也可以为容器名
命令中,指定了要提交的修改过的容器的ID、目标镜像仓库、镜像名。commit提交的知识创建容器的镜像与容器的当前状态之间的差异部分,很轻量。
# docker commit -m "A base env of apptest,already configed the network/ssh/port \
mapping/volume mapping e.g." -author "david" 3c141c17c3d9 apptest_base
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apptest_base latest edfd3e2e67dc 8 seconds ago 1.41 GB
docker.io/centos latest 1e1148e4cc2c 4 months ago 202 MB
使用inspect命令查看镜像构成信息:
docker inspect CONTAINER
# docker inspect apptest_base
[
{
"Id": "sha256:zdfd3e2e67dc0d6dc932d6ae056dd3301a264d7bbe193a599ae746b022f73ecc",
"RepoTags": [
"apptest_base:latest"
],
"RepoDigests": [],
"Parent": "sha256:fdecd6992fe0ffc893d15588ae9d5cbb0f16354ae5006fe7732f5ff4933d762f",
"Comment": "A base env of apptest,already configed the network/ssh e.g.",
"Created": "2019-04-29T11:25:23.338056461Z",
"Container": "3c141c17c3d93b6e282b2937dbbbb451ff230f8230bd743e5dfdcc1cb4f81465",
"ContainerConfig": {
"Hostname": "app-test",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"bash",
"-c",
"\necho export JAVA_HOME=/opt/jdk1.8 >> /etc/profile ;\necho export PATH=\\/bin:\\/sbin:/bin:/usr/sbin:/usr/bin >> /etc/profile ; \nsh /opt/tomcat/bin/startup.sh ;\ntail -f /dev/null \n"
],
"Image": "app:20190401",
"Volumes": {
"/opt/jdk1.8": {},
"/opt/tomcat": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "62a324e143b1caa71c789bbbaff9e6b9ad24bfba822fbac6d7a03ce848a2d446",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "dockerwebapptest",
"com.docker.compose.service": "tomcat",
"com.docker.compose.version": "1.18.0"
}
},
"DockerVersion": "1.13.1",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"bash",
"-c",
"\necho export JAVA_HOME=/opt/jdk1.8 >> /etc/profile ;\necho export PATH=\\/bin:\\/sbin:/bin:/usr/sbin:/usr/bin >> /etc/profile ; \nsh /opt/tomcat/bin/startup.sh ;\ntail -f /dev/null \n"
],
"Image": "",
"Volumes": {
"/opt/jdk1.8": {},
"/opt/tomcat": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "62a324e143b1caa71c789bbbaff9e6b9ad24bfba822fbac6d7a03ce848a2d446",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "dockerwebapptest",
"com.docker.compose.service": "tomcat",
"com.docker.compose.version": "1.18.0"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1408363971,
"VirtualSize": 1408363971,
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/fae8d0c595b319a832a5bc761b4c1a1106b59fd31d7aef4fabf3309626f858f6/diff",
"MergedDir": "/var/lib/docker/overlay2/1a63e99e77583a045388666f411be28192565b8697f1f9dc7b5086d5db647200/merged",
"UpperDir": "/var/lib/docker/overlay2/1a63e99e77583a045388666f411be28192565b8697f1f9dc7b5086d5db647200/diff",
"WorkDir": "/var/lib/docker/overlay2/1a63e99e77583a045388666f411be28192565b8697f1f9dc7b5086d5db647200/work"
}
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e2fba5912d721c26317b6b6854b65590cc032d6e8c8be1c186eba07415669d40",
"sha256:9b28877c46d48c24edc615ccf82fb0291c4c3297378119d54eafc0518d6d71f6"
]
}
}
]
提取容器IP地址:
docker inspect ${CONTAINER_NAME} | grep IPAddress