CentOS7下制作Docker 镜像具体方法

Docker 镜像如何制作,本篇文章将在CentOS7下为大家演示一下制作Docker 镜像具体方法,有需要的小伙伴可以参考一下。

  • Docker Image 的制作两种方法

    方法 1:docker commit #保存 container 的当前状态到 image 后,然后生成对应的 image
    方法 2:docker build #使用 Dockerfile 文件自动化制作 image

    方法一:docker commit

    创建一个安装好 apache 工具的容器镜像

    [root@Docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              latest              470671670cac        4 months ago        237MB
    [root@Docker ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    [root@Docker ~]# docker run -it centos:latest /bin/bash
    [root@1b96e68a3cce /]#
    [root@1b96e68a3cce /]# yum -y install httpd #在 container 中安装 apache 软件包
    [root@1b96e68a3cce /]# exit

    查看 images 列表

    [root@Docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              latest              470671670cac        4 months ago        237MB
    

    根据容器当前状态做一个 image 镜像:创建一个安装了 apache 工具的 centos 镜像

    语法: docker commit “container 的 ID” 或 “image_name”

    查看容器 ID

    [root@Docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
    1b96e68a3cce        centos:latest       "/bin/bash"              3 minutes ago       Exited (0) 2 minutes ago                          awesome_hypatia
    607752360adf        centos:latest       "/bin/bash -c 'while…"   18 minutes ago      Exited (137) 12 minutes ago                       brave_fermi
    0a297ff99af8        centos:latest       "/bin/bash"              22 minutes ago      Exited (1) 20 minutes ago                         ecstatic_yonath
    efb4af688330        centos:latest       "/bin/bash"              24 minutes ago      Exited (0) 23 minutes ago                         epic_mcclintock
    [root@Docker ~]# docker commit 1b96e68a3cce centos:apache
    sha256:b8822ec8a7bbb325793e7908e355180be82a49481cff197661fb5d0da5872e88
    [root@Docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              apache              b8822ec8a7bb        9 seconds ago       280MB
    centos              latest              470671670cac        4 months ago        237MB
    

    使用新创建的 centos:apache 镜像,生成一个容器实例

    [root@Docker ~]# docker run -it centos:apache /bin/bash
    [root@e4c295d27581 /]# 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 后产生的 Makefile

    1、创建工作目录

    [root@Docker ~]# mkdir /docker-build
    [root@Docker ~]# cd /docker-build
    [root@Docker docker-build]# touch Dockerfile
    [root@Docker docker-build]# ls
    Dockerfile
    注: make 自动化编译时需要 Makefile 文件,自动化创建 docker 镜像时,需要 Dockerfile
    

    2、编辑 Dockerfile

    Dockerfile 用来创建一个自定义的 image,包含了用户挃定的软件依赖等。
    [root@Docker docker-build ]# vim Dockerfile
    FROM centos:latest
    MAINTAINER
    RUN yum -y install httpd
    ADD start.sh /usr/local/bin/start.sh
    ADD index.html /var/www/html/index.html
    CMD echo hello world
    

    注释

    FROM centos:latest # FROM 基于哪个镜像
    MAINTAINER  # MAINTAINER 镜像创建者
    RUN yum -y install httpd #RUN 安装软件用
    ADD start.sh /usr/local/bin/start.sh
    ADD index.html /var/www/html/index.html
    # ADD 将文件拷贝到新产生的镜像的文件系统对应的路径。所有拷贝到新镜像中的
    文件和文件夹权限为 0755,uid 和 gid 为 0
    CMD echo hello world #container 启动时执行的命令或启动服务,但是一个 Dockerfile 中只能有一条 CMD 命令,有多条则另执行最后一条 CMD

    3、创建 start.sh 脚本启动 httpd 服务和 apache 默认首页 index.html 文件

    [root@Docker docker-build ]# echo "#!/bin/bash" >> start.sh[root@Docker docker-build ]# echo "/usr/sbin/httpd -DFOREGROUND" >> start.sh注:/usr/sbin/httpd -DFOREGROUND 相当于执行了 systemctl start httpd[root@Docker docker-build ]# chmod a+x start.sh创建 index.html[root@Docker docker-build ]# echo "docker image build  test from jaking" > index.html

    4、使用命令 build 来创建新的 image

    语法:docker build -t 父镜像名:镜像的 tag Dockerfile 文件所在路径 -t :表示 tag,镜像名

    例:使用命令 docker build 来创建新的 image,并命名为 centos:httpd

    [root@Docker docker-build]# lsDockerfile  index.html  start.sh[root@Docker docker-build]# docker build -t centos:httpd ./# 注: ./ 表示当前目录,另外你的当前目录下要包含 DockerfileSending build context to Docker daemon  4.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                               431  B/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                            AppStream  24 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 列表

    [root@Docker docker-build]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              httpd               85b4a3657ced        45 seconds ago      280MB
    centos              apache              b8822ec8a7bb        20 minutes ago      280MB
    centos              latest              470671670cac        4 months ago        237MB
    # 注:docker 镜像=应用/程序+库

    运行新生成的镜像

    [root@Docker docker-build]# docker run -it centos:httpd /bin/bash 
    [root@1188a43a4585 /]# ls
    bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
    dev  home  lib64  media       opt  root  sbin  sys  usr
    [root@1188a43a4585 /]# rpm -qa httpd
    httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
    [root@1188a43a4585 /]# exitexit
    [root@Docker docker-build]#

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/221122.html<

(0)
管理的头像管理
上一篇2025-04-14 21:31
下一篇 2025-04-14 21:33

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注