docker笔记06 - 核心必备知识之Dockerfile构建镜像实战

1.构建dockerfile的命令

[root@iZbp1e9mxelwe7pwimpw8sZ ~]# 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 [])

命令的翻译:

选项:

--添加主机列表添加自定义主机到IP映射(主机:IP)

--生成参数列表设置生成时变量

--从字符串中缓存图像以将其视为缓存源

--cgroup parent string容器的可选父cgroup

--压缩使用gzip压缩生成上下文

--cpu period int限制cpu CFS(完全公平调度程序)周期

--cpu quota int限制cpu CFS(完全公平调度程序)配额

-c、 --cpu共享int cpu共享(相对权重)

--cpuset cpu字符串允许执行的cpu(0-30,1--允许执行(0-301)的微机电系统串

--禁用内容信任跳过图像验证(默认为true-f、 --Dockerfile的文件字符串名称(默认为'PATH/Dockerfile'--强制rm始终移除中间容器

--iidfile字符串将图像ID写入文件

--隔离串容器隔离技术

--图像的标签列表集元数据

-m、 --内存字节内存限制

--内存交换字节交换限制等于内存加交换:'-1'以启用无限交换

--网络字符串设置生成期间运行指令的网络模式(默认“默认”)

--没有缓存生成映像时不使用缓存

--拉总是试图拉图像的新版本

-q、 --quiet取消生成输出并在成功时打印图像ID

--rm在成功生成后删除中间容器(默认为true--安全选项字符串安全选项

--shm size字节/dev/shm的大小

-t、 --标记列表名称和可选的“名称:标记”格式的标记

--目标字符串将目标生成阶段设置为生成。

--ulimit ulimit ulimit选项(默认值[]

2.编写一个名为dockerfile的文件
命令:
docker build -t centos:v1 .

内容:

# this is a dockerfile
FROM centos:7
MAINTAINER hecr 1234@qq.com
RUN echo "开始构建镜像"
WORKDIR /home/hecr
COPY 456.txt /home/hecr
RUN yum install -y net-tools

3.构建dockerfile镜像

[root@iZbp1e9mxelwe7pwimpw8sZ ~]# ls
10  456.txt  dockerfile  hecr
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker build -t centos:v1 .
Sending build context to Docker daemon  62.46kB
Step 1/6 : FROM centos:7
7: Pulling from library/centos
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
 ---> 5e35e350aded
Step 2/6 : MAINTAINER hecr 1234@qq.com
 ---> Running in 68559bf71892
Removing intermediate container 68559bf71892
 ---> 709826a42f4f
Step 3/6 : RUN echo "开始构建镜像"
 ---> Running in 0206f46c75c6
开始构建镜像
Removing intermediate container 0206f46c75c6
 ---> ae2d6ef669e1
Step 4/6 : WORKDIR /home/hecr
 ---> Running in 7c276e1ce143
Removing intermediate container 7c276e1ce143
 ---> 3c0dd5f93ecd
Step 5/6 : COPY 456.txt /home/hecr
 ---> 6a392a883cea
Step 6/6 : RUN yum install -y net-tools
 ---> Running in 81bf9812b1c0
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                          Repository  Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.25.20131004git.el7         base       306 k

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

Total download size: 306 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.25.20131004git.el7.x86_64.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
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-7.1908.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                    1/1 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                    1/1 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                   

Complete!
Removing intermediate container 81bf9812b1c0
 ---> 169452cb0d36
Successfully built 169452cb0d36
Successfully tagged centos:v1
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              v1                  169452cb0d36        31 seconds ago      268MB
centos              7                   5e35e350aded        5 months ago        203MB

4.运行容器,并进入容器验证效果
运行容器命令:
docker run -it centos:v1

[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              v1                  169452cb0d36        31 seconds ago      268MB
centos              7                   5e35e350aded        5 months ago        203MB
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# vim dockerfile 
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker run -it centos:v1
[root@1797f4ee2c57 hecr]# docker ps
bash: docker: command not found
[root@1797f4ee2c57 hecr]# ls       
456.txt
[root@1797f4ee2c57 hecr]# cat 456
cat: 456: No such file or directory
[root@1797f4ee2c57 hecr]# cat 456.txt 
456
aa
aaaaaaaaaaaaaa
[root@1797f4ee2c57 hecr]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        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

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@1797f4ee2c57 hecr]# 

5.文章参考链接
a. https://xdclass.net/#/index
b. https://blog.csdn.net/jiangyu1013/article/details/79967440?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158865629519725211901387%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.57662%2522%257D&request_id=158865629519725211901387&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值