通过container commit成image

[root@localhost docker_test]# docker container commit #可以简写成docker commit
"docker container commit" requires at least and at most arguments.
See 'docker container commit --help'. Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes
[root@localhost docker_test]# docker image build #可以简写成docker build
"docker image build" requires exactly argument.
See 'docker image build --help'. Usage: docker image build [OPTIONS] PATH | URL | - Build an image from a Dockerfile

以centos image为例,centos base image 默认没安装vim,进入交互式,yum install vim

[root@localhost docker_test]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
bigni/test1 latest f5620b92331c hours ago 861kB
ubuntu 14.04 2c5e00d77a67 weeks ago 188MB
centos latest 9f38484d220f months ago 202MB
hello-world latest fce289e99eb9 months ago .84kB
[root@localhost docker_test]# docker run centos
[root@localhost docker_test]# docker run centos -it
docker: Error response from daemon: OCI runtime create failed: container_linux.go:: starting container process caused "exec: \"-it\": executable file not found in $PATH": unknown.
ERRO[] error waiting for container: context canceled
[root@localhost docker_test]# docker run -it centos
[root@dba873e57276 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@dba873e57276 /]# vim
bash: vim: command not found
[root@dba873e57276 /]# yum install -y vim
[root@localhost docker_test]# docker container ls -a #查看所有container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dba873e57276 centos "/bin/bash" minutes ago Exited () seconds ago infallible_rubin
36c7c2011bdb centos "-it" minutes ago Created wonderful_haslett
2401371f2c7a centos "/bin/bash" minutes ago Exited () minutes ago vibrant_dijkstra
[root@localhost docker_test]# docker commit
"docker commit" requires at least and at most arguments.
See 'docker commit --help'. Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] #docker commit 使用方法 Create a new image from a container's changes
[root@localhost docker_test]# docker commit infallible_rubin bigni/centos_vim #把names:infallible_rubin 的container commit 成image:bigni/centos_vim
sha256:afe852c481e754b88b28a663f6e98e0a52816e253c64af4b2b823f8bb8702961
[root@localhost docker_test]# docker image ls -a #查看新增image,比centos image大了好多,安装vim 需要的依赖包不少。
REPOSITORY TAG IMAGE ID CREATED SIZE
bigni/centos_vim latest afe852c481e7 seconds ago 362MB
<none> <none> 3ec8199c2855 hours ago 861kB
bigni/test1 latest f5620b92331c hours ago 861kB
ubuntu 14.04 2c5e00d77a67 weeks ago 188MB
centos latest 9f38484d220f months ago 202MB
hello-world latest fce289e99eb9 months ago .84kB
[root@localhost docker_test]# docker run -it bigni/centos_vim #交互式运行
bigni/centos_vim bigni/centos_vim:latest
[root@localhost docker_test]# docker run -it bigni/centos_vim #交互式运行,测试能不能使用vim
[root@3afa6597d516 /]# vim test
[root@3afa6597d516 /]# cat test
docker so easy
[root@3afa6597d516 /]# exit #退出
exit
[root@localhost docker_test]# docker container ls -a #可以看到新增一个container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3afa6597d516 bigni/centos_vim "/bin/bash" About a minute ago Exited () seconds ago cranky_zhukovsky
dba873e57276 centos "/bin/bash" minutes ago Exited () minutes ago infallible_rubin
36c7c2011bdb centos "-it" minutes ago Created wonderful_haslett
2401371f2c7a centos "/bin/bash" minutes ago Exited () minutes ago vibrant_dijkstra
[root@localhost docker_test]#

查看原始的centos image 和新增的image

[root@localhost docker_test]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
bigni/centos_vim latest afe852c481e7 minutes ago 362MB
bigni/test1 latest f5620b92331c hours ago 861kB
ubuntu 14.04 2c5e00d77a67 weeks ago 188MB
centos latest 9f38484d220f months ago 202MB
hello-world latest fce289e99eb9 months ago .84kB
[root@localhost docker_test]# docker history 9f38484d220f
IMAGE CREATED CREATED BY SIZE COMMENT
9f38484d220f months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> months ago /bin/sh -c #(nop) ADD file:074f2c974463ab38c… 202MB
[root@localhost docker_test]# docker history afe852c481e7
IMAGE CREATED CREATED BY SIZE COMMENT
afe852c481e7 minutes ago /bin/bash 160MB #可以看到,比原始的image多了一层 layer
9f38484d220f months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> months ago /bin/sh -c #(nop) ADD file:074f2c974463ab38c… 202MB
[root@localhost docker_test]#

