欢迎大家来到IT世界,在知识的湖畔探索吧!
原创 linuxprobe Linux就该这么学
欢迎大家来到IT世界,在知识的湖畔探索吧!
本文档介绍 Docker 镜像制作的两种方法,使用的系统是 CentOS7,文内含长段代码可复制可往左滑,希望对大家有帮助!
Docker Image 的制作两种方法
方法 1:docker commit #保存 container 的当前状态到 image 后,然后生成对应的 image 方法 2:docker build #使用 Dockerfile 文件自动化制作 image
欢迎大家来到IT世界,在知识的湖畔探索吧!
<以上代码可复制粘贴,可往左滑>
方法一:
docker commit
创建一个安装好 apache 工具的容器镜像
欢迎大家来到IT世界,在知识的湖畔探索吧![root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest cac 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 cac 4 months ago 237MB
<以上代码可复制粘贴,可往左滑>
根据容器当前状态做一个 image 镜像:创建一个安装了 apache 工具的 centos 镜像
语法:docker commit “container 的 ID” 或 “image_name”
查看容器 ID
欢迎大家来到IT世界,在知识的湖畔探索吧![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 adf 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 efb4af centos:latest "/bin/bash" 24 minutes ago Exited (0) 23 minutes ago epic_mcclintock [root@Docker ~]# docker commit 1b96e68a3cce centos:apache sha256:b8822ec8a7bbbe7908ebe82a49481cfffb5d0da5872e88 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos apache b8822ec8a7bb 9 seconds ago 280MB centos latest cac 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+ae.x86_64
<以上代码可复制粘贴,可往左滑>
看到httpd软件名称说明基于 apache 镜像的容器创建成功
方法二:
通过:docker build 创建一个基于 centos 的 httpd web 服务器镜像
1、创建工作目录、
欢迎大家来到IT世界,在知识的湖畔探索吧![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
<以上代码可复制粘贴,可往左滑>
注释
欢迎大家来到IT世界,在知识的湖畔探索吧!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 来创建新的 image,并命名为 centos:httpd
欢迎大家来到IT世界,在知识的湖畔探索吧![root@Docker docker-build]# ls
Dockerfile index.html start.sh
[root@Docker docker-build]# docker build -t centos:httpd ./
# 注:./ 表示当前目录,另外你的当前目录下要包含 Dockerfile
Sending build context to Docker daemon 4.096kB
Step 1/5 : FROM centos:latest
---> 470671670cac
Step 2/5 : MAINTAINER
---> Running in 0180810d2ab3
Removing intermediate container 0180810d2ab3
---> 5b9af0184bcf
Step 3/5 : RUN yum -y install httpd
---> Running in 8f5c114649ed
CentOS-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 M
Installing 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
---> 040b5f229962
Step 4/5 : ADD start.sh /usr/local/bin/start.sh
---> 11a106005031
Step 5/5 : ADD index.html /var/www/html/index.html
---> 85b4a3657ced
Successfully built 85b4a3657ced
Successfully 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 cac 4 months ago 237MB # 注:docker 镜像=应用/程序+库
<以上代码可复制粘贴,可往左滑>
运行新生成的镜像
欢迎大家来到IT世界,在知识的湖畔探索吧![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+ae.x86_64 [root@1188a43a4585 /]# exit exit [root@Docker docker-build]#
<以上代码可复制粘贴,可往左滑>
总 结
以上就是 Docker 镜像制作方法的介绍,希望能给大家带来帮助。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/136559.html