Docker的常用命令
Docker的常用命令帮助命令docker version #显示docker的版本信息docker info #显示docker的系统信息,包括镜像和容器的数量docker 命令 --help #帮助命令帮助文档的地址:https://docs.docker.com/engine/reference/commandine/镜像命令docker images 查看所有本地的主机上的镜像[root@
·
Docker的常用命令
帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandine/
镜像命令
docker images 查看所有本地的主机上的镜像
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项 #列出所有镜像
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
docker search 搜索镜像
[root@localhost centos]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10910 [OK]
mariadb MariaDB Server is a high performing open sou… 4119 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 810 [OK]
#可选项,通过搜索来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
docker pull 下载镜像
#下载镜像 docker pull 镜像名[:tag]
[root@localhost centos]# docker pull mysql
Using default tag: latest #如果不写 tag,默认就是latest
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载:docker,image的核心 联合文件系统
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:5.7
docker r m i 删除镜像
docker rmi -f 镜像id #删除指定的镜像
docker rmi -f 镜像id 镜像id ... #删除多个镜像
docker rmi -f $(docker images -aq) #删除全部的镜像
容器命令
说明:我们有了镜像才可以创建容器
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
-p ip 主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-p 随机指定端口
#测试,启动并进入容器
[root@localhost centos]# docker run -it centos /bin/bash
[root@589b6ca421fd /]# ls #查看容器内的centos,基础版本,很多命令都是不完善的
#从容器中退回主机
[root@589b6ca421fd /]# eit
exit
列出所有运行中的容器
#docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? #显示最近床的容器
-q # 只显示容器的编号
[root@localhost centos]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost centos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
589b6ca421fd centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago exciting_antonelli
fe82a1ab4f53 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago hungry_yonath
推出容器
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
常用其他命令
后台启动容器
#命令 docker run -d 镜像名
[root@localhost centos]# docker run -d centos
#问题docker ps,发现centos停止了
#常见的坑,docker 容器使用后台运行,就必须要有一个前台进行,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止
查看日志
docker logs -f -t --tail 容器,没有日志
#显示日志
-tf #显示日志
-tail number #显示日志条数
docker logs -tf -tail 10 id
查看容器中进程信息
top命令
#命令 docker top 容器id
查看镜像的元数据
#命令
docker inspect 容器id
[root@localhost centos]# docker inspect 589b6ca421fd
[
{
"Id": "589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4",
"Created": "2021-05-24T15:00:51.401276605Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-24T15:00:54.575081956Z",
"FinishedAt": "2021-05-24T15:03:07.849442165Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4/hostname",
"HostsPath": "/var/lib/docker/containers/589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4/hosts",
"LogPath": "/var/lib/docker/containers/589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4/589b6ca421fd1dda7e06702d4a937c700c409a995b913303533eb675d804cbb4-json.log",
"Name": "/exciting_antonelli",
"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/09452d02b7c485f3a5b22ce9f8ef3d5213d2f71a5dd5dfc47733303a14c45d2b-init/diff:/var/lib/docker/overlay2/9d8556057bc4d6d9b85e1b62b813168851b988d45b6e0bea53f4c17eb40129a3/diff",
"MergedDir": "/var/lib/docker/overlay2/09452d02b7c485f3a5b22ce9f8ef3d5213d2f71a5dd5dfc47733303a14c45d2b/merged",
"UpperDir": "/var/lib/docker/overlay2/09452d02b7c485f3a5b22ce9f8ef3d5213d2f71a5dd5dfc47733303a14c45d2b/diff",
"WorkDir": "/var/lib/docker/overlay2/09452d02b7c485f3a5b22ce9f8ef3d5213d2f71a5dd5dfc47733303a14c45d2b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "589b6ca421fd",
"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": "39fb81a0cd27df85bf3616110e3fb0528f1b606117f38463656923c26e02616b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/39fb81a0cd27",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c5f332ca5a0447e28ed4ea2c733368c1b4833f99e750e6268b62feb7ec35832c",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
#我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
docker exec -it 容器id bashShell
#方式二
docker attach 容器id
docker exec #进入容器后开一个新的终端,可以在里面操作(常用)
docker attach #进入容器正在执行的终端,不会启动新的进程
从容器拷贝文件到主机上
docker cp 容器id:容器内路径 目的地主机路径
#进入docker容器内部
[root@localhost centos]# docker run -it centos
[root@0aeb949649da /]# cd /home
[root@0aeb949649da home]# ls
#在容器内新建一个文件
[root@0aeb949649da home]# touch test.java
[root@0aeb949649da home]# exit
exit
[root@localhost centos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0aeb949649da centos "/bin/bash" 39 seconds ago Exited (0) 12 seconds ago dazzling_chaum
fa8d83d4da81 centos "/bin/bash" About a minute ago Exited (0) 58 seconds ago sad_tu
6c66b32d0c59 centos "/bin/bash" 55 minutes ago Exited (0) 54 minutes ago condescending_leakey
589b6ca421fd centos "/bin/bash" 13 hours ago Exited (0) 13 hours ago exciting_antonelli
fe82a1ab4f53 hello-world "/hello" 16 hours ago Exited (0) 16 hours ago hungry_yonath
#将这文件拷贝出来到主机上
[root@localhost centos]# docker cp 0aeb949649da:/home/test.java /home
[root@localhost centos]# ls
dump.rdb temp-3255.rdb 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost centos]# cd home
bash: cd: home: 没有那个文件或目录
[root@localhost centos]# cd /home
[root@localhost home]# ls
centos mysql mytest test.java
#拷贝是一个手动过程,未来我们使用 -v卷的技术,可以实现,自动同步
Docker安装Nginx
#1、搜索镜像 search
#2、下载镜像 pull
#3、运行测试
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f0b8a9a54136 12 days ago 133MB
mysql latest c0cdc95609f1 12 days ago 556MB
hello-world latest d1165f221234 2 months ago 13.3kB
centos latest 300e315adb2f 5 months ago 209MB
# -d后台运行
# -p 宿主机端口:容器内部端口
[root@localhost home]# firewall-cmd --zone=public -add-port=80/tcp --permanent
usage: see firewall-cmd man page
firewall-cmd: error: unrecognized arguments: -add-port=80/tcp
[root@localhost home]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost home]# firewall-cmd --reload
success
[root@localhost home]# docker run -d -p 80:80 nginx
3d937ac00c68322cbf177dff62da28627d738fe4602405243e6fd7669d4412f0
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d937ac00c68 nginx "/docker-entrypoint.…" 12 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp kind_shannon
[root@localhost home]# curl localhost:80
#进入nginx
[root@localhost home]# docker exec -it nginx /bin/bash
Error: No such container: nginx
[root@localhost home]# docker exec -it kind_shannon /bin/bash
root@3d937ac00c68:/# cd /etc/nginx
root@3d937ac00c68:/etc/nginx# ls
conf.d koi-utf mime.types nginx.conf uwsgi_params
fastcgi_params koi-win modules scgi_params win-utf
思考问题:每次改动nginx配置文件,都需要进入容器内部,十分麻烦,我们要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部就可以自动修改,-v 数据卷
Docker装Tomcat
#官方的使用
docker run -it -rm tomcat:9.0
#我们之前的启动都是后台,停止了容器之后,容器还是可以查到 docker run -it --rm,一般用来测试,用完就删除
#下载在启动
docker pull tomcat
#启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat
#测试访问没有问题
#进入容器
[root@localhost home]# docker exec -it tomcat01 /bin/bash
#发现问题:1、linux命令少了,2、没有webapps,阿里云镜像的原因,默认是最小的镜像,所有不必要的全都删除
思考问题:我们以后要部署项目,如果每次都要进入容器是不是十分麻烦,我要是可以在容器外部提供一个映射路径,webapps,我们在外部放置项目,就自动同步到内部就好了!
更多推荐
已为社区贡献1条内容
所有评论(0)