docker介绍:
它是一个开源的软件项目,在Linux操作系统上,docker提供了一个额外的软件抽象层及操作系统层虚拟化的自动管理机制。
容器介绍:
把自己的应用程序,根据某个依赖的基础镜像,生成一个应用程序镜像应用程序镜像,可以运行在任何部署了Docker环境的机器上。
[root@k8s-master01 ~]# docker version #查看docker版本信息
Client: Docker Engine - Community
Version: 20.10.18
API version: 1.41
Go version: go1.18.6
Git commit: b40c2f6
Built: Thu Sep 8 23:14:08 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.18
API version: 1.41 (minimum version 1.12)
Go version: go1.18.6
Git commit: e42327a
Built: Thu Sep 8 23:12:21 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.8
GitCommit: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@k8s-master01 ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.9.1-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 14 #docker容器数
Running: 12 #运行钟的容器数
Paused: 0
Stopped: 2 #停止的容器数
Images: 7 #docker 镜像数量
Server Version: 20.10.18
Storage Driver: overlay2 #存储驱动
Backing Filesystem: xfs #必须是xfs文件系统
Supports d_type: true #必须支持d_type
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file #日志存储类型,默认存到本地(Docker Root Dir: /var/lib/docker)
Cgroup Driver: systemd
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog #日志支持种类
Swarm: inactive #docker官方支持的编排工具,inactive未开启
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc #运行容器的标准
Init Binary: docker-init
containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.12-1.el7.elrepo.x86_64 #系统内核版本,低于3.18是不持支overlay2
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.829GiB
Name: k8s-master01
ID: WT7K:XEX5:Q6MH:RLRZ:JBPW:THIE:ORPQ:CSJL:QBJY:2723:JDNB:A2H2
Docker Root Dir: /var/lib/docker #挂载路径可以更改,最好单独使用一块磁盘ssd来挂载
Debug Mode: false
Registry: https://index.docker.io/v1/ #默认官方的镜像仓库
Labels:
Experimental: false
Insecure Registries: #可能有的是没有https的,所以加入一个不安全的连接的配置(vim /etc/docker/daemon.json)
127.0.0.0/8
Live Restore Enabled: false #生产环境中,要设置为true,重启docker进程后,运行中的容器不会被重启 注:/etc/docker/daemon.json 配置会经常改
[root@k8s-master01 ~]# xfs_info /
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3079680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=12318720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1 #此处为1 是支持的
log =internal bsize=4096 blocks=6015, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@k8s-master01 containers]# cd /var/lib/docker #进入日志存储路径
[root@k8s-master01 docker]# ls
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@k8s-master01 docker]# cd containers/
[root@k8s-master01 containers]# ls #正在运行的容器
0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172 6b17c5a9f04d0a80d0bd84080a678a986270c55cb979ad80f0c0c29bf21531f3
18c887a865b0af8ee29012929d4c90c6c797a21560dbdd577fe3af2c618383e6 75ce0bb5021ddbf3e91594956428e0ad6dc0b179b4dec17fd6292993f860b9eb
2a4ba529bf292d85d8758b28fc21a020ddca231abe132d89d2966312f5a42dd2 86425103ed0ba29a9526732330a8ab1fc381c2e3b97c67da4a4d088a7b226418
2d7d4499bc618e59deba4666a9128c0b17eda6a95c0f4000ddb133abb29e2b5b a1a2112d5250ddb90cfb52da4e6da0fa2b2b7d189db7228b39b7d50f95591a17
48c02cc4a40f36019f98d1ce7c279eddc0fa2824e62843efee79c9ca6902521c caa1dc4a12eec9b2d03021537e67c03a67476c6a93f5c89f120699509219ac62
624bbbd5c8daf67f7c669504ad15a6b181b6be43d9621bc377e0674b55621aa2 d0a4db17e647d3e1c52c3db7dfad0b03e1572c6d57bb5b87d0aa5962b0b22e42
690124fbd1b9437c373170ffd820b36d5e87bf00d663a7e873b8b58b8cf3c30e fd143d419ff0981e0118b066428827af4fbf343cb76d1468ec25895c33bb08c2
[root@k8s-master01 containers]# cd 0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172 #随机进入一个容器查看
[root@k8s-master01 0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172]# ls #json.log结尾的就是该容器的日志
0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172-json.log config.v2.json hostname mounts
checkpoints hostconfig.json hosts resolv.conf
[root@k8s-master01 0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172]# tail -f 0cf34c08a2e0ba7d3cd1303dda43822e6cf8d35f2f6293589f2c284184a54172-json.log
容器关闭后,这些日志会被清理掉
[root@k8s-master01 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7364 [OK] #这里代表官方的(安全)
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 25
couchbase/centos7-systemd centos7-systemd images with additional debug… 5 [OK]
dokken/centos-7 CentOS 7 image for kitchen-dokken 3
continuumio/centos5_gcc5_base 3
dokken/centos-stream-8 2
dokken/centos-stream-9 2
spack/centos6 CentOS 6 with Spack preinstalled 1
dokken/centos-8 CentOS 8 image for kitchen-dokken 1
spack/centos7 CentOS 7 with Spack preinstalled 1
corpusops/centos-bare https://github.com/corpusops/docker-images/ 0
ustclug/centos Official CentOS Image with USTC Mirror 0
dokken/centos-6 CentOS 6 image for kitchen-dokken 0
datadog/centos-i386 0
bitnami/centos-extras-base 0
corpusops/centos centos corpusops baseimage 0
couchbase/centos-72-java-sdk 0
couchbase/centos-72-jenkins-core 0
couchbase/centos-70-sdk-build 0
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
couchbase/centos-69-sdk-nodevtoolset-build 0
couchbase/centos-69-sdk-build 0
fnndsc/centos-python3 Source for a slim Centos-based Python3 image… 0 [OK]
spack/centos-stream 0
dokken/centos-5 EOL DISTRO: For use with kitchen-dokken, Bas… 0
[root@k8s-master01 ~]# docker pull alpine:latest #本地已经存在的镜像,不会重复拉取
latest: Pulling from library/alpine
213ec9aee27d: Pull complete
Digest: sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
前台启动
[root@k8s-master01 ~]# docker run -it alpine sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
后台启动
[root@k8s-master01 ~]# docker run -d alpine
76312934bac41257649f788757b4e73599c77c60b32a2621b0582e67e9dacab1
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c11891450c4 8c811b4aec35 "sleep 3600" 48 minutes ago Up 48 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_17
a1a2112d5250 d45bf977dfbf "start_runit" 2 hours ago Up 2 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 2 hours ago Up 2 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
[root@k8s-master01 ~]# docker logs -n 10 a1a2112d5250 #查询运行容器日志
2022-10-20 03:34:01.894 [INFO][67] felix/wireguard.go 578: Wireguard is not enabled
2022-10-20 03:34:01.894 [INFO][67] felix/ipsets.go 306: Resyncing ipsets with dataplane. family="inet"
2022-10-20 03:34:01.898 [INFO][67] felix/ipsets.go 356: Finished resync family="inet" numInconsistenciesFound=0 resyncDuration=4.220363ms
2022-10-20 03:34:01.898 [INFO][67] felix/int_dataplane.go 1259: Finished applying updates to dataplane. msecToApply=4.497051
2022-10-20 03:34:12.273 [INFO][67] felix/int_dataplane.go 1245: Applying dataplane updates
2022-10-20 03:34:12.273 [INFO][67] felix/ipsets.go 223: Asked to resync with the dataplane on next update. family="inet"
2022-10-20 03:34:12.273 [INFO][67] felix/wireguard.go 578: Wireguard is not enabled
2022-10-20 03:34:12.273 [INFO][67] felix/ipsets.go 306: Resyncing ipsets with dataplane. family="inet"
2022-10-20 03:34:12.279 [INFO][67] felix/ipsets.go 356: Finished resync family="inet" numInconsistenciesFound=0 resyncDuration=5.082666ms
2022-10-20 03:34:12.279 [INFO][67] felix/int_dataplane.go 1259: Finished applying updates to dataplane. msecToApply=5.493877
[root@k8s-master01 ~]# docker ps 查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c11891450c4 8c811b4aec35 "sleep 3600" 52 minutes ago Up 52 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_17
a1a2112d5250 d45bf977dfbf "start_runit" 2 hours ago Up 2 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 2 hours ago Up 2 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
You have new mail in /var/spool/mail/root
[root@k8s-master01 ~]# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76312934bac4 alpine "/bin/sh" 10 minutes ago Exited (0) 10 minutes ago inspiring_volhard
05ac21718670 alpine "sh" 17 minutes ago Exited (0) 13 minutes ago gifted_mendeleev
6c11891450c4 8c811b4aec35 "sleep 3600" 53 minutes ago Up 53 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_17
a1a2112d5250 d45bf977dfbf "start_runit" 2 hours ago Up 2 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
caa1dc4a12ee 963564fb95ed "/usr/local/bin/flex…" 2 hours ago Exited (0) 2 hours ago k8s_flexvol-driver_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_0
2a4ba529bf29 8c811b4aec35 "sleep 3600" 2 hours ago Exited (0) 53 minutes ago k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_16
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 2 hours ago Up 2 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
fd143d419ff0 ca5564c06ea0 "/install-cni.sh" 2 hours ago Exited (0) 2 hours ago k8s_install-cni_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
86425103ed0b d45bf977dfbf "start_runit" 10 days ago Exited (255) 2 hours ago k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_3
624bbbd5c8da 0cb2976cbb7d "/usr/bin/kube-contr…" 10 days ago Exited (255) 2 hours ago k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_3
75ce0bb5021d registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 10 days ago Exited (255) 2 hours ago k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_3
48c02cc4a40f registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 10 days ago Exited (255) 2 hours ago k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_3
[root@k8s-master01 ~]# docker ps -q #查看正在运行容器的id
6c11891450c4
a1a2112d5250
2d7d4499bc61
690124fbd1b9
0cf34c08a2e0
18c887a865b0
[root@k8s-master01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c11891450c4 8c811b4aec35 "sleep 3600" 56 minutes ago Up 56 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_17
a1a2112d5250 d45bf977dfbf "start_runit" 2 hours ago Up 2 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 2 hours ago Up 2 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 2 hours ago Up 2 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
You have new mail in /var/spool/mail/root
[root@k8s-master01 ~]# docker exec -it 6c11891450c4 sh #进入正在运行的容器
/ # ls
bin dev etc home proc root sys tmp usr var
/ #
拷贝前访问结果
[root@k8s-master01 ~]# docker run -it -p 12345:80 nginx:1.14.2
[root@k8s-master01 ~]# docker run -it -p 12345:80 nginx:1.14.2
[root@k8s-master01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8dbe2fe0b5 nginx:1.14.2 "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 0.0.0.0:12345->80/tcp, :::12345->80/tcp competent_dhawan
78e4790621ad 8c811b4aec35 "sleep 3600" 30 minutes ago Up 30 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_20
a1a2112d5250 d45bf977dfbf "start_runit" 5 hours ago Up 5 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 5 hours ago Up 5 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
You have new mail in /var/spool/mail/root
[root@k8s-master01 ~]# docker exec -it db8dbe2fe0b5 bash
root@db8dbe2fe0b5:/# cd /usr/share/nginx/html
root@db8dbe2fe0b5:/usr/share/nginx/html# ls
50x.html index.html
root@db8dbe2fe0b5:/usr/share/nginx/html# exit
exit
[root@k8s-master01 ~]# echo 'test cp' > index.html
[root@k8s-master01 ~]# docker cp index.html db8dbe2fe0b5:/usr/share/nginx/html/
[root@k8s-master01 ~]# docker rm 45cea064aff9
45cea064aff9
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# docker stop db8dbe2fe0b5 #关闭容器
db8dbe2fe0b5
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# docker start db8dbe2fe0b5 #开启容器
db8dbe2fe0b5
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 2d389e545974 5 weeks ago 142MB
alpine latest 9c6f07244728 2 months ago 5.54MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
registry.cn-beijing.aliyuncs.com/dotbalo/kube-controllers v3.15.3 0cb2976cbb7d 2 years ago 52.9MB
nginx 1.14.2 295c7be07902 3 years ago 109MB
busybox 1.28 8c811b4aec35 4 years ago 1.15MB
[root@k8s-master01 ~]# docker history registry.cn-beijing.aliyuncs.com/dotbalo/node:v3.15.3
IMAGE CREATED CREATED BY SIZE COMMENT
d45bf977dfbf 2 years ago /bin/sh -c #(nop) CMD ["start_runit"] 0B
2 years ago /bin/sh -c #(nop) COPY file:8caa269b9a3556b6… 1.25MB
2 years ago /bin/sh -c #(nop) COPY file:042c32c76c7884d9… 62.3MB
2 years ago /bin/sh -c #(nop) COPY file:77605f5025704683… 11.3kB
2 years ago |4 ARCH=x86_64 GIT_VERSION=v3.15.3 IPTABLES_… 18B
2 years ago /bin/sh -c #(nop) COPY dir:8f92497206a310e7c… 3.44MB
2 years ago /bin/sh -c #(nop) COPY multi:d0822010c15fea2… 7.18MB
2 years ago |4 ARCH=x86_64 GIT_VERSION=v3.15.3 IPTABLES_… 0B
2 years ago |4 ARCH=x86_64 GIT_VERSION=v3.15.3 IPTABLES_… 76.8MB
2 years ago /bin/sh -c #(nop) COPY file:40f5c907e7d9d835… 410B
2 years ago /bin/sh -c #(nop) COPY multi:e6dd0b0d46b5dc7… 3.27MB
2 years ago /bin/sh -c #(nop) COPY multi:304fbb705bde0d7… 2.03MB
2 years ago /bin/sh -c #(nop) LABEL name=Calico node ve… 0B
2 years ago /bin/sh -c #(nop) ARG RUNIT_VER 0B
2 years ago /bin/sh -c #(nop) ARG IPTABLES_VER 0B
2 years ago /bin/sh -c #(nop) ARG GIT_VERSION 0B
2 years ago /bin/sh -c #(nop) ARG ARCH 0B
2 years ago 4.23kB
2 years ago 106MB Imported from -
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55fbf44cd733 8c811b4aec35 "sleep 3600" 15 minutes ago Up 15 minutes k8s_busybox_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_21
db8dbe2fe0b5 nginx:1.14.2 "nginx -g 'daemon of…" 52 minutes ago Up 20 minutes 0.0.0.0:12345->80/tcp, :::12345->80/tcp competent_dhawan
a1a2112d5250 d45bf977dfbf "start_runit" 5 hours ago Up 5 hours k8s_calico-node_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
2d7d4499bc61 0cb2976cbb7d "/usr/bin/kube-contr…" 5 hours ago Up 5 hours k8s_calico-kube-controllers_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
690124fbd1b9 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_busybox_default_6948af5c-4613-4ce3-b620-18cb2de58ab9_4
0cf34c08a2e0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_calico-node-tmxtg_kube-system_8d206d69-1c6b-44ae-997d-c82f9f95f5c6_4
18c887a865b0 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 "/pause" 5 hours ago Up 5 hours k8s_POD_calico-kube-controllers-cdd5755b9-qztxb_kube-system_408f0bc0-8350-4546-9566-b4fea3854bae_4
[root@k8s-master01 ~]# docker commit -a "mhw" -m "add index.html" db8dbe2fe0b5 nginx:commit
sha256:29193419358d0311b919f731e69b08f301f968e5cd4884d9d7c260fe2523bc66
[root@k8s-master01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx commit 29193419358d 14 seconds ago 109MB
nginx latest 2d389e545974 5 weeks ago 142MB
alpine latest 9c6f07244728 2 months ago 5.54MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
registry.cn-beijing.aliyuncs.com/dotbalo/kube-controllers v3.15.3 0cb2976cbb7d 2 years ago 52.9MB
nginx 1.14.2 295c7be07902 3 years ago 109MB
busybox
[root@k8s-master01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14.2 295c7be07902 3 years ago 109MB
nginx 1.15.3 06144b287844 4 years ago 109MB
nginx 1.15.2 c82521676580 4 years ago 109MB
busybox 1.28 8c811b4aec35 4 years ago 1.15MB
[root@k8s-master01 ~]# ls
anaconda-ks.cfg index.html kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm pod
dockerfiles k8s-ha-install kubernetes-server-linux-amd64.tar.gz wlnmp-release-centos.noarch.rpm
etcd-v3.4.13-linux-amd64.tar.gz kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm nginx-sts.yaml
[root@k8s-master01 ~]# docker save busybox:1.28 -o busybox.tar # 打包容器为镜像文件
You have new mail in /var/spool/mail/root
[root@k8s-master01 ~]# ls
anaconda-ks.cfg dockerfiles index.html kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm kubernetes-server-linux-amd64.tar.gz pod
busybox.tar etcd-v3.4.13-linux-amd64.tar.gz k8s-ha-install kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm nginx-sts.yaml wlnmp-release-centos.noarch.rpm
[root@k8s-master01 ~]# scp busybox.tar [email protected]:/root/
busybox.tar 100% 1338KB 19.8MB/s 00:00
[root@k8s-master01 ~]#
==================================================================================
[root@k8s-master02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/metrics-server v0.4.1 9759a41ccdf0 23 months ago 60.5MB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
nginx 1.15.2 c82521676580 4 years ago 109MB
[root@k8s-master02 ~]# ls
anaconda-ks.cfg busybox.tar kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm wlnmp-release-centos.noarch.rpm
[root@k8s-master02 ~]# docker load -i busybox.tar # 加载本地文件为镜像
432b65032b94: Loading layer [==================================================>] 1.36MB/1.36MB
Loaded image: busybox:1.28
[root@k8s-master02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/metrics-server v0.4.1 9759a41ccdf0 23 months ago 60.5MB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
nginx 1.15.2 c82521676580 4 years ago 109MB
busybox 1.28 8c811b4aec35 4 years ago 1.15MB
[root@k8s-master02 ~]#
FROM:继承基础镜像
MAINTAINER:镜像制作作者信息
RUN:用来执行shell命令
EXPOSE:暴露端口号
CMD:启动容器默认执行的命令
ENTRYPOINT:启动容器真正执行的命令
VOLUME:创建挂载点
ENV:配置环境变量
ADD:复制文件到容器
COPY:复制文件到容器
WORKDIR:设置容器的工作目录
USER:容器使用的用户
小试牛刀
[root@k8s-master01 dockerfiles]# vim Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh #测试状态分开写,测试通过后,合并在一起 RUN useradd mh && mkdir /opt/mh
RUN mkdir /opt/mh
==========================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:user . #在当前目录创建镜像
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos:latest
---> 5d0da3dc9764
Step 2/4 : LABEL maintainer="test dockerfile"
---> Running in f7586e5b1d5b
Removing intermediate container f7586e5b1d5b
---> 903f978887ee
Step 3/4 : RUN useradd mh
---> Running in 73ed6770d2bb
Removing intermediate container 73ed6770d2bb
---> 9e24dd0f917c
Step 4/4 : RUN mkdir /opt/mh
---> Running in 8c0d98bd6fb8
Removing intermediate container 8c0d98bd6fb8
---> 50ee045b70a0
Successfully built 50ee045b70a0
Successfully tagged centos:user
==========================================================================
[root@k8s-master01 dockerfiles]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos user 50ee045b70a0 58 seconds ago 232MB
nginx commit 29193419358d 2 hours ago 109MB
nginx latest 2d389e545974 5 weeks ago 142MB
alpine latest 9c6f07244728 2 months ago 5.54MB
centos latest 5d0da3dc9764 13 months ago 231MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
registry.cn-beijing.aliyuncs.com/dotbalo/kube-controllers v3.15.3 0cb2976cbb7d 2 years ago 52.9MB
nginx 1.14.2 295c7be07902 3 years ago 109MB
busybox 1.28 8c811b4aec35 4 years ago 1.15MB
==========================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:user bash
#验证:
[root@425fc2888fcf /]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
mh:x:1000:1000::/home/mh:/bin/bash
[root@425fc2888fcf /]# cd /opt/mh
[root@425fc2888fcf mh]# pwd
/opt/mh
[root@425fc2888fcf mh]#
小试牛刀2
[root@k8s-master01 ~]# cat dockerfiles/t/Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh && mkdir /opt/mh
CMD ["sh","-c","echo 1"]
====================================================================
[root@k8s-master01 dockerfiles]# docker build -f t/Dockerfile -t centos:cmd .
Sending build context to Docker daemon 3.584kB
Step 1/4 : FROM centos:latest
---> 5d0da3dc9764
Step 2/4 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/4 : RUN useradd mh && mkdir /opt/mh
---> Running in a5894d218b00
Removing intermediate container a5894d218b00
---> 2be0928dd7fe
Step 4/4 : CMD ["sh","-c","echo 1"]
---> Running in f16fcdbbd5bd
Removing intermediate container f16fcdbbd5bd
---> f7d0d25044f6
Successfully built f7d0d25044f6
Successfully tagged centos:cmd
====================================================================
[root@k8s-master01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos cmd f7d0d25044f6 2 minutes ago 232MB
centos user 50ee045b70a0 16 hours ago 232MB
nginx commit 29193419358d 18 hours ago 109MB
nginx latest 2d389e545974 5 weeks ago 142MB
alpine latest 9c6f07244728 2 months ago 5.54MB
centos latest 5d0da3dc9764 13 months ago 231MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 21 months ago 683kB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.15.3 d45bf977dfbf 2 years ago 262MB
registry.cn-beijing.aliyuncs.com/dotbalo/pod2daemon-flexvol v3.15.3 963564fb95ed 2 years ago 22.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.15.3 ca5564c06ea0 2 years ago 110MB
registry.cn-beijing.aliyuncs.com/dotbalo/kube-controllers v3.15.3 0cb2976cbb7d 2 years ago 52.9MB
nginx 1.14.2 295c7be07902 3 years ago 109MB
busybox 1.28 8c811b4aec35 4 years ago 1.15MB
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:cmd
1
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:cmd bash #bash覆盖了cmd命令,不打印
[root@aaff36f90e41 /]#
小试牛刀3
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
ENTRYPOINT ["echo"]
CMD ["3"]
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:ep .
Sending build context to Docker daemon 3.584kB
Step 1/6 : FROM centos:latest
---> 5d0da3dc9764
Step 2/6 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/6 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/6 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/6 : ENTRYPOINT ["echo"]
---> Running in 06f98e59dc81
Removing intermediate container 06f98e59dc81
---> 6ffed0ad9dd1
Step 6/6 : CMD ["3"]
---> Running in c82dd89c478c
Removing intermediate container c82dd89c478c
---> 65a4b12a6672
Successfully built 65a4b12a6672
Successfully tagged centos:ep
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:ep
3
[root@k8s-master01 dockerfiles]#
#注:cmd的值会作为entrypoint的参数使用
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:ep mh
mh
[root@k8s-master01 dockerfiles]#
#注:mh会覆盖cmd的值,并成为entrypoint的参数
小试牛刀之环境变量
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
ENV env1=test1 env2=test2
CMD echo "$env1 $env2"
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:env .
Sending build context to Docker daemon 3.584kB
Step 1/6 : FROM centos:latest
---> 5d0da3dc9764
Step 2/6 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/6 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/6 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/6 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/6 : CMD echo "$env1 $env2"
---> Running in 4f3ca8af153f
Removing intermediate container 4f3ca8af153f
---> 81d562d8b902
Successfully built 81d562d8b902
Successfully tagged centos:env
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:env
test1 test2
[root@k8s-master01 dockerfiles]#
小试牛刀之ADD
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
#ENTRYPOINT ["echo"]
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/ #docker build不会使用绝对路径,都使用相对路径
CMD echo "$env1 $env2"
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:add .
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM centos:latest
---> 5d0da3dc9764
Step 2/7 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/7 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/7 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/7 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/7 : ADD ./index.tar.gz /opt/
---> f4be759ab542
Step 7/7 : CMD echo "$env1 $env2"
---> Running in 3486622d808d
Removing intermediate container 3486622d808d
---> b61c48825832
Successfully built b61c48825832
Successfully tagged centos:add
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:add bash
[root@ec8ba68a2ade /]# cd /opt
[root@ec8ba68a2ade opt]# ls #压缩文件已经自动解压
Dockerfile index.html mh t
[root@ec8ba68a2ade opt]#
小试牛刀之COPY
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
#ENTRYPOINT ["echo"]
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/
COPY ./t /opt/mh/
CMD echo "$env1 $env2"
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:cp .
Sending build context to Docker daemon 5.632kB
Step 1/8 : FROM centos:latest
---> 5d0da3dc9764
Step 2/8 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/8 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/8 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/8 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/8 : ADD ./index.tar.gz /opt/
---> Using cache
---> f4be759ab542
Step 7/8 : COPY ./t /opt/mh/
---> 5302939d0e98
Step 8/8 : CMD echo "$env1 $env2"
---> Running in ad9c5393588d
Removing intermediate container ad9c5393588d
---> b50b878836a4
Successfully built b50b878836a4
Successfully tagged centos:cp
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:cp bash
[root@b20c240b525e /]# cd /opt
[root@b20c240b525e opt]# ls
Dockerfile index.html mh t
[root@b20c240b525e opt]# cd mh
[root@b20c240b525e mh]# ls
Dockerfile
[root@b20c240b525e mh]#
#注:只会拷贝t目录下的文件,不会拷贝t目录;拷贝压缩包不会自动解压
小试牛刀之工作目录
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/
COPY ./t /opt/mh/
WORKDIR /opt/mh
CMD pwd ; ls
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:workdir .
Sending build context to Docker daemon 5.632kB
Step 1/9 : FROM centos:latest
---> 5d0da3dc9764
Step 2/9 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/9 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/9 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/9 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/9 : ADD ./index.tar.gz /opt/
---> Using cache
---> f4be759ab542
Step 7/9 : COPY ./t /opt/mh/
---> Using cache
---> 5302939d0e98
Step 8/9 : WORKDIR /opt/mh
---> Running in 99898ecace02
Removing intermediate container 99898ecace02
---> 18ca388462c2
Step 9/9 : CMD pwd ; ls
---> Running in c7a20679f804
Removing intermediate container c7a20679f804
---> d9617da57a9e
Successfully built d9617da57a9e
Successfully tagged centos:workdir
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:workdir
/opt/mh
Dockerfile
[root@k8s-master01 dockerfiles]#
小试牛刀之容器启动用户
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/
COPY ./t /opt/mh/
WORKDIR /opt/mh
USER 1000
CMD pwd ; ls
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:user .
Sending build context to Docker daemon 5.632kB
Step 1/10 : FROM centos:latest
---> 5d0da3dc9764
Step 2/10 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/10 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/10 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/10 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/10 : ADD ./index.tar.gz /opt/
---> Using cache
---> f4be759ab542
Step 7/10 : COPY ./t /opt/mh/
---> Using cache
---> 5302939d0e98
Step 8/10 : WORKDIR /opt/mh
---> Using cache
---> 18ca388462c2
Step 9/10 : USER 1000
---> Running in be7d22aa15ed
Removing intermediate container be7d22aa15ed
---> 1af22bdfab73
Step 10/10 : CMD pwd ; ls
---> Running in dea7ab5d33d2
Removing intermediate container dea7ab5d33d2
---> 7513d5afb182
Successfully built 7513d5afb182
Successfully tagged centos:user
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:user bash
[mh@e48a34009848 mh]$ whoami
mh
[mh@e48a34009848 mh]$
小试牛刀之VOLUME
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM centos:latest
LABEL maintainer="test dockerfile"
RUN useradd mh
RUN mkdir /opt/mh
#ENTRYPOINT ["echo"]
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/
COPY ./t /opt/mh/
WORKDIR /opt/mh
USER 1000
VOLUME data #创建挂载点
CMD pwd ; ls
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t centos:vm .
Sending build context to Docker daemon 5.632kB
Step 1/11 : FROM centos:latest
---> 5d0da3dc9764
Step 2/11 : LABEL maintainer="test dockerfile"
---> Using cache
---> 903f978887ee
Step 3/11 : RUN useradd mh
---> Using cache
---> 9e24dd0f917c
Step 4/11 : RUN mkdir /opt/mh
---> Using cache
---> 50ee045b70a0
Step 5/11 : ENV env1=test1 env2=test2
---> Using cache
---> ad14e48b6966
Step 6/11 : ADD ./index.tar.gz /opt/
---> Using cache
---> f4be759ab542
Step 7/11 : COPY ./t /opt/mh/
---> Using cache
---> 5302939d0e98
Step 8/11 : WORKDIR /opt/mh
---> Using cache
---> 18ca388462c2
Step 9/11 : USER 1000
---> Using cache
---> 1af22bdfab73
Step 10/11 : VOLUME data
---> Running in 8a642c5c707e
Removing intermediate container 8a642c5c707e
---> dc4c5c23acce
Step 11/11 : CMD pwd ; ls
---> Running in 0b78bffef77f
Removing intermediate container 0b78bffef77f
---> 88a3fec69e95
Successfully built 88a3fec69e95
Successfully tagged centos:vm
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm centos:vm bash
[mh@4f8f60461d70 mh]$ ls
Dockerfile
[mh@4f8f60461d70 mh]$ cd /
[mh@4f8f60461d70 /]$ ls #容器挂载点data已创建
bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#注:没有使用-v参数也是可以启动的,他会在/var/lib/docker/volumes创建目录
[root@2852bbd93629 data]# touch 1
[root@2852bbd93629 data]# ls
1
#注:在容器中创建文件1
[root@k8s-master01 /]# cd /var/lib/docker/volumes
[root@k8s-master01 volumes]# ls
backingFsBlockDev d11d295f34727121e7b0c87ac432406e5c27dc1e9ee139d30a8d3780a4bd0c27 metadata.db
[root@k8s-master01 volumes]# cd d11d295f34727121e7b0c87ac432406e5c27dc1e9ee139d30a8d3780a4bd0c27
[root@k8s-master01 d11d295f34727121e7b0c87ac432406e5c27dc1e9ee139d30a8d3780a4bd0c27]# ls
_data
[root@k8s-master01 d11d295f34727121e7b0c87ac432406e5c27dc1e9ee139d30a8d3780a4bd0c27]# cd _data/
[root@k8s-master01 _data]# ls
1
#注:物理机中可以看到这个1的文件,当容器关闭后,物理机中的文件也会消失
====================================================================
#注:使用-v挂载
[root@k8s-master01 dockerfiles]# docker run -it -v /mysql_data/:/data --rm centos:vm bash
[root@35cfac01bd7c mh]#
[root@35cfac01bd7c data]# touch test #容器中创建test文件
[root@35cfac01bd7c data]# ls
test
# /var/lib/docker/volumes/e6305b40109561f77188b6cddb993d7cd15043786f3f99d2aae6186a8654ea4f/_data中的数据为空
[root@k8s-master01 ~]# cd /mysql_data/
[root@k8s-master01 mysql_data]# ls
test
[root@k8s-master01 mysql_data]#
#注:指定的挂载点目录下产生文件test,且容器退出后mysql_data目录下的文件不会被清空
不使用centos作为镜像
可以使用:
1、Alpine docker官方建议使用的镜像(首选)
2、busybox
3、scratch 空镜像
4、Debian
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM alpine:latest
LABEL maintainer="test dockerfile"
RUN adduser mh
RUN mkdir -p /opt/mh
ENV env1=test1 env2=test2
ADD ./index.tar.gz /opt/
COPY ./t /opt/mh/
WORKDIR /opt/mh
CMD pwd ; ls
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t alpine:workdir .
Sending build context to Docker daemon 5.632kB
Step 1/9 : FROM alpine:latest
---> 9c6f07244728
Step 2/9 : LABEL maintainer="test dockerfile"
---> Using cache
---> 877cd0ccdc14
Step 3/9 : RUN adduser -D mh
---> Running in dd31b456257a
Removing intermediate container dd31b456257a
---> 60e0ff720338
Step 4/9 : RUN mkdir -p /opt/mh
---> Running in 94ad5a3dbaa6
Removing intermediate container 94ad5a3dbaa6
---> 9dc76c04e6ea
Step 5/9 : ENV env1=test1 env2=test2
---> Running in 2372ca54c529
Removing intermediate container 2372ca54c529
---> 5a2af210062f
Step 6/9 : ADD ./index.tar.gz /opt/
---> 4bb7307e5450
Step 7/9 : COPY ./t /opt/mh/
---> 9c5a676a514d
Step 8/9 : WORKDIR /opt/mh
---> Running in 5a9b0ab5e52c
Removing intermediate container 5a9b0ab5e52c
---> 4880ec6dce5e
Step 9/9 : CMD pwd ; ls
---> Running in d63a1e049ce8
Removing intermediate container d63a1e049ce8
---> c68cc1193a50
Successfully built c68cc1193a50
Successfully tagged alpine:workdir
====================================================================
[root@k8s-master01 _data]# docker images | grep 'workdir'
alpine workdir c68cc1193a50 57 seconds ago 5.55MB
centos workdir d9617da57a9e 4 hours ago 232MB
[root@k8s-master01 _data]#
#注:实现相同的功能,大小差几十倍
使用到Glibc:
可以选用 node:slim python:slim
编译操作和生成最终镜像的操作
[root@k8s-master01 dockerfiles]# cat main.go #编译的go语言代码
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
[root@k8s-master01 dockerfiles]#
====================================================================
[root@k8s-master01 dockerfiles]# cat Dockerfile
FROM golang:1.14.4-alpine
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
[root@k8s-master01 dockerfiles]#
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t golang:hello .
Sending build context to Docker daemon 6.656kB
Step 1/5 : FROM golang:1.14.4-alpine
---> 3289bf11c284
Step 2/5 : WORKDIR /opt
---> Running in a5fb3a3792a0
Removing intermediate container a5fb3a3792a0
---> 2b96e57dc3d4
Step 3/5 : COPY main.go /opt
---> fbed98b38304
Step 4/5 : RUN go build /opt/main.go
---> Running in ce9bedff9187
Removing intermediate container ce9bedff9187
---> 68f816d7e6f2
Step 5/5 : CMD "./main"
---> Running in 3cc1d2f1de48
Removing intermediate container 3cc1d2f1de48
---> a48d740cc4bb
Successfully built a48d740cc4bb
Successfully tagged golang:hello
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm golang:hello
Hello, World!
[root@k8s-master01 dockerfiles]#
[root@k8s-master01 dockerfiles]# docker images #构建出来的镜像比较大
REPOSITORY TAG IMAGE ID CREATED SIZE
golang hello a48d740cc4bb 3 minutes ago 372MB
多层构建
# build step
FROM golang:1.14.4-alpine
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
# create real app image
FROM alpine:latest
COPY --from=0 /opt/main / # from=0 是指上述构建的第一个步骤
CMD "./main"
====================================================================
[root@k8s-master01 dockerfiles]# docker build -t golang:mul .
Sending build context to Docker daemon 6.656kB
Step 1/8 : FROM golang:1.14.4-alpine
---> 3289bf11c284
Step 2/8 : WORKDIR /opt
---> Using cache
---> 2b96e57dc3d4
Step 3/8 : COPY main.go /opt
---> Using cache
---> fbed98b38304
Step 4/8 : RUN go build /opt/main.go
---> Using cache
---> 68f816d7e6f2
Step 5/8 : CMD "./main"
---> Using cache
---> a48d740cc4bb
Step 6/8 : FROM alpine:latest
---> 9c6f07244728
Step 7/8 : COPY --from=0 /opt/main /
---> 35bbdac826d8
Step 8/8 : CMD "./main"
---> Running in 1a090bfa8512
Removing intermediate container 1a090bfa8512
---> f40ee1ffdf8c
Successfully built f40ee1ffdf8c
Successfully tagged golang:mul
====================================================================
[root@k8s-master01 dockerfiles]# docker run -it --rm golang:mul
Hello, World!
[root@k8s-master01 dockerfiles]#
[root@k8s-master01 dockerfiles]# docker images #多层构建的镜像(实现相同的功能)会小很多
REPOSITORY TAG IMAGE ID CREATED SIZE
golang mul f40ee1ffdf8c About a minute ago 7.61MB
golang hello a48d740cc4bb 15 minutes ago 372MB
多成构建还可以使用as
# build step
FROM golang:1.14.4-alpine as tp1
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
# create real app image
FROM alpine:latest
COPY --from=tp1 /opt/main / # from=0 是指上述构建的第一个步骤
CMD "./main"