dockerfile:构建文件,源代码 dockerimages:通过dockerfile构建生成 docker容器:用来提供镜像运行的服务器
版本信息及帮助信息
docker version
docker info
docker --help
镜像命令
docker images 查看所有本地主机的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
镜像的仓库源 标签 镜像ID 创建时间 大小
# 扩展参数
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
docker search 搜索镜像
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10999 [OK]
mariadb MariaDB Server is a high performing open sou… 4165 [OK]
# 扩展参数
--filter=STARS=3000 #搜索出来的镜像都是STRATS大于3000的
[root@localhost ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10999 [OK]
mariadb MariaDB Server is a high performing open sou… 4165 [OK]
[root@localhost ~]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10999 [OK]
docker pull 下载镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest #不写tag默认是latest最新版本
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载 docker的核心就是联合文件系统
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
docker pull mysql == docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
a023ae82eef5: Pull complete
e76c35f20ee7: Pull complete
e887524d2ef9: Pull complete
ccb65627e1c3: Pull complete
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 2c9028880e58 5 weeks ago 447MB
mysql latest c0cdc95609f1 5 weeks ago 556MB
docker rmi 删除镜像
docker rmi -f 容器id #删除指定容器 删除多个后边空格继续跟容器id
docker rmi -f $(docker images -aq) #删除全部的容器
容器命令
有了镜像才能创建容器
运行容器
docker run [可选参数] image
# 扩展参数
--name="name" 容器名字,例:tomcat1、tomcat2用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 指定随机端口
列出所有的运行容器
docker ps 命令 #列出当前正在运行的容器
-a #列出当前正在运行的容器以及历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
退出容器
exit #停止容器并退出
ctrl+p+q #不停止容器退出
删除容器
docker rm 容器id #删除指定容器,该容器如果处于运行状态用rm -f删除
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -aq|xargs docker rm #删除所有的容器
启动和停止容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前容器
docker kill 容器id #强制终止当前容器
其他命令
后台启动容器
docker run -d 镜像名
# docker后台运行,必须要有一个前台程序,否则会自动停止
# 例如nginx 容器启动后 发现没有提供服务 就会立即停止 就是没有程序了
查看日志
docker logs -tf --tail 容器
例:
[root@localhost ~]# docker run -it centos
[root@a8721bbc495c /]# /bin/bash -c "while true;do echo hello;sleep 1;done"
ctrl+p+q
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8721bbc495c centos "/bin/bash" 3 minutes ago Up 3 minutes keen_wilson
191a77d995f9 centos "/bin/bash -c 'while…" 4 minutes ago Up 4 minutes optimistic_wozniak
#显示日志:
-tf #显示日志
--tail number #显示日志条数
[root@localhost ~]# docker logs -tf --tail 10 a8721bbc495c
查看容器中进程信息
[root@localhost ~]# docker top a8721bbc495c
UID PID PPID C STIME TTY TIME CMD
root 8781 8760 0 10:22 pts/0 00:00:00 /bin/bash
root 8952 8781 0 10:23 pts/0 00:00:00 /bin/bash -c while true;do echo hello;sleep 1;done
root 10191 8952 0 10:31 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的源数据
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8721bbc495c centos "/bin/bash" 10 minutes ago Up 10 minutes keen_wilson
191a77d995f9 centos "/bin/bash -c 'while…" 11 minutes ago Up 11 minutes optimistic_wozniak
[root@localhost ~]# docker inspect a8721bbc495c
[
{
"Id": "a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d",
"Created": "2021-06-17T02:22:28.214517185Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 8781,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-06-17T02:22:28.515580175Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d/hostname",
"HostsPath": "/var/lib/docker/containers/a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d/hosts",
"LogPath": "/var/lib/docker/containers/a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d/a8721bbc495c3d2bbc82eb077108fa7a098bdc7b3bc90b8c41aa5ec25f6ef25d-json.log",
"Name": "/keen_wilson",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/812cb9948235d88f8fc0c811abd47ca8dbd20e3ca3d784536277fadcd5fc3fbd-init/diff:/var/lib/docker/overlay2/e137a501531c05e69d6ed3cdfdc989dbf616ecf70460c2bd073d2d34381881e8/diff",
"MergedDir": "/var/lib/docker/overlay2/812cb9948235d88f8fc0c811abd47ca8dbd20e3ca3d784536277fadcd5fc3fbd/merged",
"UpperDir": "/var/lib/docker/overlay2/812cb9948235d88f8fc0c811abd47ca8dbd20e3ca3d784536277fadcd5fc3fbd/diff",
"WorkDir": "/var/lib/docker/overlay2/812cb9948235d88f8fc0c811abd47ca8dbd20e3ca3d784536277fadcd5fc3fbd/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "a8721bbc495c",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"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": "c7865fbcb427643de1cfd2b50ad687f20a783b4b9e3b0e84cef9261c7c17ba98",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/c7865fbcb427",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2e163e05ae2086ce979d431bc65837b3375384ba1225189b8b755e972866222c",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c010f9f097ae7d3e3a895b0dc7a3a966e6feea151f943e3833e54c883fb39d94",
"EndpointID": "2e163e05ae2086ce979d431bc65837b3375384ba1225189b8b755e972866222c",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器(exec 新建一个终端操作,常用)
docker exec -it 容器id bashshell
#例子:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8721bbc495c centos "/bin/bash" 15 minutes ago Up 15 minutes keen_wilson
191a77d995f9 centos "/bin/bash -c 'while…" 16 minutes ago Up 16 minutes optimistic_wozniak
[root@localhost ~]# docker exec -it a8721bbc495c /bin/bash
[root@a8721bbc495c /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@a8721bbc495c /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:22 pts/0 00:00:00 /bin/bash
root 16 1 0 02:23 pts/0 00:00:00 /bin/bash -c while true;do echo hello;sl
root 903 0 0 02:38 pts/1 00:00:00 /bin/bash
root 929 16 0 02:38 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-sheb
root 930 903 0 02:38 pts/1 00:00:00 ps -ef
进入当前正在运行的容器(attach 进入原来的终端操作,不会新建)
docker attach 容器id
#例子:
[root@localhost ~]# docker attach 191a77d995f9
hello
hello
hello
hello
CMD和ENTRYPOINT的区别
CMD # 指定容器启动时运行的命令,只有最后一个会生效,可替代 ENTRYPOINT # 指定容器启动时运行的命令,可以追加命令
从容器内拷贝文件到主机
docker cp 容器id:容器内路径 目的主机路径
#例子:
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8721bbc495c centos "/bin/bash" 24 minutes ago Up 24 minutes keen_wilson
191a77d995f9 centos "/bin/bash -c 'while…" 24 minutes ago Up 24 minutes optimistic_wozniak
[root@localhost ~]# docker exec a8721bbc495c /bin/bash
[root@localhost ~]# docker exec -it a8721bbc495c /bin/bash
[root@a8721bbc495c /]# cd /home
[root@a8721bbc495c home]# ls
[root@a8721bbc495c home]# touch 123.txt
[root@a8721bbc495c home]# exit
exit
[root@localhost ~]# docker cp a8721bbc495c:/home/123.txt /home
[root@localhost ~]# cd /home
[root@localhost home]# ls
123.txt cao
总结
docker容器数据卷
将容器内的目录挂载到本地 数据的持久化 容器之间可以通过—volumes-from来实现数据共享
直接用命令来挂载 -v
docker run -it -v 主机目录:容器内目录
#测试
docker run -it -v /home/ceshi:/home centos /bin/bash
#起来以后可以通过docker inspect 容器id
数据卷容器数据共享
#mysql数据共享,生命周期持续到没有容器使用为止,一旦持久化到本地
docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7
实战MySQL数据持久
#pull拉取MySQL镜像
docker pull mysql:5.7
#官方运行案例:
docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:tag
#运行容器
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
具名和匿名挂载
-v 容器内路径 #匿名挂载 -v 卷名:容器内路径 #具名挂载 -v /宿主机路径:容器内路径 #指定路径挂载
docker run -d -P —name nginx01 -v juming-nginx:/etc/nginx:ro nginx
ro 限制仅可读
rw 限制仅可读写
- 具名
```javascript
[root@localhost ~]# docker run -d -P —name nginx01 -v juming-nginx:/etc/nginx nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
fcec760cb0fce1c5d5f618718e1afc1e045c9878b8ea8acf9d6020bbb7359711
查看所有volume的情况
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME local juming-nginx [root@localhost ~]# docker volume inspect juming-nginx [ {
} ]"CreatedAt": "2021-07-19T16:49:23+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
所有的docker容器中的卷在没有指定目录的情况下都是挂载到 /var/lib/docker/volumes/xxxx/_data
具名挂载也是常用的挂载方式
- **匿名**
```javascript
[root@localhost ~]# docker run -P -d --name nginx00 -v /etc/nginx nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
b6d370c96836e42ee0d54f0daff04212fb00ac13ffd135f4285a75e7f593dd7b
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 1c09a343b680b1c76a9a256f29ec2ba14084d484eacf10620aef126472b8a3f4
local juming-nginx
dockerfile
用来构建docker镜像的构造文件,实际就是命令脚本 步骤: 1、写脚本文件 2、build构建镜像 3、run运行镜像 4、push发布镜像(dockerhub、阿里云镜像仓库)
#脚本中的指定都是大写,官方案例
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"]
命令参数
FROM # 基础镜像,最底层的镜像
MAINTAINER # 镜像是谁写的,标准格式:姓名+邮箱
RUN # 镜像构建的时候序言运行的命令
ADD # 添加内容
WORKDIR # 镜像的工作目录
VOLUME # 镜像的挂载目录
EXPOSE # 暴漏端口配置
CMD # 指定容器启动的时候要运行的命令,只有最后一个会生效,可被替换
ENTRYPOINT # 指定容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承的 DockerFile 这个时候就会运行 Onbuild 指令,触发指令
COPY # 类似ADD,将我们的文件拷贝到镜像
ENV # 构建的时候设置的环境变量