文章目录

  • 文件格式
  • 实操Docker file
    • 给基础镜像centos 增加功能
      • dockerfile镜像制作
      • docker build 通过dockerfile构建镜像
      • 启动新镜像,验证
    • docker history 查看镜像历史信息


文件格式

##  Dockerfile文件格式# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..# 1、第一行必须指定 基础镜像信息
FROM ubuntu# 2、维护者信息
MAINTAINER docker_user docker_user@email.com# 3、镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf# 4、容器启动执行指令
CMD /usr/sbin/nginx

Dockerfile 分为四部分:基础镜像信息、维护者信息、镜像操作指令、容器启动执行指令。

  • 开始必须要指明所基于的镜像名称
  • 接下来通常是维护者信息
  • 后面是镜像操作指令,例如 RUN 指令。每执行一条RUN 指令,镜像添加新的一层,并提交
  • 最后是 CMD 指令,来指明运行容器时的操作命令。

实操Docker file

给基础镜像centos 增加功能


[root@VM-0-7-centos ~]# docker run -it centos
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]# ifconfig    没有ifconfig该命令
bash: ifconfig: command not found
[root@c967bcaa1b52 /]# vim a.txt    没有vim该命令
bash: vim: command not found
[root@c967bcaa1b52 /]#
[root@c967bcaa1b52 /]# exit
exit

启动容器,进入这个容器内,发现 vim 和 ipconfig 命令都是没有的 , 那怎么在搞到镜像里面去呢?

---------请 当家的 dockerfile来秀一秀吧

dockerfile镜像制作

接下来,我们通过dockerfile 来完成镜像的制作

[root@VM-0-7-centos ~]# pwd
/root
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# mkdir  dfiles
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# cd dfiles/
[root@VM-0-7-centos dfiles]#  编写dockerfile文件
[root@VM-0-7-centos dfiles]# vim artisan-centos
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#  如果命令记不住  --help少不了
[root@VM-0-7-centos dfiles]# docker build --help   Usage:  docker build [OPTIONS] PATH | URL | -Build an image from a DockerfileOptions:--add-host list           Add a custom host-to-IP mapping (host:ip)--build-arg list          Set build-time variables--cache-from strings      Images to consider as cache sources--cgroup-parent string    Optional parent cgroup for the container--compress                Compress the build context using gzip--cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period--cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota-c, --cpu-shares int          CPU shares (relative weight)--cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)--disable-content-trust   Skip image verification (default true)-f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')--force-rm                Always remove intermediate containers--iidfile string          Write the image ID to the file--isolation string        Container isolation technology--label list              Set metadata for an image-m, --memory bytes            Memory limit--memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap--network string          Set the networking mode for the RUN instructions during build (default "default")--no-cache                Do not use cache when building the image--pull                    Always attempt to pull a newer version of the image-q, --quiet                   Suppress the build output and print image ID on success--rm                      Remove intermediate containers after a successful build (default true)--security-opt strings    Security options--shm-size bytes          Size of /dev/shm-t, --tag list                Name and optionally a tag in the 'name:tag' format--target string           Set the target build stage to build.--ulimit ulimit           Ulimit options (default [])
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#


docker build 通过dockerfile构建镜像


