Docker常用命令
帮助命令
docker version # 查看当前docker的本本信息
docker info # 显示docker的系统信息 包括docker的镜像信息和容器信息
docker 命令 --help #查看命令的用法
官方文档地址:https://docs.docker.com/reference/
镜像命令
查看镜像
#查看镜像
# docker images #查看系统本地所有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 8 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像标签
IMAGE ID 镜像id
CREATED 镜像的创建时间
SIZE 镜像的大小
搜索镜像
#查找镜像
# docker search 镜像名 [-f]
[root@localhost ~]# docker search mysql8
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
justusqiao/mysql8 mysql8.0.13-1debian9 1
cnbbx/mysql8 来自官方镜像,自行配置 1
abkrim/mysql8_legacy_password 1
codesmithtech/mysql8 Simple MySQL 8 container 1 [OK]
haakco/mysql80 0
lab08second/mysql8_native_auth 0
msimmons719/mysql8.0.19 0
hq9000/mysql8_native 0
nu11secur1ty/mysql8.0-php7.2-apache2.4-docker mysql8.0-php7-apache2.4-docker
#解释
NAME 你所在的仓库对应包的名字
DESCRIPTION 包的描述
STARS 包的星级 下载次数越多的数字越大
拉取镜像
#拉取镜像
# docker pull 镜像名[:tag] 默认不加就是拉取最新的版本
[root@localhost ~]# docker pull justusqiao/mysql8
^[[CUsing default tag: latest
latest: Pulling from justusqiao/mysql8
f17d81b4b692: Pull complete #分层下载 docker image的核心 联合文件系统
c691115e6ae9: Pull complete
41544cb19235: Pull complete
254d04f5f66d: Pull complete
4fe240edfdc9: Pull complete
0cd4fcc94b67: Pull complete
8df36ec4b34a: Pull complete
720bf9851f6a: Pull complete
e933e0a4fddf: Pull complete
9ffdbf5f677f: Pull complete
a403e1df0389: Pull complete
4669c5f285a6: Pull complete
c8a82bb770b9: Pull complete
Digest: sha256:9ce135e84abe283579040f3ba2dc2cfb7dd97f41047c6c0cef8605b416e6d8d3 #签名
Status: Downloaded newer image for justusqiao/mysql8:latest
docker.io/justusqiao/mysql8:latest #真实的地址
删除镜像
#删除镜像
# docker rmi [-f] 镜像(REPOSITORY | IMAGE ID)
#一次删除多个镜像
docker rmi [-f] 镜像id(REPOSITORY | IMAGE ID)镜像id2 ....
#删除所有的镜像
docker rmi -f $(docker images -aq)
查看镜像的详细信息
#docker inspect 镜像名 | 镜像id
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 4 weeks ago 215MB
[root@localhost ~]# docker inspect 0d120b6ccaa8
[
{
"Id": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [
"centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd"
],
"Parent": "",
"Comment": "",
"Created": "2020-08-10T18:19:49.837885498Z",
"Container": "3b04ecd9fb2d3f921f12d858edf5f3a6aa7c36c8e1e6f74bd32555fd4d7f7da2",
"ContainerConfig": {
"Hostname": "3b04ecd9fb2d",
"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",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "18.09.7",
"Author": "",
"Config": {
"Hostname": "",
"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/bash"
],
"ArgsEscaped": true,
"Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 215102299,
"VirtualSize": 215102299,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/06a5adfbdcd081fcb4e5bfce873b72ba410e82204ec3bcb213da193095000a16/merged",
"UpperDir": "/var/lib/docker/overlay2/06a5adfbdcd081fcb4e5bfce873b72ba410e82204ec3bcb213da193095000a16/diff",
"WorkDir": "/var/lib/docker/overlay2/06a5adfbdcd081fcb4e5bfce873b72ba410e82204ec3bcb213da193095000a16/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
容器命令
容器新建与启动
#docker run [可选参数] images
--nam='Name' #启动容器的名字 Nginx1 Nginx2 以便于区分容器
-d # 后台方式运行
-it #使用交互式运行 进入容器可以查看内容
-P #指定容器端口 80:80
-p ip:主机(即宿主机)端口:容器端口
-p 主机端口:容器端口 #这种方式常用
-p 容器端口
容器端口
-p #随机指定端口
接下来我们实战练习下:
启动容器
#启动一个容器
[root@localhost ~]# docker run -it --name=centos01 centos:latest
[root@e133dc9cf70c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@e133dc9cf70c /]#
查看当前运行的容器docker ps
退出容器
#exit退出容器 如果启动容器是没有使用-d后台运行的话我们敲exit的时候退出容器同时会停止容器
#我们也有办法使得以上启动的容器退出容器不停止的
Ctrl + p + q # 容器不停止退出
启动、停止、重启容器
#停止容器
[root@localhost ~]# docker stop centos03
#启动容器
[root@localhost ~]# docker start centos03
#重启容器
[root@localhost ~]# docker restart centos02
删除容器
#docker删除单个容器
docker rm 容器id|容器名 #若容器处于运行状态 使用-f 强制删除
#删除多个容器
docker rm 容器id|容器名 容器id|容器名 .....#若容器处于运行状态 使用-f 强制删除
dcoker rm -f $(docker ps -qa) #删除所有的容器
其他操作命令
进入当前正在运行的容器
#背景:通常我们都是后台运行的一些容器 现在我们妖精到容器去修改写配置
#1. docker exec -it 容器id| 容器名 运行shell(一般常用/bin/bash)
[root@localhost ~]# docker exec -it centos02 /bin/bash
[root@1bd831ce019b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1bd831ce019b /]#
#2. docker attach 容器id|容器名
[root@localhost ~]# docker attach centos02
[root@1bd831ce019b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#二者的区别
docker exec -it 容器id|容器名 运行的shell #进入容器后开启一个新的终端在新的终端里面操作 (常用)
docker attach #会在原来的终端操作
检查容器的详细信息
#docker inspect 容器id|容器名
[root@localhost ~]# docker inspect centos02
[
{
"Id": "1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a", #容器id
"Created": "2020-09-09T22:40:18.722749204Z",
"Path": "/bin/bash", #容器运行的shell环境
"Args": [],
"State": {
"Status": "exited", #容器的运行状态(刚 attach是用exit 退出 说以容器关闭)
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 127,
"Error": "",
"StartedAt": "2020-09-09T22:55:42.556344274Z",
"FinishedAt": "2020-09-10T01:01:47.217879601Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a/hostname",
"HostsPath": "/var/lib/docker/containers/1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a/hosts",
"LogPath": "/var/lib/docker/containers/1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a/1bd831ce019bc129b9866403c92f9d48ba4c29e03321ad0d3319069b71a3900a-json.log",
"Name": "/centos02",
"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,
"Capabilities": null,
"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/8927cd2f4a52c0388d3ff8f1a6e52a0c12648f79687de3525628ee02147d7648-init/diff:/var/lib/docker/overlay2/06a5adfbdcd081fcb4e5bfce873b72ba410e82204ec3bcb213da193095000a16/diff",
"MergedDir": "/var/lib/docker/overlay2/8927cd2f4a52c0388d3ff8f1a6e52a0c12648f79687de3525628ee02147d7648/merged",
"UpperDir": "/var/lib/docker/overlay2/8927cd2f4a52c0388d3ff8f1a6e52a0c12648f79687de3525628ee02147d7648/diff",
"WorkDir": "/var/lib/docker/overlay2/8927cd2f4a52c0388d3ff8f1a6e52a0c12648f79687de3525628ee02147d7648/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "1bd831ce019b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": true,
"OpenStdin": true,
"StdinOnce": false,
"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": "20200809",
"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": "dcefb8f25251dcdd50cb2c3c57c2cf5d3641c73063f93c2b0e285c06ba2769f3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/dcefb8f25251",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "27e69a0c595a673ff9c10e8b6e33de12f27f84d96cffd6eef86e91903b912f8e",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
后台启用
#docker run -d 镜像名
注意:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
查看日志
docker logs [--tail -t -f ] 容器名|容器id # --tail 是linux的tail命令 -t 是带时间戳 -f 新增有日志会持续输出
root@localhost ~]# docker logs centos02
[root@1bd831ce019b /]# exit
[root@1bd831ce019b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1bd831ce019b /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Sep09 pts/0 00:00:00 /bin/bash
root 29 1 0 00:54 pts/0 00:00:00 ps -ef
[root@1bd831ce019b /]# who
[root@1bd831ce019b /]# docker inspect centos02
bash: docker: command not found
[root@1bd831ce019b /]# exit
exit
[root@localhost ~]#
查看容器中的进程信息
# docker top 容器名|容器id
[root@localhost ~]# docker top centos02
UID PID PPID C STIME TTY TIME CMD
root 15327 15310 0 09:12 pts/0 00:00:00 /bin/bash
[root@localhost ~]#
容器与宿主机文件互拷贝
#1. docker cp 容器id:容器内目标文件路径 目的主机路径
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bd831ce019b centos "/bin/bash" 3 hours ago Up 33 minutes centos02
[root@localhost ~]# docker exec -it centos02 /bin/bash
[root@1bd831ce019b /]# cd /home/
[root@1bd831ce019b home]# touch test.txt
[root@1bd831ce019b home]# exit
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# docker cp centos02:/home/test.txt ./
[root@localhost ~]# ls
anaconda-ks.cfg test.txt
#2. docker cp 主机文件路径 容器id:容器内目的文件路径
[root@localhost ~]# docker cp ./test.txt centos02:/
[root@localhost ~]# docker exec -it centos02 /bin/bash
[root@1bd831ce019b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test.txt tmp usr var
#文件拷贝不管容器是处于运行或者停止状态都是可以拷贝的
attach Attach to a running container # 当前shell下attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem #从容器中拷贝指定文件或目录到宿主机中
create Create a new container # 创建一个新的容器,同run,但不启动容器
diff Inspect changes to files or directories on a container‘s filesystem #查看docker容器的变化
events Get real time events from the server # 从docker服务获取容器实时事件
exec Run a command in a running container # 在已存在的容器上运行命令
export Export a container filesystem as a tar archive # 导出容器的内容流作为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image # 从tar包中的内容创建一个新的文件系统镜像[对应export]
info Display system-wide information # 显示系统相关信息
inspect Return low-level information on Docker objects # 查看容器详细信息
kill Kill one or more running containers # 杀死指定的docker容器
load Load an image from a tar archive or STDIN # 从一个tar包加载一个镜像[对应save]
login Log in to a Docker registry # 注册或者登录一个docker源服务器
logout Log out from a Docker registry # 从当前Docker registry退出
logs Fetch the logs of a container # 输出当前容器日志信息
pause Pause all processes within one or more containers # 暂停容器
port List port mappings or a specific mapping for the container # 查看映射端口对应容器内部源端口
ps List containers # 列出容器列表
pull Pull an image or a repository from a registry # 从docker镜像源服务器拉取指定镜像或库镜像
push Push an image or a repository to a registry # 推送指定镜像或者库镜像至docker源服务器
rename Rename a container # 给docker容器重新命名
restart Restart one or more containers # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或者多个镜像[无容器使用该镜像时才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行一个命令
save Save one or more images to a tar archive (streamed to STDOUT by default) # 保存一个镜像为一个tar包[对应load]
search Search the Docker Hub for images # 在docker hub中搜索镜像
start Start one or more stopped containers # 启动容器
stats Display a live stream of container(s) resource usage statistics # 实时显示容器资源使用统计
stop Stop one or more running containers # 停止容器
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE # 给源中镜像打标签
top Display the running processes of a container # 查看容器中运行的进程信息
unpause Unpause all processes within one or more containers # 取消暂停容器
update Update configuration of one or more containers # 更新一个或多个容器配置
version Show the Docker version information # 查看docker版本号
wait Block until one or more containers stop, then print their exit codes # 截取容器停止时的退出状态值
实战:docker部署Nginx
#1. 启动一个Nginx 容器
[root@localhost ~]# docker run -d -it --name=Nginx01 -p 80:80 nginx
Unable to find image 'nginx:latest' locally #没有找到Nginx镜像所有到共有仓去pull
latest: Pulling from library/nginx
d121f8d1c412: Pull complete
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
Digest: sha256:9a1f8ed9e2273e8b3bbcd2e200024adac624c2e5c9b1d420988809f5c0c41a5e
Status: Downloaded newer image for nginx:latest
acddfa04bd0a603dc57a14f763d417f4327734233696d588cd3a2ed440971d00
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acddfa04bd0a nginx "/docker-entrypoint.…" 24 seconds ago Up 23 seconds 0.0.0.0:80->80/tcp Nginx01
#通过访问宿主机的80 端口得到
推荐一个不错的Docker讲解视频链接:https://www.bilibili.com/video/BV1og4y1q7M4
Docker常用命令
命令的帮助文档地址: https://docs.docker.com/engine/reference/commandline/docker/
管理镜像常用命令
创建容器
容器限制
基本命令
参考:https://blog.csdn.net/m0_49960764/article/details/124292208
-----------------------------------------------------------------