1. 基于一个已经存在container创建docker image
docker container commit
-> 简写:docker commit
[root@localhost ~]# docker container commit
"docker container commit" requires at least 1 and at most 2 argument(s).
See 'docker container commit --help'.
Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
1.1 首先查看一下所有的container:
[root@localhost etc]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
712d8fc38a12 centos "/bin/bash" 13 minutes ago Exited (127) 4 seconds ago serene_blackwell
5761e167445a centos "/bin/bash" About an hour ago Exited (0) About an hour ago jovial_brattain
04479c838b35 centos "/bin/bash" About an hour ago Exited (127) About an hour ago sad_heisenberg
76d7c3bb5ae0 hello-world "/hello" 10 hours ago Exited (0) 10 hours ago keen_morse
1.2 执行:docker commit keen_morse helloworld/centos-vim
:
[root@localhost ~]# docker commit keen_morse helloworld/centos-vim
sha256:62cf4fc411f93c02f2c4fb59a44824e6d9f71873285d34bb75e14d720ebda807
查看一下:
[root@localhost etc]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld/centos-vim latest fae5c0510522 About an hour ago 1.85 kB
docker.io/centos latest 5182e96772bf 3 days ago 200 MB
docker.io/hello-world latest 2cb0d9787c4d 4 weeks ago 1.85 kB
这种方式不是很好,其他人不知道这个image是如何创建出来的,具有不安全因素。
2. 用Dockerfile构建镜像
docker image build
-> 简写:docker build
[root@localhost ~]# docker image build
"docker image build" requires exactly 1 argument(s).
See 'docker image build --help'.
Usage: docker image build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
我们以构建vim为例进行简单入门:
2.1 首先创建一个目录
mkdir /root/docker-centos-vim
2.2 创建Dockerfile
- 将目录切换到docker-centos-vim下:
cd /root/docker-centos-vim
- 创建文件:
vim Dockfile
- 在Dockfile中输入如下内容:
FROM centos
RUN yum install -y vim
- 执行构建命令:
docker build -t jerry/centos-vim-new .
;
.
表示根据当前目录下的Dockerfile进行构建