# 通过这个文件构建镜像
#  命令 docker build -f 文件路径 -t 镜像名:[tag] .  (路径)[root@VM-0-7-centos dfiles]# docker build -f artisan-centos -t artisan-centos:0.1  .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM centos---> 5d0da3dc9764
Step 2/9 : MAINTAINER  artisan@artisan.com---> Running in 27cb6c9d895c
Removing intermediate container 27cb6c9d895c---> 29294f1d8e36
Step 3/9 : ENV MYPATH /usr/local---> Running in 05c3deb8fd0f
Removing intermediate container 05c3deb8fd0f---> db06a98c3f9d
Step 4/9 : WORKDIR  $MYPATH---> Running in 2b1072aaba22
Removing intermediate container 2b1072aaba22---> a8849a4c2da5
Step 5/9 : RUN yum -y install vim---> Running in 1ed0427ca12b
CentOS Linux 8 - AppStream                      8.8 MB/s | 9.3 MB     00:01
CentOS Linux 8 - BaseOS                          19 MB/s | 7.5 MB     00:00
CentOS Linux 8 - Extras                          27 kB/s |  10 kB     00:00
Dependencies resolved.
================================================================================Package             Arch        Version                   Repository      Size
================================================================================
Installing:vim-enhanced        x86_64      2:8.0.1763-15.el8         appstream      1.4 M
Installing dependencies:gpm-libs            x86_64      1.20.7-17.el8             appstream       39 kvim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 Mvim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 kwhich               x86_64      2.21-12.el8               baseos          49 kTransaction Summary
================================================================================
Install  5 PackagesTotal download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm        364 kB/s |  39 kB     00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 818 kB/s |  48 kB     00:00
(3/5): which-2.21-12.el8.x86_64.rpm             1.3 MB/s |  49 kB     00:00
(4/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm  5.3 MB/s | 1.4 MB     00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm     17 MB/s | 6.3 MB     00:00
--------------------------------------------------------------------------------
Total                                           5.6 MB/s | 7.8 MB     00:01
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      130 kB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65DFrom       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                        1/1Installing       : which-2.21-12.el8.x86_64                               1/5Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                2/5Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    3/5Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    5/5Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/5Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/5Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/5Verifying        : which-2.21-12.el8.x86_64                               5/5Installed:gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarchwhich-2.21-12.el8.x86_64Complete!
Removing intermediate container 1ed0427ca12b---> f3576d019b4c
Step 6/9 : RUN yum -y install net-tools---> Running in 34aa99eae420
Last metadata expiration check: 0:00:12 ago on Wed Oct 13 16:48:57 2021.
Dependencies resolved.
================================================================================Package         Architecture Version                        Repository    Size
================================================================================
Installing:net-tools       x86_64       2.0-0.52.20160912git.el8       baseos       322 kTransaction Summary
================================================================================
Install  1 PackageTotal download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm   9.5 MB/s | 322 kB     00:00
--------------------------------------------------------------------------------
Total                                           604 kB/s | 322 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                        1/1Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1Installed:net-tools-2.0-0.52.20160912git.el8.x86_64Complete!
Removing intermediate container 34aa99eae420---> d71b7d53d1e3
Step 7/9 : EXPOSE 80---> Running in 0cb2dfa57d63
Removing intermediate container 0cb2dfa57d63---> 8c68c1ba1995
Step 8/9 : CMD echo $MYPATH---> Running in 5d1480e72c54
Removing intermediate container 5d1480e72c54---> 03bb5037e4cd
Step 9/9 : CMD /bin/bash---> Running in 453d2bddf83b
Removing intermediate container 453d2bddf83b---> fed722a5031b
Successfully built fed722a5031b
Successfully tagged artisan-centos:0.1
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#
[root@VM-0-7-centos dfiles]#


启动新镜像,验证

启动这个镜像,再次进入这个容器内,看看 vim 和 ipconfig 这


上一步指定了tag 0.1 ,所里这里要指定tag号,不然会找latest
[root@VM-0-7-centos dfiles]# docker run -it artisan-centos:0.1
[root@21ec25b4785f local]#
[root@21ec25b4785f local]#
[root@21ec25b4785f local]#
[root@21ec25b4785f local]#
[root@21ec25b4785f local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.18.0.3  netmask 255.255.0.0  broadcast 172.18.255.255ether 02:42:ac:12:00:03  txqueuelen 0  (Ethernet)RX packets 7  bytes 586 (586.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@21ec25b4785f local]# vim a.txt
[root@21ec25b4785f local]#


docker history 查看镜像历史信息


[root@VM-0-7-centos ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                                                  NAMES
21ec25b4785f   artisan-centos:0.1    "/bin/sh -c /bin/bash"   13 minutes ago   Up 13 minutes   80/tcp                                                                                 tender_mcclintock
3eab753d9573   portainer/portainer   "/portainer"             7 days ago       Up 7 days       0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   portainer[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# docker history artisan-centos:0.1
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
fed722a5031b   15 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B
03bb5037e4cd   15 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
8c68c1ba1995   15 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B
d71b7d53d1e3   15 minutes ago   /bin/sh -c yum -y install net-tools             32.3MB
f3576d019b4c   15 minutes ago   /bin/sh -c yum -y install vim                   72.6MB
a8849a4c2da5   16 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B
db06a98c3f9d   16 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B
29294f1d8e36   16 minutes ago   /bin/sh -c #(nop)  MAINTAINER artisan@artisa…   0B
5d0da3dc9764   3 weeks ago      /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      3 weeks ago      /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>      3 weeks ago      /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB
[root@VM-0-7-centos ~]#

Docker Review - dockerfile 实战_给基础镜像增加功能相关推荐

  1. Docker Review - dockerfile 实战_使用dockerfile制作tomcat镜像

    文章目录 Pre Docker 官方镜像 Dockerfile dockerfile制作tomcat镜像 准备软件 编写Dockerfile文件 dockerfile构建镜像 启动镜像 测试访问tom ...

  2. 实战:借助ucloud镜像加速功能下载镜像(亲测成功)-2022.1.1

    实战:借助ucloud镜像加速功能下载镜像(亲测成功)-2022.1.1 目录 文章目录 实战:借助ucloud镜像加速功能下载镜像(亲测成功)-2022.1.1 目录 实验环境 实验软件 1.背景 ...

  3. Docker Review - dockerfile 入门篇

    文章目录 Docker 镜像.容器 .Docker file 概述 Docker File文件格式 Docker File 解读 1.FROM 指定基础镜像 2.RUN 执行命令 3.COPY 复制文 ...

  4. Dockerfile文件-FROM指定基础镜像

    1 FROM 不写会报错 2 FROM 运行一次就可以了 第二次Docker images 查看镜像id,写到FROM后,例如: FROM b7dff675qwe As build Posted by ...

  5. docker 安装mysql实战_使用docker搭建mysql实例教程

    mysql服务器是目前比较流行的开源数据库服务器,下面介绍如何使用docker来做一个mysql数据库服务 从网站直接拉一个mysql的镜像core @ localhost〜/ base $ dock ...

  6. docker中如何制作自己的基础镜像

    一.本地镜像 举个例子:现在把自己的开发环境打包,取名为centos6-base.tar,然后在docker中,以centos6-base.tar作为基准镜像. 1.创建自己的镜像,放置于/root目 ...

  7. Docker创建Dockerfile脚本构建jdk1.8镜像并启动容器示例

  8. docker常用命令 | game2048网页游戏 | nginx镜像 | 数据卷管理 | docker网络管理 | 构建apache镜像 | Dockerfile常用的命令

    DOCKER https://www.docker.com http://www.runoob.com/docker/docker-command-manual.html docker命令大全 镜像用 ...

  9. docker新增目录映射_开发也需了解的运维知识之Docker

    点击上方"码农沉思录",选择"设为星标"优质文章,及时送达docker容器化部署应用可以简化应用的部署流程.假设部署一个应用需要在机器上安装和配置nginx.t ...

最新文章

  1. java 去掉 时期中的图片,去除图片浅色背景(Java 实现)
  2. ubuntu下移植QT基本流程
  3. JEPLUS表格组件数据平铺——JEPLUS软件快速开发平台
  4. Swift里计数相关的小细节
  5. jsx中的注释的写法
  6. leetcode 714. 买卖股票的最佳时机含手续费(dp)
  7. 百度2015校园招聘软件开发笔试题及答案
  8. 最近程序员频繁被抓,如何避免面向监狱编程?!
  9. java递归mysql生成树_JavaScript图形实例:递归生成树
  10. 云图说|一张图带你了解华为云分布式数据库中间件
  11. python数据结构-链表
  12. 轻松四步配置Oracle数据库监听
  13. ECSHOP解决大量重复页面问题,提高SEO效果
  14. 制作一个简单HTML个人网页网页(HTML+CSS)大话西游之大圣娶亲电影网页设计
  15. 贝叶斯回归 matlab,逻辑回归模型的贝叶斯分析
  16. [完全免费] 在线UML State Diagram 状态机图工具 - 教程第7部分
  17. 网络安全等级保护拓扑图大全
  18. 【区块链扩容】侧链技术 Plasma(Layer 2)
  19. html中数字的格式设置,fmt:formatNumber 标签 | 菜鸟教程
  20. 【Docker学习笔记 二】Docker安装、运行流程与常用命令

热门文章

  1. 环信ease-sdk-3.3.4_r1 集成用户头像 昵称功能的简单处理方法
  2. java trackid_Java Preference.getContext方法代码示例
  3. top99 slam
  4. c++可视化开发_页面可视化配置搭建工具技术要点
  5. pytorch函数整理
  6. 听说你想去大厂看学妹,带你看看京东软件产品经理面经
  7. Tableau必知必会之妙用Fixed函数聚合分析维度
  8. HMM模型和Viterbi算法
  9. 账号 linux_Linux入门之UID和GID(用户ID和组ID)
  10. 目标检测 - 如何在图片中标记Annotations中的坐标信息?