LinuxProbe 发表于 2021-1-27 12:46:43

Docker 镜像制作方法


本文档介绍 Docker 镜像制作的两种方法,使用的系统是 CentOS7
Docker Image 的制作两种方法
方法 1:docker commit #保存 container 的当前状态到 image 后,然后生成对应的 image方法 2:docker build #使用 Dockerfile 文件自动化制作 image方法一:docker commit
创建一个安装好 apache 工具的容器镜像# docker imagesREPOSITORY          TAG               IMAGE ID            CREATED             SIZEcentos            latest            470671670cac      4 months ago      237MB# docker psCONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES# docker run -it centos:latest /bin/bash## yum -y install httpd #在 container 中安装 apache 软件包# exit查看 images 列表# docker imagesREPOSITORY          TAG               IMAGE ID            CREATED             SIZEcentos            latest            470671670cac      4 months ago      237MB根据容器当前状态做一个 image 镜像:创建一个安装了 apache 工具的 centos 镜像语法: docker commit "container 的 ID" 或 "image_name"查看容器 ID# docker ps -aCONTAINER ID      IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES1b96e68a3cce      centos:latest       "/bin/bash"            3 minutes ago       Exited (0) 2 minutes ago                        awesome_hypatia607752360adf      centos:latest       "/bin/bash -c 'while…"   18 minutes ago      Exited (137) 12 minutes ago                     brave_fermi0a297ff99af8      centos:latest       "/bin/bash"            22 minutes ago      Exited (1) 20 minutes ago                         ecstatic_yonathefb4af688330      centos:latest       "/bin/bash"            24 minutes ago      Exited (0) 23 minutes ago                         epic_mcclintock# docker commit 1b96e68a3cce centos:apachesha256:b8822ec8a7bbb325793e7908e355180be82a49481cff197661fb5d0da5872e88# docker imagesREPOSITORY          TAG               IMAGE ID            CREATED             SIZEcentos            apache            b8822ec8a7bb      9 seconds ago       280MBcentos            latest            470671670cac      4 months ago      237MB使用新创建的 centos:apache 镜像,生成一个容器实例# docker run -it centos:apache /bin/bash# rpm -qa httpd httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64看到httpd软件名称说明基于 apache 镜像的容器创建成功方法二:通过:docker build 创建一个基于 centos 的 httpd web 服务器镜像
使用 docker build 创建镜像时,需要使用 Dockerfile 文件自动化制作 image 镜像
Dockerfile 有点像源码编译时./configure 后产生的 Makefile1、创建工作目录
# mkdir /docker-build# cd /docker-build# touch Dockerfile# lsDockerfile注: make 自动化编译时需要 Makefile 文件,自动化创建 docker 镜像时,需要 Dockerfile2、编辑 Dockerfile
Dockerfile 用来创建一个自定义的 image,包含了用户挃定的软件依赖等。# vim DockerfileFROM centos:latestMAINTAINER RUN yum -y install httpdADD start.sh /usr/local/bin/start.shADD index.html /var/www/html/index.htmlCMD echo hello world注释FROM centos:latest # FROM 基于哪个镜像MAINTAINER# MAINTAINER 镜像创建者RUN yum -y install httpd #RUN 安装软件用ADD start.sh /usr/local/bin/start.shADD index.html /var/www/html/index.html# ADD 将文件拷贝到新产生的镜像的文件系统对应的路径。所有拷贝到新镜像中的文件和文件夹权限为 0755,uid 和 gid 为 0CMD echo hello world #container 启动时执行的命令或启动服务,但是一个 Dockerfile 中只能有一条 CMD 命令,有多条则另执行最后一条 CMD3、创建 start.sh 脚本启动 httpd 服务和 apache 默认首页 index.html 文件
# echo "#!/bin/bash" >> start.sh# echo "/usr/sbin/httpd -DFOREGROUND" >> start.sh注:/usr/sbin/httpd -DFOREGROUND 相当于执行了 systemctl start httpd# chmod a+x start.sh创建 index.html# echo "docker image buildtest from jaking" > index.html4、使用命令 build 来创建新的 image
语法:docker build -t 父镜像名:镜像的 tag Dockerfile 文件所在路径
-t :表示 tag,镜像名例:使用命令 docker build 来创建新的 image,并命名为 centos:httpd# lsDockerfileindex.htmlstart.sh# docker build -t centos:httpd ./# 注: ./ 表示当前目录,另外你的当前目录下要包含 DockerfileSending build context to Docker daemon4.096kBStep 1/5 : FROM centos:latest ---> 470671670cacStep 2/5 : MAINTAINER---> Running in 0180810d2ab3Removing intermediate container 0180810d2ab3 ---> 5b9af0184bcfStep 3/5 : RUN yum -y install httpd ---> Running in 8f5c114649edCentOS-8 - AppStream                            228 kB/s | 7.0 MB   00:31    CentOS-8 - Base                                 547 kB/s | 2.2 MB   00:04    CentOS-8 - Extras                               431B/s | 5.9 kB   00:14    Dependencies resolved.================================================================================ Package         Arch   Version                               Repo       Size================================================================================Installing: httpd             x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 MInstalling dependencies: apr               x86_64 1.6.3-9.el8                           AppStream 125 k apr-util          x86_64 1.6.1-6.el8                           AppStream 105 k centos-logos-httpd                   noarch 80.5-2.el8                            AppStream24 k ...省略部分输出信息...                Complete!Removing intermediate container 8f5c114649ed ---> 040b5f229962Step 4/5 : ADD start.sh /usr/local/bin/start.sh ---> 11a106005031Step 5/5 : ADD index.html /var/www/html/index.html ---> 85b4a3657cedSuccessfully built 85b4a3657cedSuccessfully tagged centos:httpd查看 images 列表# docker imagesREPOSITORY          TAG               IMAGE ID            CREATED             SIZEcentos            httpd               85b4a3657ced      45 seconds ago      280MBcentos            apache            b8822ec8a7bb      20 minutes ago      280MBcentos            latest            470671670cac      4 months ago      237MB# 注:docker 镜像=应用/程序+库运行新生成的镜像# docker run -it centos:httpd /bin/bash # lsbinetc   lib    lost+foundmntprocrun   srvtmpvardevhomelib64media       optrootsbinsysusr# rpm -qa httpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64# exitexit#总结
以上就是 Docker 镜像制作方法的介绍,希望能给大家带来帮助。

share132 发表于 2021-4-14 15:23:44

版主你发完自己有浏览帖子吗?排版太乱了阅读困难
页: [1]
查看完整版本: Docker 镜像制作方法