Docker常用命令
- 一:帮助命令
- 二:镜像命令
- 1. docker images 查看所有本地的主机上的镜像
- 2. docker search 镜像名
- 3. docker pull 下载镜像
- 4. docker rmi
- 三:容器命令
- 1.docker run 新建容器并启动
- 2.从容器返回到主机:
- 3.docker ps 列出容器
- 4.删除容器
- 5.启动和停止容器的操作
- 其它常用命令(重要)
- 后台启动容器
- 查看日志
- 查看容器中进程信息
- 查看镜像的元数据
- 进入当前正在运行的容器
- 从容器内部拷文件到主机
- 练习:
- 部署tomcat
一:帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
docker info:
[root@localhost ~]# 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)
compose: Docker Compose (Docker Inc., v2.12.2)
scan: Docker Scan (Docker Inc., v0.21.0)
Server:
Containers: 1 #容器有1个
Running: 0 #运行的容器0个
Paused: 0 #暂停的容器0个
Stopped: 1 #停止的容器0个
Images: 1 #镜像1个
Server Version: 20.10.21
Docker帮助文档:docker官网帮助文档
二:镜像命令
1. docker images 查看所有本地的主机上的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 14 months ago 13.3kB
解释:
REPOSITORY :镜像的仓库源;
TAG :镜像的标签
IMAGE ID :镜像的id
CREATED :镜像的创建时间
SIZE :镜像的大小
可选项
-a,--all :列出所有镜像
-q,--quiet :只显示镜像的id
2. docker search 镜像名
例如:
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13515 [OK]
mariadb MariaDB Server is a high performing open sou… 5159 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 696 [OK]
percona Percona Server is a fork of the MySQL relati… 595 [OK]
bitnami/mysql Bitnami MySQL Docker Image 79 [OK]
...等,只放了一部分
可选项:-f;--filter 过滤
例如:
#查看收藏量在600以上的
[root@localhost ~]# docker search mysql --filter=stars=600
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13515 [OK]
mariadb MariaDB Server is a high performing open sou… 5159 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 696 [OK]
#查看 OFFICIAL等于ok的
[root@localhost ~]# docker search mysql -f is-official=true
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13515 [OK]
mariadb MariaDB Server is a high performing open sou… 5159 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 696 [OK]
percona Percona Server is a fork of the MySQL relati… 595 [OK]
3. docker pull 下载镜像
例如:
#下载镜像 docker pull 镜像名[:tag]
[root@localhost ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
0bb5c0c24818: Pull complete #分量下载,docker image的核心,联合文件系统
cbb3106fbb5a: Pull complete
550536ae1d5e: Pull complete
33f98928796e: Pull complete
a341087cff11: Pull complete
0e26ac5b33f6: Pull complete
c883b83a7112: Pull complete
873af5c876c6: Pull complete
8fe8ebd061d5: Pull complete
7ac2553cf6b4: Pull complete
ad655e218e12: Pull complete
Digest: sha256:96439dd0d8d085cd90c8001be2c9dde07b8a68b472bd20efcbe3df78cff66492 #签名,防伪标识
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
docker pull mysql:5.7
4. docker rmi
例如:docker rmi -f 镜像id
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3842e9cdffd2 11 days ago 538MB
mysql 5.7 eef0fab001e8 3 weeks ago 495MB
hello-world latest feb5d9fea6a5 14 months ago 13.3kB
[root@localhost ~]# docker rmi -f eef0fab001e8
#删除多个容器:docker rmi -f 容器id,容器id,...
#骚操作:docker rmi -f $(docker images -ag) :删除所有镜像。
三:容器命令
有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
1.docker run 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名称 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
测试:启动并进入容器
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 14 months ago 13.3kB
centos latest 5d0da3dc9764 14 months ago 231MB
[root@localhost ~]# docker run -it centos /bin/bash
[root@0aaab05f4bf3 /]# ll
bash: ll: command not found
[root@0aaab05f4bf3 /]# ls -a
. .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@0aaab05f4bf3 /]#
2.从容器返回到主机:
[root@0aaab05f4bf3 /]# exit 退出并停止容器
exit
Ctrl + P + Q #容器不停止退出
3.docker ps 列出容器
docker ps
#列出正在运行的容器
-a #所有容器,也会带上历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0aaab05f4bf3 centos "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago nice_leakey
f5eea2ee93d6 hello-world "/hello" 4 days ago Exited (0) 4 days ago hopeful_torvalds
4.删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除加上 :-f
docker rm -f $(docker ps -aq) #删除所有容器
docker ps -a -q|xargs docker rm #删除所有容器
5.启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
其它常用命令(重要)
后台启动容器
docker run -d 镜像名
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]# docker run -d centos
3860fae53d50fcc7b05997be02c331cfca82cbabf6665587a6702490e56c067c
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
问题:docker ps ,发现centos停止了
常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有前台应用,就会自动停止,,例如:nginx,容器启动后,发现自己没有提供服务,就会立即停止。
查看日志
# 自己编写一段shell脚本保证后台运行的镜像不停止
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo dawei;sleep 1;done"
eab4df0334af25521740856c7caeb7631f1f9dd19845563cfeebcb4b349f979b
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eab4df0334af centos "/bin/sh -c 'while t…" 4 seconds ago Up 3 seconds quirky_williams
[root@localhost /]# docker logs -tf --tail 10 eab4df0334af
2022-12-11T14:04:15.294537507Z dawei
2022-12-11T14:04:16.307557420Z dawei
2022-12-11T14:04:17.321154497Z dawei
2022-12-11T14:04:18.335179408Z dawei
2022-12-11T14:04:19.348546859Z dawei
2022-12-11T14:04:20.360551612Z dawei
...
解释:
-tf 显示日志
–tail number 要显示日志的条数
查看容器中进程信息
命令:docker top 容器id
[root@localhost /]# docker top 431eed870393
UID PID PPID C STIME TTY TIME CMD
root 60717 60698 0 22:11 ? 00:00:00 /bin/sh -c while true;do echo dawei;sleep 5 ;done
root 60751 60717 0 22:11 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
[root@localhost /]#
说明:
UID:用户id;
PID:父id;
PPID: 进程id;
查看镜像的元数据
命令:docker inspect 镜像id
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
431eed870393 centos "/bin/sh -c 'while t…" 15 minutes ago Up 15 minutes quirky_pascal
[root@localhost /]# docker inspect 431eed870393
[
{
"Id": "431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e",
"Created": "2022-12-11T14:11:36.363150149Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo dawei;sleep 5 ;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 60717,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-12-11T14:11:36.775315242Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e/hostname",
"HostsPath": "/var/lib/docker/containers/431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e/hosts",
"LogPath": "/var/lib/docker/containers/431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e/431eed87039370e109e50431fc3a1efa0b497cadce9fdadaa2ec69c5dab9d63e-json.log",
"Name": "/quirky_pascal",
"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/1e6f50a2ed32b8de8a0b6dd331e51caf272d0b708928d6d220a59cb8933fd17e-init/diff:/var/lib/docker/overlay2/cd6a3eedf972705a4643ef126c89b45a0934f9e20183e9f5ff3591062e4bb097/diff",
"MergedDir": "/var/lib/docker/overlay2/1e6f50a2ed32b8de8a0b6dd331e51caf272d0b708928d6d220a59cb8933fd17e/merged",
"UpperDir": "/var/lib/docker/overlay2/1e6f50a2ed32b8de8a0b6dd331e51caf272d0b708928d6d220a59cb8933fd17e/diff",
"WorkDir": "/var/lib/docker/overlay2/1e6f50a2ed32b8de8a0b6dd331e51caf272d0b708928d6d220a59cb8933fd17e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "431eed870393",
"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 dawei;sleep 5 ;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"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": "e81c009e6da6ba6e71ea01a7355335d737de9ccd0b7d1f8d02eac4e088f2684f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e81c009e6da6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "baa0951ba57015c2771f891d195fab96afdc35da95db3195293d189d3179efd2",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "5e4b1cfc30a23811b3a4a54d0539ef961c0e31e1be6e15d37e7eaa70b1363b83",
"EndpointID": "baa0951ba57015c2771f891d195fab96afdc35da95db3195293d189d3179efd2",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@localhost
进入当前正在运行的容器
命令1:docker exec -it 容器id bashShell (进入容器后开启一个新的终端,可以在里面操作(常用))
命令2:docker attach 容器id(进入容器正在执行的终端,不会启动新的进程)
#方法一:
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
431eed870393 centos "/bin/sh -c 'while t…" 21 minutes ago Up 21 minutes quirky_pascal
[root@localhost /]# docker exec -it 431eed870393 /bin/bash
[root@431eed870393 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@431eed870393 /]#
#方式二:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
431eed870393 centos "/bin/sh -c 'while t…" 30 minutes ago Up 30 minutes quirky_pascal
[root@localhost ~]# docker attach 431eed870393
正在执行当前的代码...
从容器内部拷文件到主机
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
431eed870393 centos "/bin/sh -c 'while t…" 38 minutes ago Up 38 minutes quirky_pascal
[root@localhost ~]# docker exec -it 431eed870393 /bin/bash
[root@431eed870393 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@431eed870393 /]# cd home
[root@431eed870393 home]# ll
bash: ll: command not found
[root@431eed870393 home]# ls
# 创建一个Java文件
[root@431eed870393 home]# touch test.java
[root@431eed870393 home]# ls
test.java
[root@431eed870393 home]# exit
exit
#一开始root目录下没有test.java 文件
[root@localhost ~]# ll
总用量 1056
-rw-r--r--. 1 root root 0 11月 28 04:01 600
-rw-------. 1 root root 1234 5月 21 2022 anaconda-ks.cfg
drwxr-xr-x. 9 1001 1001 186 5月 23 2022 nginx-1.21.6
-rw-r--r--. 1 root root 1073364 5月 23 2022 nginx-1.21.6.tar.gz
# 将容器内部的test.java文件拷到主机的root文件中
[root@localhost ~]# docker cp 431eed870393:/home/test.java /root
# 成功
[root@localhost ~]# ll
总用量 1056
-rw-r--r--. 1 root root 0 11月 28 04:01 600
-rw-------. 1 root root 1234 5月 21 2022 anaconda-ks.cfg
drwxr-xr-x. 9 1001 1001 186 5月 23 2022 nginx-1.21.6
-rw-r--r--. 1 root root 1073364 5月 23 2022 nginx-1.21.6.tar.gz
-rw-r--r--. 1 root root 0 12月 11 22:52 test.java
docker命令总览图:
练习:
部署tomcat
-
获取tomcat镜像
docker pull tomcat[root@localhost ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 6e3729cf69e0: Pull complete 4d8d923227d8: Pull complete eda8241fd25f: Pull complete 35dccabde73d: Pull complete 978c906bcdda: Pull complete 45999e75f51e: Pull complete ecd30916ffc4: Pull complete Digest: sha256:dd920d167352e9e21b297dbe08c54ca35c445c100f38bf9be9a8fa85c9196e7f Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 1dad8c5497f9 8 days ago 474MB hello-world latest feb5d9fea6a5 15 months ago 13.3kB centos latest 5d0da3dc9764 15 months ago 231MB userxy2015/ngnix latest 17a92fa0c614 6 years ago 182MB
-
启动容器
docker run -d -p 3355:8080 --name tomcatTest tomcat[root@localhost ~]# docker run -d -p 3355:8080 --name tomcatTest tomcat c2d90f16f4c55121236dd9b578a20818189435bbc7e4b6042a737663ebb1bf54 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c2d90f16f4c5 tomcat "catalina.sh run" 4 seconds ago Up 3 seconds 0.0.0.0:3355->8080/tcp, :::3355->8080/tcp tomcatTest
命令讲解:
-p: 是进行端口映射,例如上面的是:将主机的3355端口映射到docker容器的8080端口上。
–name(杠杠neme):是给容器起一个名字 -
访问
使用虚拟机的ip加端口访问后发现访问到tomcat了但是通过tomcat没有访问到任何东西。此时已经证明tomcat容器启动成功了。
将webapps.dist文件夹中的文件放入到webapp文件夹下
命令:cp webapps.dist/* webapps
root@c2d90f16f4c5:/usr/local/tomcat# cp webapps.dist/* webapps
cp: -r not specified; omitting directory 'webapps.dist/docs'
cp: -r not specified; omitting directory 'webapps.dist/examples'
cp: -r not specified; omitting directory 'webapps.dist/host-manager'
cp: -r not specified; omitting directory 'webapps.dist/manager'
cp: -r not specified; omitting directory 'webapps.dist/ROOT'
root@c2d90f16f4c5:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@c2d90f16f4c5:/usr/local/tomcat# cd webapps
root@c2d90f16f4c5:/usr/local/tomcat/webapps# ll
total 4
drwxr-xr-x. 1 root root 81 Dec 18 17:08 ./
drwxr-xr-x. 1 root root 57 Dec 9 20:41 ../
drwxr-xr-x. 15 root root 4096 Dec 18 17:08 docs/
drwxr-xr-x. 7 root root 99 Dec 18 17:08 examples/
drwxr-xr-x. 6 root root 79 Dec 18 17:08 host-manager/
drwxr-xr-x. 6 root root 114 Dec 18 17:08 manager/
drwxr-xr-x. 3 root root 223 Dec 18 17:08 ROOT/
再访问就成功了
查看dockerCPU使用状态:
docker stats
[root@localhost ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c2d90f16f4c5 tomcatTest 0.33% 113.4MiB / 972.3MiB 11.66% 20.2kB / 270kB 29.7MB / 0B 32
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c2d90f16f4c5 tomcatTest 0.12% 113.4MiB / 972.3MiB 11.66% 20.2kB / 270kB 29.7MB / 0B 32