Docker Review - dockerfile 实战_给基础镜像增加功能
生活随笔
收集整理的這篇文章主要介紹了
Docker Review - dockerfile 实战_给基础镜像增加功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 文件格式
- 實操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/nginxDockerfile 分為四部分:基礎鏡像信息、維護者信息、鏡像操作指令、容器啟動執行指令。
- 開始必須要指明所基于的鏡像名稱
- 接下來通常是維護者信息
- 后面是鏡像操作指令,例如 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 实战_给基础镜像增加功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker Review - dock
- 下一篇: Docker Review - dock