内容简介:Docker镜像创建方法有三种,分别为基于已有镜像创建、基于本地模板创建以及基于Dockerfile创建。这里主要记录下基于Dockerfile创建方法,Dockerfile是由一组指令组成的文件,其中每条指令对应Linux中的一条命令,Docker程序将读取Dockerfile中的指令生成指定镜像。Dockerfile结构大致分为四个部分:
创建镜像三种方法
Docker镜像创建方法有三种,分别为基于已有镜像创建、基于本地模板创建以及基于Dockerfile创建。
通过Dockerfile创建镜像
这里主要记录下基于Dockerfile创建方法,Dockerfile是由一组指令组成的文件,其中每条指令对应 Linux 中的一条命令,Docker程序将读取Dockerfile中的指令生成指定镜像。
Dockerfile结构大致分为四个部分: 基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令 。Dockerfile每行支持一条指令,每条指令可带多个参数,支持使用以#号开头的注释。
Docker操作指令
指令 | 含义 |
---|---|
FROM镜像 | 指定新镜像所基于的镜像,必须为第一条指令 |
MAINTAINER 名字 | 新镜像的维护人信息 |
RUN 命令 | 在所基于的镜像上执行命令,并提交到新镜像中 |
EXPOSE端口号 | 指定新镜像加载到 Docker 时开启的端口号 |
ENV 环境变量 变量值 | 设置一个环境变量的值,会被后面的RUN使用 |
ADD 源文件/目录 目标文件/目录 | 将源文件复制到目标文件,源文件要与Docker位于同一目录下,或者为一个URL |
COPY 源文件/目录 目标文件/目录 | 将本地主机上的源文件/目录复制到目标地点,源文件/目录要与Dockerfile在同一目录下 |
VOLUME["目录"] | 在容器中创建一个挂载点 |
USER 用户名 /UID | 指定运行容器时的用户 |
WORKDIR 路径 | 为后续的RUN、CMD、ENTRYPOINT指定工作目录 |
ONBUILD命令 | 指定所生成的镜像作为一个基础镜像时所要运行的命令 |
CMD["要运行的程序","参数1","参数2"] | 指定启动容器时运行的命令或脚本,只能有一条CMD命令,多条时只有最后一条被执行 |
以下通过Dockerfile来创建httpd镜像
1.创建镜像工作目录并进入
mkdir apache cd apache/
2.创建并编写dockerfile文件
vim Dockerfile
#基于centos镜像 FROM centos #维护人的信息 MAINTAINER The CentOS Project <303323496@qq.com> #安装httpd软件包 RUN yum -y update RUN yum -y install httpd #开启80端口 EXPOSE 80 #复制网站首页文件至镜像中web站点下 ADD index.html /var/www/html/index.html #复制该脚本至镜像中,并修改其权限 ADD run.sh /run.sh RUN chmod 775 /run.sh #当启动容器时执行的脚本文件 CMD ["/run.sh"]
3.创建脚本文件
vim run.sh
#!/bin/bash #清空httpd缓存文件 rm -rf /run/httpd/* #启动httpd服务 exec /usr/sbin/apachectl -D FOREGROUND
4.创建网站首页文件
echo "Thiss is Nginx webapp." > index.html
5.查看当前目录文件列表
ls
6.通过Dockerfile文件生成镜像
docker build -t httpd:centos .
Sending build context to Docker daemon 4.096kB Step 1/9 : FROM centos ---> 5182e96772bf Step 2/9 : MAINTAINER The CentOS Project <303323496@qq.com> ---> Running in 6d7c8eae519d ---> 46fc589066ff Removing intermediate container 6d7c8eae519d Step 3/9 : RUN yum -y update ---> Running in 444105af6041 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.zju.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package centos-release.x86_64 0:7-5.1804.1.el7.centos will be updated ---> Package centos-release.x86_64 0:7-5.1804.4.el7.centos will be an update --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Updating: centos-release x86_64 7-5.1804.4.el7.centos updates 25 k Transaction Summary ================================================================================ Upgrade 1 Package Total download size: 25 k Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. Public key for centos-release-7-5.1804.4.el7.centos.x86_64.rpm is not installed Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 warning: /var/cache/yum/x86_64/7/updates/packages/centos-release-7-5.1804.4.el7.centos.x86_64.rpm:Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-5.1804.1.el7.centos.x86_64 (@Updates) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : centos-release-7-5.1804.4.el7.centos.x86_64 1/2 Cleanup : centos-release-7-5.1804.1.el7.centos.x86_64 2/2 Verifying : centos-release-7-5.1804.4.el7.centos.x86_64 1/2 Verifying : centos-release-7-5.1804.1.el7.centos.x86_64 2/2 Updated: centos-release.x86_64 0:7-5.1804.4.el7.centos Complete! ---> 8a4bb67da908 Removing intermediate container 444105af6041 Step 4/9 : RUN yum -y install httpd ---> Running in 982f008f5f3c Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed --> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed ---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.4.6-80.el7.centos.1 updates 2.7 M Installing for dependencies: apr x86_64 1.4.8-3.el7_4.1 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M httpd-tools x86_64 2.4.6-80.el7.centos.1 updates 90 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ================================================================================ Install 1 Package (+5 Dependent packages) Total download size: 24 M Installed size: 31 M Downloading packages: -------------------------------------------------------------------------------- Total 1.1 MB/s | 24 MB 00:21 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-3.el7_4.1.x86_64 1/6 Installing : apr-util-1.5.2-6.el7.x86_64 2/6 Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64 3/6 Installing : centos-logos-70.0.6-3.el7.centos.noarch 4/6 Installing : mailcap-2.1.41-2.el7.noarch 5/6 Installing : httpd-2.4.6-80.el7.centos.1.x86_64 6/6 Verifying : mailcap-2.1.41-2.el7.noarch 1/6 Verifying : httpd-tools-2.4.6-80.el7.centos.1.x86_64 2/6 Verifying : apr-util-1.5.2-6.el7.x86_64 3/6 Verifying : httpd-2.4.6-80.el7.centos.1.x86_64 4/6 Verifying : apr-1.4.8-3.el7_4.1.x86_64 5/6 Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6 Installed: httpd.x86_64 0:2.4.6-80.el7.centos.1 Dependency Installed: apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7 Complete! ---> d3f0d6f963cb Removing intermediate container 982f008f5f3c Step 5/9 : EXPOSE 80 ---> Running in b511725feca7 ---> 7f7887f5875e Removing intermediate container b511725feca7 Step 6/9 : ADD index.html /var/www/html/index.html ---> 3c5953c9fd43 Removing intermediate container 85dad653f265 Step 7/9 : ADD run.sh /run.sh ---> 41980651502b Removing intermediate container 91ff7e24daa4 Step 8/9 : RUN chmod 775 /run.sh ---> Running in 7aebb5d8f194 ---> f673460e23c7 Removing intermediate container 7aebb5d8f194 Step 9/9 : CMD /run.sh ---> Running in 9c0f5c4f1e32 ---> 551b9fc524ba Removing intermediate container 9c0f5c4f1e32 Successfully built 551b9fc524ba Successfully tagged httpd:centos
7.镜像文件加载到容器中运行
docker run -d -p 80:80 --name=httpd httpd:centos
8.查看运行中的容器
docker ps
9.运行浏览器测试httpd服务
http://192.168.96.44:80
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 从0开始制造一个docker自定义镜像
- KVM镜像制作及挂载镜像文件
- 查看Docker镜像仓库中镜像的所有标签
- Vagrant 使用国内镜像安装插件和 box 镜像
- 不要轻易使用 Alpine 镜像来构建 Docker 镜像,有坑!
- Harbor 1.7 发布,在线 GC、镜像历史、镜像复制
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。