1.什么是docker
解决了什么难点
- 在开发中,经常会遇到本地环境与生产环境不一致,然后开发和运维言语沟通或者武力沟通的情况,相互甩锅,程序员最常说的一句话就是:“为什么在我的电脑上没事”
- 在需要搭建集群的情况时,如果需要在特定的时区快速扩容机器(机器数量庞大,并非人力可为的情况),并且搭建好环境,事后又能快速的关掉
解决有哪些方案
- 可以编写脚本,进行自动部署,自动扩容;
- 但这仍然会遇到不可预期的情况,当编写好自动部署脚本,是为centos的linux服务器,但如果实际情况下服务器是ubuntu的linux, 则有可能部署不成功
- 其次就是假如我的java项目需要部署到服务器上,我的java项目只能支持java8,如果服务器上已有java11,则无从下手, 卸载重装则有可能会影响到服务器上的别的程序,如果不装java8,则无法运行我们的程序,隔离性不强
- 构建虚拟机镜像文件,在虚拟机里运行创建出虚拟机
- 他解决了隔离性和一定地跨平台性,但是一台虚拟机创建出来需要模拟整个操作系统,包括硬盘,内存,网络等一系列的操作系统相关的组件 会很浪费资源
- 虚拟容器技术
- 他解决了隔离性和跨平台性,他并不像虚拟机一样庞大,硬件驱动相关的与主机共用(主机:正真的电脑,宿机:虚拟出来的),只是环境相关 的为容器内部所管理,容器和容器之间互不影响,docker是这种技术的一种实现,他可以让开发者配置好dockerfile,构建出镜像或者直接推至仓库 , 运维拿到镜像则直接使用少量参数则器启动
docker的三要素
- 仓库,类似于git服务器、maven中央仓库、百度云盘(不严谨,但好理解)、等,仓库用于存放镜像,我们可以拉取镜像,用来快速创建容器,docker官方的仓库地址
- 镜像,用镜像可以创建出一个或者多个容器,可以认为是一个模板,类似java的类,用类可以创建出对象,比如windows10的安装文件,利用这一个文件可以给无数台电脑安装windows
- 容器,利用镜像创建出来的真实运行环境,他是一个虚拟出的一个小型的linux,也是最终运行应用程序的东西
2.docker常用命令
docker 系统相关
docker info
docker info 可以查看docker的详细信息,包括镜像数,容器数,主机硬件信息等
[root@izwz9g1c3fleilt56ermd5z docker]# docker info
containers: 8
running: 6
paused: 0
stopped: 2
images: 8
server version: 1.13.1
storage driver: overlay2
backing filesystem: extfs
supports d_type: true
native overlay diff: true
logging driver: journald
cgroup driver: systemd
plugins:
volume: local
network: bridge host macvlan null overlay
swarm: inactive
runtimes: docker-runc runc
default runtime: docker-runc
init binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 66aedde759f33c190954815fb765eedc1d782dd9 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
security options:
seccomp
warning: you're not using the default seccomp profile
profile: /etc/docker/seccomp.json
kernel version: 3.10.0-1127.19.1.el7.x86_64
operating system: centos linux 7 (core)
ostype: linux
architecture: x86_64
number of docker hooks: 3
cpus: 2
total memory: 3.561 gib
name: izwz9g1c3fleilt56ermd5z
id: ljdy:tl4j:a5ql:nya7:r25t:fru2:bnvk:anci:gc6f:ozq5:c4in:jak5
docker root dir: /var/lib/docker
debug mode (client): false
debug mode (server): false
registry: https://index.docker.io/v1/
experimental: false
insecure registries:
127.0.0.0/8
registry mirrors:
https://n81gglan.mirror.aliyuncs.com
live restore enabled: false
registries: docker.io (secure)
docker version
docker version 可以查看docker服务端和客户端版本信息
[root@izwz9g1c3fleilt56ermd5z docker]# docker version
client:
version: 1.13.1
api version: 1.26
package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
go version: go1.10.3
git commit: 0be3e21/1.13.1
built: thu nov 12 15:11:46 2020
os/arch: linux/amd64
server:
version: 1.13.1
api version: 1.26 (minimum version 1.12)
package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
go version: go1.10.3
git commit: 0be3e21/1.13.1
built: thu nov 12 15:11:46 2020
os/arch: linux/amd64
experimental: false
docker 镜像相关
docker images
docker images 可以查看本地的镜像
[root@izwz9g1c3fleilt56ermd5z docker]# docker images
repository tag image id created size
docker.io/wordpress latest cfb931188dab 2 months ago 546 mb
docker.io/redis latest 74d107221092 2 months ago 104 mb
docker.io/mysql 5.7.32 1b12f2e9257b 3 months ago 448 mb
docker.io/elasticsearch 7.9.3 1ab13f928dc8 3 months ago 742 mb
docker.io/nacos/nacos-server latest a81222848024 3 months ago 921 mb
docker.io/hello-world latest bf756fb1ae65 12 months ago 13.3 kb
docker.io/kibana 7.4.2 230d3ded1abc 15 months ago 1.1 gb
docker.io/nginx 1.10 0346349a1a64 3 years ago 182 mb
展示出来的表格参数详解
preository | tag | image id | created | size |
---|---|---|---|---|
表示镜像的仓库源 | 镜像的标签 | 镜像id(由docker产生) | 镜像创建的时间 | 镜像大小 |
同一个镜像可以有多个tag,可以认为是镜像的版本,最新版本为latest,如果不指定版本标签,默认为latest docker images 还有许多参数
-a: 用于查看全部的镜像对应单词 all
[root@izwz9g1c3fleilt56ermd5z docker]# docker images -a
repository tag image id created size
docker.io/wordpress latest cfb931188dab 2 months ago 546 mb
docker.io/redis latest 74d107221092 2 months ago 104 mb
docker.io/mysql 5.7.32 1b12f2e9257b 3 months ago 448 mb
docker.io/elasticsearch 7.9.3 1ab13f928dc8 3 months ago 742 mb
docker.io/nacos/nacos-server latest a81222848024 3 months ago 921 mb
docker.io/hello-world latest bf756fb1ae65 12 months ago 13.3 kb
docker.io/kibana 7.4.2 230d3ded1abc 15 months ago 1.1 gb
docker.io/nginx 1.10 0346349a1a64 3 years ago 182 mb
-q/-qa: -q是只查看image id的结果,-qa则是-a和-q的组合
[root@izwz9g1c3fleilt56ermd5z docker]# docker images -q
cfb931188dab
74d107221092
1b12f2e9257b
1ab13f928dc8
a81222848024
bf756fb1ae65
230d3ded1abc
0346349a1a64
--digests:可以查看镜像的说明,使用命令后,展示的表格会多出一个digest列
[root@izwz9g1c3fleilt56ermd5z docker]# docker images --digests
repository tag digest image id created size
docker.io/wordpress latest sha256:92e97d9b3147038e3cc541a224cc951bef597061827e23a208a24c36bff1c1fe cfb931188dab 2 months ago 546 mb
docker.io/redis latest sha256:5b98e32b58cdbf9f6b6f77072c4915d5ebec43912114031f37fa5fa25b032489 74d107221092 2 months ago 104 mb
docker.io/mysql 5.7.32 sha256:4d2b34e99c14edb99cdd95ddad4d9aa7ea3f2c4405ff0c3509a29dc40bcb10ef 1b12f2e9257b 3 months ago 448 mb
docker.io/elasticsearch 7.9.3 sha256:a13cd87cbf139fadbca64972ef2c8777222236887d303e4177c1ab7cff1b52f6 1ab13f928dc8 3 months ago 742 mb
docker.io/nacos/nacos-server latest sha256:c2f6fee129ed53381ab6973313f77fe9438b6cb52df23ac026903d9592b81ce1 a81222848024 3 months ago 921 mb
docker.io/hello-world latest sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d bf756fb1ae65 12 months ago 13.3 kb
docker.io/kibana 7.4.2 sha256:355f9c979dc9cdac3ff9a75a817b8b7660575e492bf7dbe796e705168f167efc 230d3ded1abc 15 months ago 1.1 gb
docker.io/nginx 1.10 sha256:6202beb06ea61f44179e02ca965e8e13b961d12640101fca213efbfd145d7575 0346349a1a64 3 years ago 182 mb
--no-trunc:可以查看镜像id更加详细的信息
[root@izwz9g1c3fleilt56ermd5z ~]# docker images --no-trunc
repository tag image id created size
docker.io/wordpress latest sha256:cfb931188dab87aa1cb9da7b94068fecdb5d1f7e1372a381a7555b2acf3476f9 2 months ago 546 mb
docker.io/redis latest sha256:74d107221092875724ddb06821416295773bee553bbaf8d888ababe9be7b947f 2 months ago 104 mb
docker.io/mysql 5.7.32 sha256:1b12f2e9257be96da5c075f186dc371b18442b0f9281728ac64c9a69c6e0e264 3 months ago 448 mb
docker.io/elasticsearch 7.9.3 sha256:1ab13f928dc8aa958574d060fde595bd7715c1c2d3260446356a6a02d231e168 3 months ago 742 mb
docker.io/nacos/nacos-server latest sha256:a81222848024f406b6bef585540847bcfea1a06ec97d64e954835ee7411c53f3 3 months ago 921 mb
docker.io/hello-world latest sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b 12 months ago 13.3 kb
docker.io/kibana 7.4.2 sha256:230d3ded1abc1468536e41d80a9cc6a67908358c0e4ebf065c29b8ef0370ba4b 15 months ago 1.1 gb
docker.io/nginx 1.10 sha256:0346349a1a640da9535acfc0f68be9d9b81e85957725ecb76f3b522f4e2f0455 3 years ago 182 mb
docker search
docker search 镜像名字, 可以去中央仓库搜索镜像,对应的网站地址docker镜像仓库
[root@izwz9g1c3fleilt56ermd5z ~]# docker search tomcat
index name description stars official automated
docker.io docker.io/tomcat apache tomcat is an open source implementa... 2919 [ok]
docker.io docker.io/tomee apache tomee is an all-apache java ee cert... 86 [ok]
docker.io docker.io/dordoka/tomcat ubuntu 14.04, oracle jdk 8 and tomcat 8 ba... 55 [ok]
docker.io docker.io/bitnami/tomcat bitnami tomcat docker image 37 [ok]
docker.io docker.io/kubeguide/tomcat-app tomcat image for chapter 1 29
docker.io docker.io/consol/tomcat-7.0 tomcat 7.0.57, 8080, "admin/admin" 17 [ok]
docker.io docker.io/cloudesire/tomcat tomcat server, 6/7/8 15 [ok]
docker.io docker.io/aallam/tomcat-mysql debian, oracle jdk, tomcat & mysql 13 [ok]
docker.io docker.io/arm32v7/tomcat apache tomcat is an open source implementa... 10
docker.io docker.io/maluuba/tomcat7-java8 tomcat7 with java8. 6
docker.io docker.io/rightctrl/tomcat centos , oracle java, tomcat application s... 6 [ok]
docker.io docker.io/unidata/tomcat-docker security-hardened tomcat docker container. 5 [ok]
docker.io docker.io/arm64v8/tomcat apache tomcat is an open source implementa... 3
docker.io docker.io/amd64/tomcat apache tomcat is an open source implementa... 2
docker.io docker.io/cfje/tomcat-resource tomcat concourse resource 2
docker.io docker.io/jelastic/tomcat an image of the tomcat java application se... 2
docker.io docker.io/99taxis/tomcat7 tomcat7 1 [ok]
docker.io docker.io/camptocamp/tomcat-logback docker image for tomcat with logback integ... 1 [ok]
docker.io docker.io/chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [ok]
docker.io docker.io/oobsri/tomcat8 testing ci jobs with different names. 1
docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and manager_user / manag... 1 [ok]
docker.io docker.io/ppc64le/tomcat apache tomcat is an open source implementa... 1
docker.io docker.io/s390x/tomcat apache tomcat is an open source implementa... 0
docker.io docker.io/secoresearch/tomcat-varnish tomcat and varnish 5.0 0 [ok]
docker.io docker.io/softwareplant/tomcat tomcat images for jira-cloud testing 0 [ok]
参数说明
-s 指定点赞数,只展示超过指定点赞数的数据
[root@izwz9g1c3fleilt56ermd5z ~]# docker search -s 30 tomcat
flag --stars has been deprecated, use --filter=stars=3 instead
index name description stars official automated
docker.io docker.io/tomcat apache tomcat is an open source implementa... 2919 [ok]
docker.io docker.io/tomee apache tomee is an all-apache java ee cert... 86 [ok]
docker.io docker.io/dordoka/tomcat ubuntu 14.04, oracle jdk 8 and tomcat 8 ba... 55 [ok]
docker.io docker.io/bitnami/tomcat bitnami tomcat docker image 37 [ok]
--no-trunc 不省略,当字符超过展示数时省略的三个...功能关掉
[root@izwz9g1c3fleilt56ermd5z ~]# docker search -s 30 --no-trunc tomcat
flag --stars has been deprecated, use --filter=stars=3 instead
index name description stars official automated
docker.io docker.io/tomcat apache tomcat is an open source implementation of the java servlet and javaserver pages technologies 2919 [ok]
docker.io docker.io/tomee apache tomee is an all-apache java ee certified stack where apache tomcat is top dog. 86 [ok]
docker.io docker.io/dordoka/tomcat ubuntu 14.04, oracle jdk 8 and tomcat 8 based docker container. 55 [ok]
docker.io docker.io/bitnami/tomcat bitnami tomcat docker image 37 [ok]
--automated 只列出automated build为ok的镜像
[root@izwz9g1c3fleilt56ermd5z ~]# docker search -s 30 --no-trunc --automated tomcat
flag --stars has been deprecated, use --filter=stars=3 instead
flag --automated has been deprecated, use --filter=automated=true instead
index name description stars official automated
docker.io docker.io/dordoka/tomcat ubuntu 14.04, oracle jdk 8 and tomcat 8 based docker container. 55 [ok]
docker.io docker.io/bitnami/tomcat bitnami tomcat docker image 37 [ok]
docker pull
docker pull 镜像名[:tag] 从中央仓库拉取镜像,如果不写tag,等价于docker pull 镜像名:latest
[root@izwz9g1c3fleilt56ermd5z ~]# docker pull tomcat
using default tag: latest
trying to pull repository docker.io/library/tomcat ...
latest: pulling from docker.io/library/tomcat
b9a857cbf04d: pull complete
d557ee20540b: pull complete
3b9ca4f00c2e: pull complete
667fd949ed93: pull complete
661d3b55f657: pull complete
511ef4338a0b: pull complete
a56db448fefe: pull complete
00612a99c7dc: pull complete
326f9601c512: pull complete
c547db74f1e1: pull complete
digest: sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
status: downloaded newer image for docker.io/tomcat:latest
docker rmi
docker rmi 镜像名:tag,没写tag默认是latest,删除某个镜像
[root@izwz9g1c3fleilt56ermd5z ~]# docker rmi hello-world
untagged: hello-world:latest
untagged: docker.io/hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
-f 强制删除,如果有容器依赖镜像则需要强制删除
[root@izwz9g1c3fleilt56ermd5z ~]# docker rmi -f hello-world
untagged: hello-world:latest
untagged: docker.io/hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
可以批量删除,镜像后面加空格继续追加即可
[root@izwz9g1c3fleilt56ermd5z ~]# docker rmi -f hello-world tomcat
untagged: hello-world:latest
untagged: docker.io/hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
untagged: tomcat:latest
untagged: docker.io/tomcat@sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
deleted: sha256:040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb
deleted: sha256:03ac467e286d36301dba7daaec530f481074197d22d41bd48a32e5a2e3069573
deleted: sha256:c56c3d4e36aac2060fdb29c95baf4b4d6916fe9a454751b77b8d89f27dd8dda4
deleted: sha256:0f3911a87064866cd2aa7fd863afc1e02bdfa4856afa351e871f0e6a48cfd52b
deleted: sha256:7891c5716382337382836e0af1ea998bf77c1eed9c8e806c7aac5f489952f4ba
deleted: sha256:56dee2fc4110951264d4b134b303920c633aabbbb0264dc218518b3547320de2
deleted: sha256:cd253bbc5a1ce457359958f4905b53a3f6f0ac167ba5065f68385600fdbae99e
deleted: sha256:03b0292dbc6978fe1077f948ac07472a8cac23fb4c3cf92c76f5eb62891e3d5a
deleted: sha256:19f2a825ed46bbf16a06d9d5185a0ac68196217f302e760abb4cbc3178b14cc1
deleted: sha256:b4f0436e967b6b9b021d3ed900a5422da6d8fe8c17c51c7d7e4f9c105812f868
deleted: sha256:4762552ad7d851a9901571428078281985074e5ddb806979dd7ad24748db4ca0
docker rmi -f \$(docker ps -qa) \${}可以用命令的结果做占位
docker 容器相关
docker run
docker run 命令可以创建并启动一个容器 参数格式为 docker run [options] image [command] [arg...]
options说明
- --name 容器新名字 为容器指定一个名称
- -d 后台运行容器,并返回容器id,也即启动守护式容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -d -p 8889:8080 fenqing/tomcat02:1.2 f6cca3295b968776b63f1ee5733f230bdb22a73527159f12663d79485a13186b
- -i 以交互模式运行容器,通常与-t 同时使用
- -t 为容器重新分配一个伪输入终端,通常与-i 同时使用
- -p 随机端口映射
- -p 指定端口映射,有以下四种模式
- ip:hostport:containerport
- ip:containerport
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -d -p 8889:8080 fenqing/tomcat02:1.2 f6cca3295b968776b63f1ee5733f230bdb22a73527159f12663d79485a13186b
- hostport:containerport
- containerport
-i和-t 组合使用,运行并且启动虚拟终端
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -it --name mycentos01 centos
[root@32c23775d2b1 /]# pwd
-d 守护式进程启动
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -d centos
c27c084e44bb8a2d8b23ce62e9e93d3888ff74f909178724b2e101133914bd7b
然后docker ps -a 查看发现容器已经退出,原因是docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命令,就会自动退出 可以使用在容器里执行一个死循环来模拟centos容器不停止
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -d centos /bin/sh -c "while true; do echo hello fenqing; sleep 2; done"
f3e5471f302fbf5e9e104146ad9d0b708015a5a22ad984337c265b7ca1ff6c10
docker ps [options]
docker ps 查看现有的容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker ps
container id image command created status ports names
32c23775d2b1 centos "/bin/bash" 27 minutes ago up 27 minutes mycentos01
8b2481b4ac48 redis "docker-entrypoint..." 6 weeks ago up 2 weeks 0.0.0.0:6379->6379/tcp redis
d335af0a7119 nginx:1.10 "nginx -g 'daemon ..." 2 months ago up 3 weeks 0.0.0.0:80->80/tcp, 443/tcp nginx
253d60c0e3be kibana:7.4.2 "/usr/local/bin/du..." 2 months ago up 2 weeks 0.0.0.0:5601->5601/tcp kibana
9ab079d39b8d elasticsearch:7.9.3 "/tini -- /usr/loc..." 2 months ago up 3 weeks 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
6aa3c6d07701 nacos/nacos-server "bin/docker-startu..." 2 months ago up 3 weeks 0.0.0.0:8848->8848/tcp nacos
8d37715ca8a8 mysql:5.7.32 "docker-entrypoint..." 2 months ago up 3 weeks 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
options 说明
- -a 列出当前所有正在运行的容器 历史上运行过的
[root@izwz9g1c3fleilt56ermd5z ~]# docker ps -a container id image command created status ports names 32c23775d2b1 centos "/bin/bash" 35 minutes ago up 35 minutes mycentos01 af3b17c82193 bf756fb1ae65 "/hello" about an hour ago exited (0) about an hour ago sad_euler 8b2481b4ac48 redis "docker-entrypoint..." 6 weeks ago up 2 weeks 0.0.0.0:6379->6379/tcp redis abaece61dd15 wordpress "docker-entrypoint..." 8 weeks ago exited (0) 3 weeks ago wordpress d335af0a7119 nginx:1.10 "nginx -g 'daemon ..." 2 months ago up 3 weeks 0.0.0.0:80->80/tcp, 443/tcp nginx 253d60c0e3be kibana:7.4.2 "/usr/local/bin/du..." 2 months ago up 2 weeks 0.0.0.0:5601->5601/tcp kibana 9ab079d39b8d elasticsearch:7.9.3 "/tini -- /usr/loc..." 2 months ago up 3 weeks 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch 6aa3c6d07701 nacos/nacos-server "bin/docker-startu..." 2 months ago up 3 weeks 0.0.0.0:8848->8848/tcp nacos 8d37715ca8a8 mysql:5.7.32 "docker-entrypoint..." 2 months ago up 3 weeks 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
- -l 显示最近创建的容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker ps -l container id image command created status ports names 32c23775d2b1 centos "/bin/bash" 38 minutes ago exited (0) 11 seconds ago mycentos01
- -n 显示最近n个创建的容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker ps -n 5 container id image command created status ports names 32c23775d2b1 centos "/bin/bash" 39 minutes ago exited (0) 50 seconds ago mycentos01 af3b17c82193 bf756fb1ae65 "/hello" about an hour ago exited (0) about an hour ago sad_euler 8b2481b4ac48 redis "docker-entrypoint..." 6 weeks ago up 2 weeks 0.0.0.0:6379->6379/tcp redis abaece61dd15 wordpress "docker-entrypoint..." 8 weeks ago exited (0) 3 weeks ago wordpress d335af0a7119 nginx:1.10 "nginx -g 'daemon ..." 2 months ago up 3 weeks 0.0.0.0:80->80/tcp, 443/tcp nginx
- -q 静默模式,只显示容器编号
[root@izwz9g1c3fleilt56ermd5z ~]# docker ps -q 8b2481b4ac48 d335af0a7119 253d60c0e3be 9ab079d39b8d 6aa3c6d07701 8d37715ca8a8
- --no-trunc 不截断输出
退出容器的两种方式
- exit 容器停止退出,在容器终端内输入该命令退出
- ctrl p q 容器不停止退出,在容器终端内输入按下组合键退出
docker start
docker start 容器id或者容器名,可以启动容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker start mycentos01
mycentos01
docker restart
docker restart 容器id或者容器名,可以重启容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker restart mycentos02
mycentos02
docker stop
docker stop 容器id或者容器名,可以停止容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker stop mycentos02
mycentos02
docker kill
docker kill 容器id或者容器名,强制停止容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker kill mycentos01
mycentos01
docker rm
docker rm 删除已停止的容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker rm mycentos01
mycentos01
options 说明
- -f 强制删除容器,会执行先停止再删除流程
[root@izwz9g1c3fleilt56ermd5z ~]# docker rm -f mycentos02
mycentos02
- 一次性删除多个容器
- docker rm -f \$(docker ps -a -q) 删除所有容器,将\$()内的命令结果当做参数,属于shell语法
- docker ps -a -q | xargs docker rm 删除所有容器,将前者命令的结果当做参数给到后者命令
docker logs
docker logs 查询容器的日志
[root@izwz9g1c3fleilt56ermd5z ~]# docker logs f3e
hello fenqing
hello fenqing
hello fenqing
options说明
-t 及收入时间戳
[root@izwz9g1c3fleilt56ermd5z ~]# docker logs -t f3e
2021-01-25t02:06:24.337771000z hello fenqing
2021-01-25t02:06:26.346211000z hello fenqing
2021-01-25t02:06:28.348766000z hello fenqing
-f 跟随最新日志打印
[root@izwz9g1c3fleilt56ermd5z ~]# docker logs -f f3e
hello fenqing
hello fenqing
hello fenqing
--tall 数字显示最后多少条
[root@izwz9g1c3fleilt56ermd5z ~]# docker logs --tail 1 f3e
hello fenqing
docker top
docker top 查看docker容器内进程
[root@izwz9g1c3fleilt56ermd5z ~]# docker top f3e
uid pid ppid c stime tty time cmd
root 31088 31070 0 10:06 ? 00:00:00 /bin/sh -c while true; do echo hello fenqing; sleep 2; done
root 31394 31088 0 10:12 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
docker inspect
docker inspect 容器id 查看容器内部细节
[root@izwz9g1c3fleilt56ermd5z ~]# docker inspect f3e
[
{
"id": "f3e5471f302fbf5e9e104146ad9d0b708015a5a22ad984337c265b7ca1ff6c10",
"created": "2021-01-25t02:06:24.13609455z",
"path": "/bin/sh",
"args": [
"-c",
"while true; do echo hello fenqing; sleep 2; done"
],
"state": {
"status": "running",
"running": true,
"paused": false,
"restarting": false,
"oomkilled": false,
"dead": false,
"pid": 31088,
"exitcode": 0,
"error": "",
"startedat": "2021-01-25t02:06:24.339002895z",
"finishedat": "0001-01-01t00:00:00z"
},
"image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"resolvconfpath": "/var/lib/docker/containers/f3e5471f302fbf5e9e104146ad9d0b708015a5a22ad984337c265b7ca1ff6c10/resolv.conf",
"hostnamepath": "/var/lib/docker/containers/f3e5471f302fbf5e9e104146ad9d0b708015a5a22ad984337c265b7ca1ff6c10/hostname",
"hostspath": "/var/lib/docker/containers/f3e5471f302fbf5e9e104146ad9d0b708015a5a22ad984337c265b7ca1ff6c10/hosts",
"logpath": "",
"name": "/cocky_kalam",
"restartcount": 0,
"driver": "overlay2",
"mountlabel": "",
"processlabel": "",
"apparmorprofile": "",
"execids": null,
"hostconfig": {
"binds": null,
"containeridfile": "",
"logconfig": {
"type": "journald",
"config": {}
},
"networkmode": "default",
"portbindings": {},
"restartpolicy": {
"name": "no",
"maximumretrycount": 0
},
"autoremove": false,
"volumedriver": "",
"volumesfrom": null,
"capadd": null,
"capdrop": null,
"dns": [],
"dnsoptions": [],
"dnssearch": [],
"extrahosts": null,
"groupadd": null,
"ipcmode": "",
"cgroup": "",
"links": null,
"oomscoreadj": 0,
"pidmode": "",
"privileged": false,
"publishallports": false,
"readonlyrootfs": false,
"securityopt": null,
"utsmode": "",
"usernsmode": "",
"shmsize": 67108864,
"runtime": "docker-runc",
"consolesize": [
0,
0
],
"isolation": "",
"cpushares": 0,
"memory": 0,
"nanocpus": 0,
"cgroupparent": "",
"blkioweight": 0,
"blkioweightdevice": null,
"blkiodevicereadbps": null,
"blkiodevicewritebps": null,
"blkiodevicereadiops": null,
"blkiodevicewriteiops": null,
"cpuperiod": 0,
"cpuquota": 0,
"cpurealtimeperiod": 0,
"cpurealtimeruntime": 0,
"cpusetcpus": "",
"cpusetmems": "",
"devices": [],
"diskquota": 0,
"kernelmemory": 0,
"memoryreservation": 0,
"memoryswap": 0,
"memoryswappiness": -1,
"oomkilldisable": false,
"pidslimit": 0,
"ulimits": null,
"cpucount": 0,
"cpupercent": 0,
"iomaximumiops": 0,
"iomaximumbandwidth": 0
},
"graphdriver": {
"name": "overlay2",
"data": {
"lowerdir": "/var/lib/docker/overlay2/ce04f8aeb99d708b4fa3ea2eb45aaf1b3daa53195d8afc0cbcaf575b73053e97-init/diff:/var/lib/docker/overlay2/9c9e47d354836790de324736040a427c9d171929bb70aaca3baa60e837491777/diff",
"mergeddir": "/var/lib/docker/overlay2/ce04f8aeb99d708b4fa3ea2eb45aaf1b3daa53195d8afc0cbcaf575b73053e97/merged",
"upperdir": "/var/lib/docker/overlay2/ce04f8aeb99d708b4fa3ea2eb45aaf1b3daa53195d8afc0cbcaf575b73053e97/diff",
"workdir": "/var/lib/docker/overlay2/ce04f8aeb99d708b4fa3ea2eb45aaf1b3daa53195d8afc0cbcaf575b73053e97/work"
}
},
"mounts": [],
"config": {
"hostname": "f3e5471f302f",
"domainname": "",
"user": "",
"attachstdin": false,
"attachstdout": false,
"attachstderr": false,
"tty": false,
"openstdin": false,
"stdinonce": false,
"env": [
"path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"cmd": [
"/bin/sh",
"-c",
"while true; do echo hello fenqing; sleep 2; done"
],
"image": "centos",
"volumes": null,
"workingdir": "",
"entrypoint": null,
"onbuild": null,
"labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "gplv2",
"org.label-schema.name": "centos base image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "centos"
}
},
"networksettings": {
"bridge": "",
"sandboxid": "844f87080acf92b538f4948ca6dae4bb4764b356e51ae46199156eeaff9ece71",
"hairpinmode": false,
"linklocalipv6address": "",
"linklocalipv6prefixlen": 0,
"ports": {},
"sandboxkey": "/var/run/docker/netns/844f87080acf",
"secondaryipaddresses": null,
"secondaryipv6addresses": null,
"endpointid": "d3ba39bbd5532b1f7c6d2cf21bb107ea3f9ab456e4b5103ee512a2d1fe936d18",
"gateway": "172.17.0.1",
"globalipv6address": "",
"globalipv6prefixlen": 0,
"ipaddress": "172.17.0.8",
"ipprefixlen": 16,
"ipv6gateway": "",
"macaddress": "02:42:ac:11:00:08",
"networks": {
"bridge": {
"ipamconfig": null,
"links": null,
"aliases": null,
"networkid": "7aaaabad7bc8c86d46db221e582b43b38d2c746e6f1c9b589eae2cbc0e657a26",
"endpointid": "d3ba39bbd5532b1f7c6d2cf21bb107ea3f9ab456e4b5103ee512a2d1fe936d18",
"gateway": "172.17.0.1",
"ipaddress": "172.17.0.8",
"ipprefixlen": 16,
"ipv6gateway": "",
"globalipv6address": "",
"globalipv6prefixlen": 0,
"macaddress": "02:42:ac:11:00:08"
}
}
}
}
]
docker attach
docker attach 容器id,可以进入到容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker attach c9e
[root@c9ea3f54cea7 /]# ls
bin dev etc home lib lib64 lost found media mnt opt proc root run sbin srv sys tmp usr var
docker exec
docker exec 可以执行容器内某种脚本 即可以在主机命令行直接执行脚本,如果运行bin/bash则进入容器
[root@izwz9g1c3fleilt56ermd5z ~]# docker exec -t c9e ls -l /tmp
total 8
-rwx------ 1 root root 701 dec 4 17:37 ks-script-esd4my7v
-rwx------ 1 root root 671 dec 4 17:37 ks-script-eusq_sc5
[root@izwz9g1c3fleilt56ermd5z ~]# docker exec -t c9e /bin/bash
[root@c9ea3f54cea7 /]#
docker cp
docker cp 容器id:容器内路径 目的主机路径 ,可以复制文件
[root@izwz9g1c3fleilt56ermd5z ~]# docker cp c9e:/opt/application.yml ~
[root@izwz9g1c3fleilt56ermd5z ~]# ls
application.yml elasticsearch-analysis-ik-7.9.3.zip
小结
push 推送制定镜像或者库镜像至docker源服务器
restart 重启运行的容器
rm 删除一个或者多个容器
rmi 删除一个或者多个镜像[无容器使用的镜像才可删除,否则需要删除相关容器才可以继续,或者-f强制删]
run 创建一个新的容器并运行一个命令
save 保存一个镜像为一个tar包
search 在docker hub中搜索镜像
start 启动容器
stop 停止容器
tag 给源中镜像打上标签
top 查看容器中运行的进程信息
unpause 取消暂停容器
version 查看docker版本号
wait 截取容器停止时的退出状态值
docker 镜像高级
镜像是一种轻量级,可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,他包含运行某个软件所需的所有内容,包括代码,运行时,库,环境变量,和配置文件
unionfs 联合文件系统
unionfs 是一种分层,轻量级,并且搞性能的文件系统,对文件系统的修改最稳一次提交来一层层的叠加 特性:一次同事加载多个文件系统,但是总体看来,只能看到一个文件系统,联合加载或吧各层文件系统叠加起来,最终的文件系统会包含所有底层文件和目录
docker 镜像加载原理
docker的镜像实际上有一层一层的文件系统组成,这种层级的文件系统unionfs,
bootfs,主要包含bootloader和kernel,botloader主要是引导加载kernel,linux启动时会加载bootfs文件系统,在docker镜像最底层是bootfs,这一层与linux/unix系统是一样的
rootfs 在bootfs之上,包含最经典的/dev /proc /bin /etc 等标准目录,和文件,rootfs就是各种不同的操作系统发行版
为什么centos的镜像是4g,docker的镜像只有200m 对于一个精简的os,rootfs可以很小,只需要包括最基本的命令,工具,程序库, 就可以,因为底层直接使用主机的kernel,自己只需要提供rootfs即可,由于可见对于不同的linux发行版, bootfs基本是一致的,rootfs会有差别,英雌不同的发行版可以共用bootfs
docker commit
docker commit 提交容器使之成为新的镜像
[root@izwz9g1c3fleilt56ermd5z ~]# docker commit -a "fenqing" -m "tomcat 自己定制" 7468 fenqing/tomcat02:1.2
sha256:fb29bf1e4ca81529fc1300b2bd688b8fdff45a970f1661bb8736d235642b5785
[root@izwz9g1c3fleilt56ermd5z ~]# docker images
repository tag image id created size
fenqing/tomcat02 1.2 fb29bf1e4ca8 9 seconds ago 649 mb
docker.io/tomcat latest 040bdb29ab37 2 weeks ago 649 mb
docker.io/centos latest 300e315adb2f 7 weeks ago 209 mb
docker.io/wordpress latest cfb931188dab 2 months ago 546 mb
docker.io/redis latest 74d107221092 2 months ago 104 mb
docker.io/mysql 5.7.32 1b12f2e9257b 3 months ago 448 mb
docker.io/elasticsearch 7.9.3 1ab13f928dc8 3 months ago 742 mb
docker.io/nacos/nacos-server latest a81222848024 4 months ago 921 mb
docker.io/kibana 7.4.2 230d3ded1abc 15 months ago 1.1 gb
docker.io/nginx 1.10 0346349a1a64 3 years ago 182 mb
-a "作者名称" 指定作者名称
-m "描述信息" 指定描述信息
容器数据卷
-
是什么
将运用与运行的环境打包成容器运行,运行可以伴随容器,但是我们队数据的要求希望是持久化的
容器之间希望有可能共享数据
docker 容器产生的数据,如果不通过docker commit生成新的镜像,是的数据作为镜像的一部分保存下来,那么当容器删除后,数据自然就没有了.
为了能保存数据在docker中我们使用卷
-
能干嘛
卷就是目录或文件,存在于一个或者多个容器中,由docker挂在到容器,但不属于联合文件系统,因此 能够绕开union file system提供一些用于持续存储或者共享数据的特性
特点
数据卷可在容器之间共享或重用数据
卷中的更改可以直接生效
数据卷中更改不会包含在镜像的更新中
数据卷的生命周期一直持续到没有容器使用它为止
-
如何使用
直接命令添加
- docker run命令的-v参数,主机目录:容器内目录
[root@izwz9g1c3fleilt56ermd5z docker-study]# docker run -it -v /opt/fenqingdata/docker-study/centos1datavolume:/datavolumecontainer centos [root@129c34b93fa0 /]#
主机
[root@izwz9g1c3fleilt56ermd5z ~]# cd /opt/fenqingdata/docker-study/ [root@izwz9g1c3fleilt56ermd5z docker-study]# ls centos1datavolume tomcat
容器内
[root@129c34b93fa0 /]# ls bin datavolumecontainer dev etc home lib lib64 lost found media mnt opt proc root run sbin srv sys tmp usr var
查看数据卷是否挂载成功 可以使用上面提到的inspect命令,查看到hostconfig->binds
容器内与主机数据共享 主机写,容器读
# 主机 [root@izwz9g1c3fleilt56ermd5z centos1datavolume]# echo local -> local.txt [root@izwz9g1c3fleilt56ermd5z centos1datavolume]# ls local.txt out.log # 容器内 [root@129c34b93fa0 datavolumecontainer]# ls local.txt out.log
容器写,主机读
# 容器内 [root@129c34b93fa0 datavolumecontainer]# echo container -> container.txt [root@129c34b93fa0 datavolumecontainer]# ls container.txt local.txt out.log # 主机 [root@izwz9g1c3fleilt56ermd5z centos1datavolume]# ls container.txt local.txt out.log
容器退出后是否仍然能保持一致
# 容器退出的情况下,在主机将要挂载卷宗的目录下创建文件 [root@izwz9g1c3fleilt56ermd5z centos1datavolume]# echo local -> local1.txt [root@izwz9g1c3fleilt56ermd5z centos1datavolume]# ls container.txt local1.txt local.txt out.log # 启动容器,查看 [root@izwz9g1c3fleilt56ermd5z docker-study]# docker start 129 [root@izwz9g1c3fleilt56ermd5z docker-study]# docker attach 129 [root@129c34b93fa0 /]# cd datavolumecontainer/ [root@129c34b93fa0 datavolumecontainer]# ls container.txt local.txt local1.txt out.log [root@129c34b93fa0 datavolumecontainer]#
带权限的命令 docker run 的 -v 命令 主机目录:容器内目录:ro 镜像名,指定只读
[root@izwz9g1c3fleilt56ermd5z docker-study]# docker run -it -v /opt/fenqingdata/docker-study/centos1datavolume:/datavolumecontainer:ro centos [root@46b6e0b7c873 /]# # 但是容器内没有写权限,ro对应单词则是read-only [root@46b6e0b7c873 datavolumecontainer]# echo hello -> container1.txt bash: container1.txt: read-only file system # inspect 命令查看。得到一个数据描述 "mounts": [ { "type": "bind", "source": "/opt/fenqingdata/docker-study/centos1datavolume", "destination": "/datavolumecontainer", "mode": "ro", "rw": false,# 已经切换成只读了 "propagation": "rprivate" } ]
dockerfile添加
什么是dockerfile
dockerfile是对镜像的描述,使用volume 指令,由于在dockerfile内可以指定主机目录的话, 则会带来移植性的困扰,即并非所有机器在特定目录都能使用,所以volume会在容器内创建目录,并绑定在主机的目录上默认位置
[root@izwz9g1c3fleilt56ermd5z dockerfile]# vim dockerfile [root@izwz9g1c3fleilt56ermd5z dockerfile]# cat dockerfile from centos volume ["/datavolumecontainer1", "/datavolumecontainer2"] cmd echo "finised,-----------success1" cmd /bin/bash
dockerfile通过build命令可以创建新的容器
[root@izwz9g1c3fleilt56ermd5z dockerfile]# docker build -f ./dockerfile -t fenqing/centos02 . sending build context to docker daemon 2.048 kb step 1/4 : from centos ---> 300e315adb2f step 2/4 : volume /datavolumecontainer1 /datavolumecontainer2 ---> running in f254d54b257d ---> 492ee80ce787 removing intermediate container f254d54b257d step 3/4 : cmd echo "finised,-----------success1" ---> running in 823adfcc500f ---> 66de2f0ba779 removing intermediate container 823adfcc500f step 4/4 : cmd /bin/bash ---> running in 7aab1e5390c7 ---> 0fdd547f0ecb removing intermediate container 7aab1e5390c7 successfully built 0fdd547f0ecb [root@izwz9g1c3fleilt56ermd5z dockerfile]# docker images repository tag image id created size fenqing/centos02 latest 0fdd547f0ecb 42 seconds ago 209 mb fenqing/tomcat02 1.2 fb29bf1e4ca8 9 hours ago 649 mb docker.io/tomcat latest 040bdb29ab37 2 weeks ago 649 mb docker.io/centos latest 300e315adb2f 7 weeks ago 209 mb docker.io/wordpress latest cfb931188dab 2 months ago 546 mb docker.io/redis latest 74d107221092 2 months ago 104 mb docker.io/mysql 5.7.32 1b12f2e9257b 3 months ago 448 mb docker.io/elasticsearch 7.9.3 1ab13f928dc8 3 months ago 742 mb docker.io/nacos/nacos-server latest a81222848024 4 months ago 921 mb docker.io/kibana 7.4.2 230d3ded1abc 15 months ago 1.1 gb docker.io/nginx 1.10 0346349a1a64 3 years ago 182 mb
[root@izwz9g1c3fleilt56ermd5z dockerfile]# docker run -it fenqing/centos02 /bin/bash [root@466677b0f4fc /]# ls bin datavolumecontainer1 datavolumecontainer2 dev etc home lib lib64 lost found media mnt opt proc root run sbin srv sys tmp usr var
数据卷容器
命名的容器挂在数据卷,其他容器通过挂在这个父容器,实现数据共享,挂在数据卷的容器,称之为数据卷容器
使用--volume-form 容器id,可以继承同镜像的容器的数据卷,即,父子容器之间共享同一份数据卷,只要还有至少一个容器在使用数据卷,数据卷则一直有效
# 数据卷容器(父容器)
[root@izwz9g1c3fleilt56ermd5z dockerfile]# docker run -it --name fcentos1 fenqing/centos02 /bin/bash
[root@57f322a82815 /]# cd datavolumecontainer1
[root@57f322a82815 datavolumecontainer1]# vi centos1.txt
# 子容器1
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -it --name fcentos2 --volumes-from fcentos1 fenqing/centos02
[root@1e224af2be20 /]# cd datavolumecontainer1/
[root@1e224af2be20 datavolumecontainer1]# cat centos1.txt
test centos1
# 子容器2
[root@izwz9g1c3fleilt56ermd5z ~]# docker run -it --name fcentos3 --volumes-from fcentos1 fenqing/centos02
[root@52bcf9601227 /]# cd datavolumecontainer1/
[root@52bcf9601227 datavolumecontainer1]# cat centos1.txt
test centos1
dockerfile
-
是什么
- dockerfile 是用来构建docker镜像的构建文件,是由一些列命令和参数构成的脚本
- 构建三步骤
- 手动编写一个dockerfile文件,当然,必须要符合file的规范
- 有这个文件后,直接docker build命令执行,获得一个自定义镜像
- 运行这个容器
-
docker 过程解析
-
dockerfile 内容基础知识
- 每条保留字指令都必须为大写字母且后面要跟随至少一个参数
- 指令按照从上到下,顺序执行
- #标识注释
- 每条指令都会创建一个新的镜像层,并对镜像进行提交
-
dockerfile 的大致流程
- docker从基础镜像运行一个容器
- 执行一条指令并对容器做出修改
- 执行类似docker commit的操作提交一个新的镜像层
- docker 在基于刚提交的镜像运行一个新容器
- 执行dockerfile中的下一条指令直到所有指令都执行完成
-
小总结 从应用软件的角度来看,dockerfile,docker镜像与docker容器分别代表软件的三个不同阶段
- dockerfile是软件的原材料
- docker镜像是软件的交付品
- docker容器则可以认为是软件的运行态
dockerfile面向开发,docker镜像成为交付标准,docker容器则设计部署与运营,三者缺一不可,合力充当docker体系的基石
-
-
dockerfile体系结构
-
from 基础镜像,当前新镜像是基于哪个镜像
-
maintainer 镜像维护的姓名和邮箱地址
-
run 容器构建时需要运行的命令
-
expose 当前容器对外暴露出的端口
-
workdir 指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点
-
env 用来构建镜像过程中设置环境变量
-
add 将主机目录下的文件拷贝进镜像,且add命令会自动处理url和解压tar压缩包
-
copy 类似add,拷贝文件和目录到镜像中,将从构建上下文目录中<源路径>的文件或 者目录复制到新的一层镜像内<目标路径>位置, copy src desc 或者 copy ["src", "desc"]
-
volume 容器数据卷,用于数据保存和持久化工作
-
cmd 指定一个容器时,要运行的命令, cmd <命令> 或者 cmd ["可执行文件", "参数1", "参数2",...]
- dockerfile中可以有多个cmd命令,但只有最后一个生效,cmd会被docker run 之后的参数替换
-
entrypoint 指定一个容器启动时要运行的命令
- entrypoint 的目的和cmd一样,都是在指定容器启动程序及参数
-
onbuild 当构建一个被继承的dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发
-
总结
build | both | run |
---|---|---|
from | workdir | cmd |
maintainer | user | env |
copy | expose | |
add | volume | |
run | entrypoint | |
onbuild | ||
.dockerignore |
案例
-
base镜像(scratch)
- docker hub 中99%的镜像都是通过在base镜像中安装和配置需要的软件构建出来的
-
自定义镜像 mycentos
-
编写
- hub默认centos镜像什么情况
from scratch add centos-7-x86_64-docker.tar.xz / label \\ org.label-schema.schema-version="1.0" \\ org.label-schema.name="centos base image" \\ org.label-schema.vendor="centos" \\ org.label-schema.license="gplv2" \\ org.label-schema.build-date="20201113" \\ org.opencontainers.image.title="centos base image" \\ org.opencontainers.image.vendor="centos" \\ org.opencontainers.image.licenses="gpl-2.0-only" \\ org.opencontainers.image.created="2020-11-13 00:00:00 00:00" cmd ["/bin/bash"]
-
官方centos不支持vim与ifconfig,并且登录到终端默认在/路径,现在自定义镜像将其扩展
dockerfile:
from centos maintainer fenqing<[email protected]> env mypath /usr/local workdir \$mypath run yum -y install vim run yum -y install net-tools expose 80 cmd echo \$mypath cmd echo "success----------ok" cmd /bin/bash
build:
[root@izwz9g1c3fleilt56ermd5z dockerfile]# docker build -f ./dockerfile2 -t fenqing/centos03:1.0 . sending build context to docker daemon 3.072 kb step 1/10 : from centos ---> 300e315adb2f step 2/10 : maintainer fenqing<[email protected]> ---> running in 0cae220909a2 ---> ded525754bcd removing intermediate container 0cae220909a2 step 3/10 : env mypath /usr/local ---> running in b362f6a29ee9 ---> 3b19fce3e503 removing intermediate container b362f6a29ee9 step 4/10 : workdir \$mypath ---> d2cafdd90697 removing intermediate container deac86def4f9 step 5/10 : run yum -y install vim ---> running in e0d0a4ea5bfc centos linux 8 - appstream 665 kb/s | 6.3 mb 00:09 centos linux 8 - baseos 282 kb/s | 2.3 mb 00:08 centos linux 8 - extras 4.7 kb/s | 8.6 kb 00:01 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-15.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-15.el8.x86_64.rpm 62 kb/s | 39 kb 00:00 (2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 232 kb/s | 48 kb 00:00 (3/5): which-2.21-12.el8.x86_64.rpm 74 kb/s | 49 kb 00:00 (4/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 78 kb/s | 1.4 mb 00:17 (5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 266 kb/s | 6.3 mb 00:24 -------------------------------------------------------------------------------- total 317 kb/s | 7.8 mb 00:25 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: header v3 rsa/sha256 signature, key id 8483c65d: nokey centos linux 8 - appstream 1.6 mb/s | 1.6 kb 00:00 importing gpg key 0x8483c65d: userid : "centos (centos official signing key)
" 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-15.el8.x86_64 4/5 running scriptlet: gpm-libs-1.20.7-15.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-15.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-15.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! ---> dbf17b793d8b removing intermediate container e0d0a4ea5bfc step 6/10 : run yum -y install net-tools ---> running in c96260bf2978 last metadata expiration check: 0:00:33 ago on mon feb 1 09:43:22 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 96 kb/s | 322 kb 00:03 -------------------------------------------------------------------------------- total 85 kb/s | 322 kb 00:03 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! ---> 6e22d2b69cdb removing intermediate container c96260bf2978 step 7/10 : expose 80 ---> running in c54a10544eaf ---> 42063eff3009 removing intermediate container c54a10544eaf step 8/10 : cmd echo \$mypath ---> running in 0abd9d8cc74e ---> c2ac718dd9e7 removing intermediate container 0abd9d8cc74e step 9/10 : cmd echo "success----------ok" ---> running in 5c62bebe3f8d ---> 24050d240b42 removing intermediate container 5c62bebe3f8d step 10/10 : cmd /bin/bash ---> running in 47974823d8c5 ---> dcfd11f63ab1 removing intermediate container 47974823d8c5 successfully built dcfd11f63ab1 - 运行 docker run -it 新镜像名称:tag
- 列出镜像的变更历史
- docker history 镜像名
[root@izwz9g1c3fleilt56ermd5z dockerfile]# docker history fenqing/centos03:1.0 image created created by size comment dcfd11f63ab1 8 minutes ago /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "/b... 0 b 24050d240b42 8 minutes ago /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "ec... 0 b c2ac718dd9e7 8 minutes ago /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "ec... 0 b 42063eff3009 8 minutes ago /bin/sh -c #(nop) expose 80/tcp 0 b 6e22d2b69cdb 8 minutes ago /bin/sh -c yum -y install net-tools 23.4 mb dbf17b793d8b 8 minutes ago /bin/sh -c yum -y install vim 58.1 mb d2cafdd90697 9 minutes ago /bin/sh -c #(nop) workdir /usr/local 0 b 3b19fce3e503 9 minutes ago /bin/sh -c #(nop) env mypath=/usr/local 0 b ded525754bcd 9 minutes ago /bin/sh -c #(nop) maintainer fenqing<1286... 0 b 300e315adb2f 7 weeks ago /bin/sh -c #(nop) cmd ["/bin/bash"] 0 b
7 weeks ago /bin/sh -c #(nop) label org.label-schema.... 0 b 7 weeks ago /bin/sh -c #(nop) add file:bd7a2aed6ede423... 209 mb
- docker history 镜像名
-
cmd/entrypoint 案例
- cmd 可以有多个,但只有最后一个生效
- 小案例,安装curl,利用cmd 执行curl访问ip.com网址
# dockerfile from centos:7.9.2009 run yum install -y curl cmd ["curl", "-s", "http://ip.cn"]
如果需要加参数,例如在curl前加 -i参数
- entrypoint
- 使用entrypoint则可以实现,
[root@localhost dockerfile]# cat dockerfile2 from centos:7.9.2009 run yum install -y curl entrypoint ["curl", "-s", "http://ip.cn"]
[root@localhost dockerfile]# docker build -f ./dockerfile2 -t myip2 . sending build context to docker daemon 3.072kb step 1/3 : from centos:7.9.2009 ---> 8652b9f0cb4c step 2/3 : run yum install -y curl ---> using cache ---> 2a29b7609867 step 3/3 : entrypoint ["curl", "-s", "http://ip.cn"] ---> running in b8136ad53fc2 removing intermediate container b8136ad53fc2 ---> 7b1d383c574e successfully built 7b1d383c574e successfully tagged myip2:latest
在run镜像后,可以额外传参数,他不会覆盖原有的命令,而是以追加参数的形式执行
[root@localhost dockerfile]# docker run myip2 -i http/1.1 301 moved permanently date: tue, 02 feb 2021 17:30:52 gmt transfer-encoding: chunked connection: keep-alive cache-control: max-age=3600 expires: tue, 02 feb 2021 18:30:52 gmt location: https://ip.cn/ cf-request-id: 08056524480000e7fd4a2f4000000001 report-to: {"max_age":604800,"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report?s=ebvqoa03h2gnwh4yf75bdwo+smtrum8jlyyb2te9cigfqel38o6ek3al8yzyzv00uqkp4iiswnebnbtkjqhxk6yxkozdpa=="}],"group":"cf-nel"} nel: {"max_age":604800,"report_to":"cf-nel"} server: cloudflare cf-ray: 61b5a4807fede7fd-lax alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400
- 使用entrypoint则可以实现,
onbuild 案例
onbuild 如果自己被当做基础镜像的话,则会执行 dockerfile 基础镜像
[root@localhost dockerfile]# cat dockerfile2
from centos:7.9.2009
run yum install -y curl
entrypoint ["curl", "-s", "http://ip.cn"]
onbuild run echo "father onbuild-------886"
build
[root@localhost dockerfile]# docker build -f ./dockerfile2 -t myip3 .
sending build context to docker daemon 3.072kb
step 1/4 : from centos:7.9.2009
---> 8652b9f0cb4c
step 2/4 : run yum install -y curl
---> using cache
---> 2a29b7609867
step 3/4 : entrypoint ["curl", "-s", "http://ip.cn"]
---> using cache
---> 7b1d383c574e
step 4/4 : onbuild run echo "father onbuild-------886"
---> running in 31370a385936
removing intermediate container 31370a385936
---> 4d63fd8adaf0
successfully built 4d63fd8adaf0
successfully tagged myip3:latest
继承镜像dockerfile
[root@localhost dockerfile]# cat dockerfile3
from myip3
run yum install -y curl
entrypoint ["curl", "-s", "http://ip.cn"]
build
[root@localhost dockerfile]# docker build -f ./dockerfile3 -t myip4 .
sending build context to docker daemon 4.096kb
step 1/3 : from myip3
# executing 1 build trigger
---> running in b2b291608afa
father onbuild-------886
removing intermediate container b2b291608afa
---> 0148a4a2e330
step 2/3 : run yum install -y curl
---> running in 04d9362298f8
loaded plugins: fastestmirror, ovl
loading mirror speeds from cached hostfile
* base: mirrors.neusoft.edu.cn
* extras: mirrors.neusoft.edu.cn
* updates: mirror.bit.edu.cn
package curl-7.29.0-59.el7_9.1.x86_64 already installed and latest version
nothing to do
removing intermediate container 04d9362298f8
---> 338da1dbe843
step 3/3 : entrypoint ["curl", "-s", "http://ip.cn"]
---> running in e8e7a3c15eef
removing intermediate container e8e7a3c15eef
---> dc43b252dac6
successfully built dc43b252dac6
successfully tagged myip4:latest
确实执行了父镜像的 (打印出"father onbuild-------886")
自定义tomcat
准备好jdk和tomcat,设置相关环境变量,以及最后运行,以下是dockerfile
[root@localhost tomcat]# cat dockerfile
[root@localhost tomcat]# cat dockerfile
from centos:7.9.2009
maintainer fenqing<[email protected]>
copy c.txt /usr/local/cincontainer.txt
add jdk-8u281-linux-x64.tar.gz /usr/local/
add apache-tomcat-9.0.41.tar.gz /usr/local/
run yum -y install vim
env mypath /usr/local
workdir \$mypath
env java_home /usr/local/jdk1.8.0_281
env classpath \$java_home/lib/dt.jar:\$java_home/lib/tools.jar
env catalina_home /usr/local/apache-tomcat-9.0.41
env catalina_base \$catalina_home
env path \$path:\$java_home/bin:\$catalina_home/lib:\$catalina_home/bin
expose 8080
cmd /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.41/logs/catalina.out