docker常用命令总结
目录重要命令总结
(1)批量操作
docker 命令 $(命令)
eg: docker stop $(docker ps -qa) 停止所有容器,根据id号去停止
docker rm $(docker ps -qa)
(2)docker help
查看docker命令帮助文档
具体命令帮助信息 docker run --help
镜像
2、docker pull
拉取镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest #默认最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
......
docker.io/library/mysql:latest
[root@localhost ~]# docker pull nginx:1.18 #可带版本号拉取
1.18: Pulling from library/nginx
f7ec5a41d630: Pull complete
0b20d28b5eb3: Pull complete
......
Status: Downloaded newer image for nginx:1.18
docker.io/library/nginx:1.18
3、docker images /docker image ls
查看镜像列表 (查看结果相同)
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 14 months ago 141MB
mysql latest 3218b38490ce 14 months ago 516MB
httpd latest dabbfbe0c57b 14 months ago 144MB
centos latest 5d0da3dc9764 18 months ago 231MB
nginx 1.18 c2c45d506085 23 months ago 133MB
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 14 months ago 141MB
mysql latest 3218b38490ce 14 months ago 516MB
httpd latest dabbfbe0c57b 14 months ago 144MB
centos latest 5d0da3dc9764 18 months ago 231MB
nginx 1.18 c2c45d506085 23 months ago 133MB
- REPOSITORT:仓库+镜像名称
- TAG:版本号
- IMAGE ID:镜像ID
- CREATED:镜像发布时间
- SIZE:镜像大小
4、docker inspect $image_ID /$docker_ID/$网络
获取容器、镜像的元数据 (也可以接镜像/容器名字 名字要确保唯一性)
[root@localhost ~]# docker inspect 605 #镜像
[
{
"Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"
],
......
[root@localhost ~]# docker inspect e50 #容器
[
{
"Id": "e50cf9f78af84e6d4cc6fafb31d0d85c7360ea5e72ef1b43805618f0d6f59a12",
"Created": "2023-03-13T02:46:18.914385955Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
5、docker rmi
删除镜像
注意:
-
删除镜像前提确保镜像没有被容器占用(容器被删除了) up、exit全是被占用,只有容器被删除了才结束占用。
-
docker rm -f 强制删除 ,强制删除被占用的镜像不会彻底被删除则名字会为none。
[root@localhost ~]# docker rmi 321 #没有被占用可直接删除
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
[root@localhost ~]# docker stop e86 #解除占用再删除镜像
e86
[root@localhost ~]# docker rm e86
e86
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
容器
1、docker ps
查看容器进程列表
选项:
- -a 显示所有的容器,包括未运行的。
- -f 根据条件过滤显示的内容 (--filter)
- -l 显示最近创建的容器
- -n (数字) 列出最近创建的n个容器
- -q 只显示容器的id号
- -s 显示总的文件大小
- --no-trunc 不截断输出 (完整id号)
#查看所有容器进程列表
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a7296ba4ba7 centos "/bin/bash" 37 seconds ago Up 36 seconds jolly_wescoff
#过滤满足条件的容器 也可以docker ps --filter
[root@localhost ~]# docker ps -f status=running
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a7296ba4ba7 centos "/bin/bash" 3 minutes ago Up 3 minutes jolly_wescoff
可以按照容器名称过滤,也可以按照容器的运行状态(created、restarting、running、removing、paused、exited、dead)过滤,也可以按照容器的ID号进行过滤
#显示最近创建的容器
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a7296ba4ba7 centos "/bin/bash" 19 minutes ago Up 19 minutes jolly_wescoff
#列出最近创建的n容器
[root@localhost ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce31e5f1067 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp priceless_heyrovsky
9a7296ba4ba7 centos "/bin/bash" 22 minutes ago Up 22 minutes jolly_wescoff
#只显示容器id
[root@localhost ~]# docker ps -aq
6ce31e5f1067
9a7296ba4ba7
#显示文件总大小
[root@localhost ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
6ce31e5f1067 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp priceless_heyrovsky 1.09kB (virtual 141MB)
#不截断输出
[root@localhost ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce31e5f106702b4babd48c3d95360d12d0a9dc33f51986057aaff928babaa43 nginx "/docker-entrypoint.sh nginx -g 'daemon off;'" 6 minutes ago Up 6 minutes 80/tcp priceless_heyrovsky
2、docker run
格式
docker run [options] image
选项:(常用)
- -d 在后台运行容器并打印容器ip
- -it 以交互式伪终端的方式运行 (在前台!!!)
- -itd 以交互式伪终端的方式运行在后台
- --restart=always 开机自启
- --name 为容器分配一个名称
- -p 主机映射端口:容器固定服务端口 将容器的端口映射到主机
- -P 将容器的端口随机映射到主机
- -v 主机:容器 绑定挂载卷
- --add-host 格式:host:ip 添加主机映射到容器 (hosts)
- --dns 设置自定义DNS服务器
- -e 设置环境变量
- --net / --network 网络模式
- --rm 退出时自动删除
- --link 链接的容器名:别名 将链接添加到另一个容器(并且链接会随ip变化而变化) 相当于动态hosts
# -d 后台运行
[root@localhost ~]# docker run -d httpd
77f56fbe5c9b04d5a8db1a49b0a5092971a26d0acd867d269b274f16205116cb
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77f56fbe5c9b httpd "httpd-foreground" 3 seconds ago Up 3 seconds 80/tcp pedantic_mclaren #一直放在后台运行
# -it交互式伪终端在前台运行
[root@localhost ~]# docker run -it centos
[root@fa5d85d0d047 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa5d85d0d047 centos "/bin/bash" 7 seconds ago Exited (0) 4 seconds ago goofy_gates #在前台直接进入终端,退出后容器停止运行
#以交互式伪终端运行在后台
[root@localhost ~]# docker run -itd centos
228b1f92c1324751fcab2a40f1ebaf5500fa5b4f9119c99dffd6aadaca3add55
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
228b1f92c132 centos "/bin/bash" 7 seconds ago Up 6 seconds trusting_wright
#开机自启、为容器分配名字、主机端口映射
[root@localhost ~]# docker run -itd --restart=always --name http-1 -p 8000:80 httpd:latest
8ab08a43ec55051a3d7286557d05452bfe38326062a9416a8da5762e7c8f29ea
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ab08a43ec55 httpd:latest "httpd-foreground" 11 seconds ago Up 10 seconds 0.0.0.0:8000->80/tcp, :::8000->80/tcp http-1 #把主机8000端口映射给容器80端口
#随机映射
[root@localhost ~]# docker run -itd --restart=always --name http-2 -P httpd:latest
a3f526704035b3961933d084fbbc50e8302ac8a5c5b59c6b2ea2280b003b05e6
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3f526704035 httpd:latest "httpd-foreground" 23 seconds ago Up 22 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp http-2 #随机映射32768给容器80
#绑定挂载卷(目录)
[root@localhost ~]# docker run -itd --name nginx-2 -P -v /share/html:/usr/share/nginx/html nginx:latest #将宿主机目录挂载在容器的某个目录
eee076d230e5001de2179c45121af1f4e63605bf67789d49c5ff8a6301a36da3
#绑定挂载卷(文件) 文件要具体到文件或者容器内目录下只有一个文件
[root@localhost ~]# docker run -itd --name nginx-1 -P -v /share/etc/nginx.conf:/etc/nginx/nginx.conf nginx:latest #将宿主机文件挂载在容器的某个文件
#添加主机名映射(hosts)
[root@docker ~]# docker run -itd --name centos-1 --add-host="docker.example.com:192.168.130.50" centos:latest
5076b560e4bb459417c59c1d5667d4e766a23bebdd1710e54c1652a279bbab89 #为容器添加映射
[root@docker ~]# docker exec -it 50 /bin/bash
[root@5076b560e4bb /]# ping docker.example.com #能通过添加的域名访问资源
PING docker.example.com (192.168.130.50) 56(84) bytes of data.
64 bytes from docker.example.com (192.168.130.50): icmp_seq=1 ttl=64 time=0.051 ms
^C
--- docker.example.com ping statistics ---
[root@5076b560e4bb /]# cat /etc/hosts
.....
192.168.130.50 docker.example.com #能看到hosts文件
#dns、环境变量
[root@docker ~]# docker run -itd --dns 114.114.114.114 -e "MYSQL_ROOT_PASSWORD=123456" mysql
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c306c4507358 mysql "docker-entrypoint.s…" 9 seconds ago Up 9 seconds 3306/tcp, 33060/tcp stoic_mendel
#网络
[root@docker ~]# docker run -itd --network net-1 --name centos centos
[root@docker ~]# docker run -itd --net bridge --name centos-2 centos
#链接
[root@docker ~]# docker run -itd --name http-1 --link mysql-1:db1 httpd
[root@docker ~]# docker exec -it http-1 /bin/bash
root@34a085f7e12e:/usr/local/apache2# cat /etc/hosts
......
172.17.0.2 db1 d4182d0f9480 mysql-1 #链接在了mysql-1 通过db1访问 会随数据库ip变化自动变化
注意:
1、系统容器-d放在后台会直接退出,服务容器不会(docker exec 分配it就行)
3、docker exec
退出时不会结束容器进程
#进入容器并分配一个bash
[root@docker ~]# docker exec -it http-1 /bin/bash
root@34a085f7e12e:/usr/local/apache2# exit
4、docker attach
退出时即刻结束容器进程
[root@docker ~]# docker attach http-1
[Sun Apr 16 09:28:45.527489 2023] [mpm_event:notice] [pid 1:tid 140216923340096] AH00492: caught SIGWINCH, shutting down gracefully
注意:docker exec 不会影响容器运行的状态,而docker attach 会影响容器运行的状态,即退出容器,容器会被终止运行
5、docker stop
# 停止容器
[root@docker ~]# docker stop mysql-1
mysql-1
6、docker kill
# 强制停止容器
[root@docker ~]# docker kill mysql-1
mysql-1
7、docker rm
#删除容器
[root@docker ~]# docker rm mysql-1
mysql-1
8、docker restart
#重启容器
[root@docker ~]# docker restart http-1
http-1
9、docker port
查看容器端口映射
[root@docker ~]# docker port http-2
80/tcp -> 0.0.0.0:32768
80/tcp -> [::]:32768
10、docker cp
docker cp 容器:路径 宿主机路径
docker cp 宿主机路径 容器:路径
#从宿主机复制文件到容器
[root@localhost share]# docker cp /share/zc.txt http-1:/
Preparing to copy...
Copying to container - 1.536kB
Successfully copied 1.536kB to http-1:/
#从容器复制文件到宿主机
[root@localhost ~]# docker cp http-1:/usr/local/apache2/htdocs/index.html /share/
Preparing to copy...
Successfully copied 2.048kB to /share/
注意:当容器已经运行起来了,这时,需要传递文件给容器内部使用,就建议使用docker cp。
容器没有启动也可复制
标签:总结,容器,ago,--,常用命令,docker,root,localhost From: https://www.cnblogs.com/favoyyqxxxxx/p/17323997.html