//使用阿里云的镜像,不使用国外的(慢)
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum makecache fast
安装docker-ce(ce代表社区版,推荐使用这个版本)
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
启动docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 24.0.6
API version: 1.43
Go version: go1.20.7
Git commit: ed223bc
Built: Mon Sep 4 12:35:25 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.6
API version: 1.43 (minimum version 1.12)
Go version: go1.20.7
Git commit: 1a79695
Built: Mon Sep 4 12:34:28 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.24
GitCommit: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
runc:
Version: 1.1.9
GitCommit: v1.1.9-0-gccaecfc
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@localhost ~]#
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally //发现本地没有镜像,于是去Docker Hub上查找下载。
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
(---省略-----)
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
(---省略-----)
// 查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
[root@localhost ~]#
docker帮助命令
docker version #显示Docker版本信息。
docker info #显示 Docker系统信息,包括镜像和容器数。。
docker --he1p #帮助
docker images
docker images --digests
docker search mysql
docker search mysql --filter=stars=50 #列出收藏数不小于指定值的镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest #不写tag,默认使用latest,然后会进行分层下载
[root@localhost ~]# docker pull mysql:5.7 #指定版本下载
5.7: Pulling from library/mysql
docker rmi -f 镜像id #删除单个
docker rmi -f 镜像名:tag 镜像名:tag #删除多个
$(docker images -qa) #删除全部
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 3b85be0b10d3 2 months ago 581MB
mysql latest ae2502152260 2 months ago 574MB
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
[root@localhost ~]# docker rmi -f 9c7a54a9a43c #根据ID删除
#或者可用下面命令删除
[root@localhost ~]# docker rmi -f hello-world #删除hello-world镜像(latest)
docker run 镜像id #新建容器并启动
docker ps 列出所有运行的容器
docker container list
docker rm 容器id #删除指定容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
docker run常用参数
–name 给容器指定一个名字
-d 后台方式运行容器
-i 以交互模式运行容器,和-t一起起作用
-t 给容器重新分配一个终端
-P 随机端口映射(大写)
-p 指定端口映射(小结),一般可以有四种写法
启动并进入窗口测试
[root@localhost ~]# docker pull centos #需要镜像运行容器
[root@localhost ~]# docker run -it centos /bin/bash #启动并进入容器
[root@23108d3b9071 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@23108d3b9071 /]#
[root@23108d3b9071 /]# exit #退出容器
exit
[root@localhost ~]#
[root@localhost ~]# cd ..
[root@localhost /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]#
[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
23108d3b9071 centos "/bin/bash" 5 minutes ago Exited (0) 3 minutes ago ecstatic_lamport
699504c6c586 hello-world "/hello" 41 minutes ago Exited (0) 41 minutes ago practical_yalow
c5c0a2894d35 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago relaxed_herschel
2f3bb0597c74 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago practical_hugle
[root@localhost /]#
[root@localhost /]# docker ps -a -n=2 #最近运行的2个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23108d3b9071 centos "/bin/bash" 8 minutes ago Exited (0) 6 minutes ago ecstatic_lamport
699504c6c586 hello-world "/hello" 44 minutes ago Exited (0) 43 minutes ago practical_yalow
[root@localhost /]#
[root@localhost /]# docker ps -aq #显示所有窗口的编号(当前+历史)
23108d3b9071
699504c6c586
c5c0a2894d35
2f3bb0597c74
[root@localhost /]#
退出容器
exit 退出容器
ctrl+ p + q 容器不停止退出
删除容器
docker rm 容器id # 删除指定容器,不能删除正在运行的容器,如果要强制删除,加-f
docker rm -f $(docker ps -aq) # 强制删除所有容器
docker ps -a -q| xargs docker rm # 删除所有容器
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23108d3b9071 centos "/bin/bash" 20 minutes ago Exited (0) 17 minutes ago ecstatic_lamport
699504c6c586 hello-world "/hello" 55 minutes ago Exited (0) 55 minutes ago practical_yalow
c5c0a2894d35 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago relaxed_herschel
2f3bb0597c74 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago practical_hugle
[root@localhost /]# docker rm 699504c6c586 # 删除指定容器
699504c6c586
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23108d3b9071 centos "/bin/bash" 20 minutes ago Exited (0) 18 minutes ago ecstatic_lamport
c5c0a2894d35 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago relaxed_herschel
2f3bb0597c74 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago practical_hugle
[root@localhost /]# docker rm -f $(docker ps -aq) # 强制删除所有容器
23108d3b9071
c5c0a2894d35
2f3bb0597c74
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
#后台启动容器
[root@localhost ~]# docker run -d centos
3a41c5c7c5318213566fb95a7f5f9107004bc1fc04a55cb2adbaeedf5b588924
#问题:此时docker ps查看正在运行的容器,发现容器已经退出了
#原因:容器后台运行,必须有一个前台进程,docker如果发现容器没有应用,会自动停止
#自己编写一段shell脚本运行
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 5;done"
6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e0438f439df centos "/bin/sh -c 'while t…" 4 seconds ago Up 3 seconds thirsty_raman
655524a70ae2 centos "/bin/bash" 9 minutes ago Up 9 minutes hardcore_sutherland
#查看日志
[root@localhost ~]# docker logs -tf --tail 5 6e0438f439df #显示5条
2023-10-22T09:01:01.113510715Z kuangshen
2023-10-22T09:01:06.125281297Z kuangshen
2023-10-22T09:01:11.133929316Z kuangshen
2023-10-22T09:01:16.141564794Z kuangshen
2023-10-22T09:01:21.147913613Z kuangshen
2023-10-22T09:01:26.155296321Z kuangshen
2023-10-22T09:01:31.163129003Z kuangshen
^C #显示5条后,会继续打印最新的日志
[root@localhost ~]#
#查看容器的进程信息
[root@localhost ~]# docker top 6e0438f439df
UID PID PPID C STIME TTY TIME CMD
root 79550 79530 0 17:00 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 5;done
root 80114 79550 0 17:21 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
[root@localhost ~]#
#查看容器/镜像的元数据
[root@localhost ~]# docker inspect 6e0438f439df
[
{
"Id": "6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64",
"Created": "2023-10-22T09:00:55.352842039Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo kuangshen;sleep 5;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 79550,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-10-22T09:00:56.10670108Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64/hostname",
"HostsPath": "/var/lib/docker/containers/6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64/hosts",
"LogPath": "/var/lib/docker/containers/6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64/6e0438f439df5642bd1f1404ccbb1fba770b287b83033bf66bca480847c76e64-json.log",
"Name": "/thirsty_raman",
"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,
"ConsoleSize": [
24,
206
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/f7a4b9fcf88380102068108275cd65dc3d35047ffbe309ef1c306e947af15de9-init/diff:/var/lib/docker/overlay2/0003c65765408dd1a5dced26a12a96ebc378ece0910600d984e3c07d74e120de/diff",
"MergedDir": "/var/lib/docker/overlay2/f7a4b9fcf88380102068108275cd65dc3d35047ffbe309ef1c306e947af15de9/merged",
"UpperDir": "/var/lib/docker/overlay2/f7a4b9fcf88380102068108275cd65dc3d35047ffbe309ef1c306e947af15de9/diff",
"WorkDir": "/var/lib/docker/overlay2/f7a4b9fcf88380102068108275cd65dc3d35047ffbe309ef1c306e947af15de9/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "6e0438f439df",
"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 kuangshen;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": "28f0dbd011e8b73eef5f61c77cd53bdc62e69e429eb20a71223f4d8c270c624f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/28f0dbd011e8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "9ace90139e4cb8f17abf4c6a4934ca870a8a8423734072d4c652433333fd90af",
"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": "1ef6b34989f7dd4a3925baa2054986c7cf9fa1716c1cd9c4fa582715cca8ac92",
"EndpointID": "9ace90139e4cb8f17abf4c6a4934ca870a8a8423734072d4c652433333fd90af",
"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
}
}
}
}
]
[root@localhost ~]#
进入当前容器
#进入当前容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e0438f439df centos "/bin/sh -c 'while t…" 24 minutes ago Up 24 minutes thirsty_raman
655524a70ae2 centos "/bin/bash" 33 minutes ago Up 33 minutes hardcore_sutherland
[root@localhost ~]# docker exec -it 6e0438f439df /bin/bash
[root@6e0438f439df /]#
3f283ce12aa6
docker exec #进入窗口开启一个新的终端,可以在里面操作(常用)
docker attach #进入容器正在执行的的终端,不会启动新的进程
从容器内拷贝文件到主机上
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e48d5cb1850 centos "/bin/bash" 5 hours ago Up 5 hours charming_ramanujan
[root@localhost ~]#
#进入容器
[root@localhost ~]# docker attach 8e48d5cb1850
[root@8e48d5cb1850 /]# cd /home
[root@8e48d5cb1850 home]# ls
[root@8e48d5cb1850 home]# touch test.java
[root@8e48d5cb1850 home]# ls
test.java
[root@8e48d5cb1850 home]# exit
exit
[root@localhost ~]#
[root@localhost ~]# cd /home
[root@localhost home]# ls
ldsinfo
#拷贝操作(从容器里向主机/home位置拷贝)
[root@localhost home]# docker cp 8e48d5cb1850:/home/test.java /home
Successfully copied 1.54kB to /home
[root@localhost home]# ls
ldsinfo test.java
[root@localhost home]#
链接
Docker常用命令总结