上面就是通过docker commit 在base image至少build自己的image,这种方式不值得提倡,因为当把这个image发布出去后,别人并不知该image安装了什么,

而且也有可能是不安全的image。

另一种方式是通过Dockerfile来build一个image。

[root@localhost ~]# mkdir docker_vim
[root@localhost ~]# vim docker_vim/Dockerfile
[root@localhost ~]# cat docker_vim/Dockerfile
FROM centos
RUN yum install -y vim
[root@localhost ~]# docker build -t bigni/centos_vim ./docker_vim/ #通过dockerfile build 新的image
Sending build context to Docker daemon .048kB
Step / : FROM centos #from centos
---> 9f38484d220f #直接引用centos image这一层,而不会新创建一层
Step / : RUN yum install -y vim #运行Dockerfile里配置的命令
---> Running in 551ad2fd3374 #创建临时container
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 :7.4.-.el7_6 will be installed
--> Processing Dependency: vim-common = :7.4.-.el7_6 for package: :vim-enhanced-7.4.-.el7_6.x86_64
中间省略---> Package perl-Pod-Usage.noarch :1.63-.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-.el7.noarch
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 :7.4.-.el7_6 updates 1.0 M
Installing for dependencies:
中间省略which x86_64 2.20-.el7 base k Transaction Summary
================================================================================
Install Package (+ Dependent packages) Total download size: M
Installed size: M
Downloading packages:
warning: /var/cache/yum/x86_64//base/packages/gpm-libs-1.20.-.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for gpm-libs-1.20.-.el7.x86_64.rpm is not installed
Public key for perl-Pod-Escapes-1.04-.el7_6.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total kB/s | MB :
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release--6.1810..el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : gpm-libs-1.20.-.el7.x86_64 / 中间省略 Complete!
Removing intermediate container 551ad2fd3374
---> f853f2a3f901
Successfully built f853f2a3f901 #创建完成

查看创建的image

[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
bigni/centos_vim latest f853f2a3f901 minutes ago 362MB
bigni/test1 latest f5620b92331c hours ago 861kB
ubuntu 14.04 2c5e00d77a67 weeks ago 188MB
centos latest 9f38484d220f months ago 202MB

最新文章

  1. Nginx配置文件nginx.conf中文详解(转)
  2. oracle建立数据库连接
  3. 移动 Web 开发技巧
  4. Week1 Java 基础知识
  5. 使用SWFUpload无刷新上传图片
  6. LR场景设置里的各参数解释
  7. mybatis中:returned more than one row, where no more than one was expected.异常
  8. Linux环境下编译并执行ava helloworld程序
  9. HDU1160(LIS)
  10. Android PopupWindows
  11. Struts2如何搭建?
  12. SQL-45 将titles_test表名修改为titles_2017。
  13. MySQL Cluster
  14. 编译和运行java文件 找不到或无法加载主类
  15. aspx后台代码写在前段
  16. 20145311 《Java程序设计》第九周学习总结
  17. 用 S5PV210 学习 Linux (三) SD卡下载
  18. 资源很多,你却不会使用——以不变应万变才是自学Java的正确方法
  19. 【LG4609】[FJOI2016]建筑师
  20. VisualSVN 5.1.4破解

热门文章

  1. Python Web开发:使用Django框架创建HolleWorld项目
  2. 免费资源(CDN,顶级域名)汇集
  3. Linux学习笔记之磁盘与文件系统的管理
  4. spring启动图案修改(纯属好玩)
  5. update all line start with -- to space
  6. 前端学习(三十五)模块化es6(笔记)
  7. Java Web入门二
  8. Delphi 运行后错误提示“无效的授权说明”
  9. spark streaming简单示例
  10. Hadoop编程调用HDFS(JAVA)