前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker Review - dockerfile 实战_给基础镜像增加功能

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

作者头像
小小工匠
发布2022-11-30 14:07:39
5990
发布2022-11-30 14:07:39
举报
文章被收录于专栏:小工匠聊架构

文章目录

在这里插入图片描述
在这里插入图片描述

文件格式

代码语言:javascript
复制
##  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 增加功能

代码语言:javascript
复制
[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 来完成镜像的制作

代码语言:javascript
复制
[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 Dockerfile

Options:
      --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构建镜像

代码语言:javascript
复制
# 通过这个文件构建镜像 
#  命令 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 k
 vim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 k
 which               x86_64      2.21-12.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total 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 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : which-2.21-12.el8.x86_64                               1/5
  Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                2/5
  Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    3/5
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5
  Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5
  Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5
  Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    5/5
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5
  Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/5
  Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/5
  Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/5
  Verifying        : which-2.21-12.el8.x86_64                               5/5

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64
  vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
  which-2.21-12.el8.x86_64

Complete!
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 k

Transaction Summary
================================================================================
Install  1 Package

Total 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 transaction
  Preparing        :                                                        1/1
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64

Complete!
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 这

代码语言:javascript
复制
上一步指定了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 1500
        inet 172.18.0.3  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 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 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX 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 查看镜像历史信息

代码语言:javascript
复制
[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 ~]#
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 文件格式
  • 实操Docker file
    • 给基础镜像centos 增加功能
      • dockerfile镜像制作
      • docker build 通过dockerfile构建镜像
      • 启动新镜像,验证
    • docker history 查看镜像历史信息
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档