方法1:docker commit

说明:从容器生成新镜像。

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

用法

####查看当前运行的容器####
[root@harbor <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7377e020567a redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp redis

#####在当前运行的容器redis的/tmp目录下创建1.txt文件
[root@harbor </sub>]# docker exec -it redis touch /tmp/1.txt

#####提交当前运行容器状态为新的docker 镜像redis:v1
[root@harbor <sub>]# docker commit redis redis:v1
sha256:cc6cf492f5953414156563ca122d7e1ef151bba3b4ffad103f586a050d08faba

######docker image中查看新生成的镜像
[root@harbor </sub>]# docker images | grep redis
redis v1 cc6cf492f595 14 seconds ago 113MB
redis latest 7614ae9453d1 3 months ago 113MB

#####运行新的镜像并查看/tmp目录下1.txt文件
[root@harbor ~]# docker run -it redis:v1 ls /tmp/
1.txt

方法2:docker build

说明:使用Dockerfile文件自动生成docker 镜像。

用法

Dockerfile文件如下:

FROM centos:centos7.9.2009

COPY ./jdk-8u321-linux-x64.rpm /tmp/
RUN yum update -y \
&& yum install telnet -y \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& yum install -y /tmp/jdk-8u321-linux-x64.rpm \
&& rm -rf /tmp/jdk-8u321-linux-x64.rpm \
&& yum clean all

构建docker镜像:

docker build --no-cache -t jdk:v6 .

查看生成的docker镜像:

[root@harbor ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jdk v6 4b2890f7fb4d 49 seconds ago 818MB