目录
(1)docker start/stop/restart/kill
一、Dockers 名词术语
1. Docker 镜像
Docker镜像是Docker容器运行时的只读模板,每一个镜像由一系列的层组成。当改变一个Docker镜像时,比如升级某个程序到其新版本,一个新的层就会被创建,而不用替换整个原先的镜像或者重新建立镜像。docker 的镜像概念类似虚拟机的镜像。是一个只读的模板,一个独立的文件系统,包括运行容器所需的数据,可以用来创建新的容器。
2. Docker 仓库
Docker仓库就是用来保存Docker镜像的,可以i理解为代码管理控制中的代码仓库。
3. Docker 容器
Docker容器和文件夹很相似,一个Docker容器包含了某个应用运行所需要的所有环境,每一个Docker容器都是从Docker镜像创建的。可以对Docker容器执行运行、开始、停止、移动和删除等操作。每一个Docker容器都是独立和安全的应用平台,Docker容器时Docker的运行部分。可以把容器看作一个简易版的linux环境(包含root用户权限,进程空间,用户空间和网络空间等)和运行在其中的应用程序。
二、Docker的部署
1. 检查系统的内核版本
[root@zy-host ~]# uname -r
3.10.0-1127.19.1.el7.x86_64
2. 安装系统必要的软件包
[root@zy-host ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
3. 安装 docker
[root@zy-host ~]# yum install docker -y
4. 启动 docker 服务
[root@zy-host ~]# systemctl start docker
[root@zy-host ~]# ps -ef|grep docker
root 26566 1 0 21:19 ? 00:00:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
root 26571 26566 0 21:19 ? 00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root 26653 26199 0 21:19 pts/1 00:00:00 grep --color=auto docker
5. 测试 docker 安装是否成功
从结果可以看出,能正常拉取镜像文件。docker 部署完成。
[root@zy-host ~]# docker pull centos:latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for docker.io/centos:latest
三、Docker 的数据存储
Docker 管理数据的方式有以下两种:
(1)数据卷的方式
(2)容器卷的方式
1. 数据卷
数据卷是一个或多个容器专门指定绕过Union File System 的目录,为持续性或共享数据提供一些有用的功能。
(1)数据卷可以在容器间共享和重用。
(2)数据卷的数据的改变是直接修改的。
(3)数据卷数据的改变不会被包括在容器中。
(4)数据卷是持续性的,直到没有容器使用它们。
容器使用数据卷的参数说明:
-v /Dir_name #直接将数据目录挂载到容器 /Dir_name 目录
-v src:dst #将物理机目录挂载到容器目录
实例一:
[root@zy-host ~]# docker run -it --name test_docker -v /data centos
[root@408c6cfdf7a9 /]# ls -l /data
total 0
新开一个shell窗口:
[root@zy-host ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
408c6cfdf7a9 centos "/bin/bash" About a minute ago Up About a minute test_docker
[root@zy-host ~]# docker inspect 408c6cfdf7a9
[
{
"Id": "408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f",
"Created": "2023-01-01T13:34:19.116466096Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 26723,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-01-01T13:34:19.371748554Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/hostname",
"HostsPath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/hosts",
"LogPath": "",
"Name": "/test_docker",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e-init/diff:/var/lib/docker/overlay2/1160e211438f4e10a35f852fde8757300b931ea570b6ccbe612312f2723cd285/diff",
"MergedDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/merged",
"UpperDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/diff",
"WorkDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/work"
}
},
"Mounts": [
{
"Type": "volume",
"Name": "83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f",
"Source": "/var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data", #挂载的源目录路径
"Destination": "/data", #将存储目录直接挂载到窗口 /data目录
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "408c6cfdf7a9",
"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": {
"/data": {}
},
"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": "8053f79bd06f429cfc4728d5c49944292fbd2071c06ab650f69b54dcf22da3ba",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/8053f79bd06f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5721b2a49b96006bf1df575d9afbb3a2d6871c38ae460f782d940ec4fbf9d117",
"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": "4a7e8cd0fdde1b0b9e50ffef6d865d5e80f320c88b34c1f5d288bb04126a7f5e",
"EndpointID": "5721b2a49b96006bf1df575d9afbb3a2d6871c38ae460f782d940ec4fbf9d117",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
[root@zy-host ~]# cd /var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data
[root@zy-host _data]# ls
从结果发现,挂载的源目录下的确没有任何数据
下面在源目录下创建数据,然后去容器的目录查看是否有数据产生,操作如下:
[root@zy-host _data]# pwd
/var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data
[root@zy-host _data]# mkdir test
[root@zy-host _data]# ll
total 4
drwxr-xr-x 2 root root 4096 Jan 1 21:53 test
[root@408c6cfdf7a9 /]# pwd
/
[root@408c6cfdf7a9 /]# hostname
408c6cfdf7a9
[root@408c6cfdf7a9 /]# ls -l /data
total 4
drwxr-xr-x 2 root root 4096 Jan 1 13:53 test
容器挂载的目录已有数据产生。
实例二:
前者是物理机目录,后者是容器目录。
[root@zy-host _data]# docker run -it -v /data1:/mnt centos
[root@44d62f93e82e /]# cd /mnt
[root@44d62f93e82e mnt]# ls -l
total 0
在物理机目录 data1 创建文件
[root@zy-host ~]# cd /data1/
[root@zy-host data1]# echo "hello">test.txt
[root@zy-host data1]# ll
total 4
-rw-r--r-- 1 root root 6 Jan 1 21:58 test.txt
[root@zy-host data1]# cat test.txt
hello
检查容器目录是否有文件和内容:
[root@44d62f93e82e mnt]# ls -l
total 4
-rw-r--r-- 1 root root 6 Jan 1 13:58 test.txt
[root@44d62f93e82e mnt]# cat test.txt
hello
[root@44d62f93e82e mnt]# pwd
/mnt
[root@44d62f93e82e mnt]# hostname
44d62f93e82e
2. 容器卷
容器卷指定的是直接挂载其他容器的数据目录,使用的参数如下:
--volumes-from #使用其他容器的目录
实例:
[root@zy-host data1]# docker run -d --name mydocker -v /data centos
161e866bbe4c1e10df63c28e00f81995be0784a477315b8f9073af7f580e0dec
[root@zy-host data1]# docker run -it --name mynfs --volumes-from mydocker centos
[root@dd8f9e68dc7e /]# ls -l /data
total 0
此时进入mydocker 容器/data 目录写入数据进行测试:
[root@zy-host ~]# cd /var/lib/docker/volumes/5c4e8fca1cac444def95cdc129e1826e14e73d1b4ecb315541386782fc34e307/_data/
[root@zy-host _data]# ls
[root@zy-host _data]# echo "welcome to here">file
[root@zy-host _data]# ll
total 4
-rw-r--r-- 1 root root 16 Jan 1 22:11 file
再查看刚才容器中是否有数据:
[root@dd8f9e68dc7e /]# cd /data/
[root@dd8f9e68dc7e data]# ls -l
total 4
-rw-r--r-- 1 root root 16 Jan 1 14:11 file
[root@dd8f9e68dc7e data]# cat file
welcome to here
四、Docker 容器的管理
1. Docker 容器的创建和删除
创建容器方法一:
没有指定容器的名称,自动命名,状态是自动退出:
[root@zy-host ~]# docker run centos /bin/echo "nihao" #创建容器
nihao
[root@zy-host ~]# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de3d25c3fd65 centos "/bin/echo nihao" 7 seconds ago Exited (0) 6 seconds ago boring_austin
dd8f9e68dc7e centos "/bin/bash" 10 minutes ago Up 10 minutes mynfs
161e866bbe4c centos "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago mydocker
44d62f93e82e centos "/bin/bash" 22 minutes ago Up 21 minutes stoic_swanson
408c6cfdf7a9 centos "/bin/bash" 44 minutes ago Up 44 minutes test_docker
创建容器方法二:
创建一个自定义名称的容器。
[root@zy-host ~]# docker run --name zy -t -i centos /bin/bash
[root@d77e6cd81fcf /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 14:22 ? 00:00:00 /bin/bash
root 14 1 0 14:22 ? 00:00:00 ps -ef
[root@zy-host ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d77e6cd81fcf centos "/bin/bash" About a minute ago Exited (0) 50 seconds ago zy
de3d25c3fd65 centos "/bin/echo nihao" 5 minutes ago Exited (0) 5 minutes ago boring_austin
dd8f9e68dc7e centos "/bin/bash" 15 minutes ago Up 15 minutes mynfs
161e866bbe4c centos "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago mydocker
44d62f93e82e centos "/bin/bash" 27 minutes ago Up 27 minutes stoic_swanson
408c6cfdf7a9 centos "/bin/bash" 49 minutes ago Up 49 minutes test_docker
删除容器:
[root@zy-host ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d77e6cd81fcf centos "/bin/bash" About a minute ago Exited (0) 50 seconds ago zy
de3d25c3fd65 centos "/bin/echo nihao" 5 minutes ago Exited (0) 5 minutes ago boring_austin
dd8f9e68dc7e centos "/bin/bash" 15 minutes ago Up 15 minutes mynfs
161e866bbe4c centos "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago mydocker
44d62f93e82e centos "/bin/bash" 27 minutes ago Up 27 minutes stoic_swanson
408c6cfdf7a9 centos "/bin/bash" 49 minutes ago Up 49 minutes test_docker
[root@zy-host ~]# docker rm de3d25c3fd65 #删除一个停止的容器
de3d25c3fd65
[root@zy-host ~]# docker rm -f dd8f9e68dc7e #删除一个正在运行的容器
dd8f9e68dc7e
[root@zy-host ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d77e6cd81fcf centos "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago zy
161e866bbe4c centos "/bin/bash" 19 minutes ago Exited (0) 19 minutes ago mydocker
44d62f93e82e centos "/bin/bash" 31 minutes ago Up 31 minutes stoic_swanson
408c6cfdf7a9 centos "/bin/bash" 53 minutes ago Up 53 minutes test_docker
2. 进入与退出容器:
启动和进入:
[root@zy-host ~]# docker start 408c6cfdf7a9
408c6cfdf7a9
[root@zy-host ~]# docker attach 408c6cfdf7a9
[root@408c6cfdf7a9 /]#
退出:exit
[root@408c6cfdf7a9 /]# exit
exit
3. docker 容器的运行
创建docker容器之后,需要将容器运行起来,运行一个容器的操作命令如下:
-d 表示启动到后台运行
-P 随机端口映射
-p 指定端口映射
[root@zy-host ~]# docker run -d -P nginx
8fc624241bdce84094a0d6ce1ff4952ba1d1a112e591e5215fbfc8307f07c720
[root@zy-host ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8fc624241bdc nginx "/docker-entrypoin..." About a minute ago Up About a minute 0.0.0.0:32769->80/tcp infallible_austin
44d62f93e82e centos "/bin/bash" 45 minutes ago Up 45 minutes stoic_swanson
实例操作:
[root@zy-host ~]# docker run -d -p 81:80 nginx
cba9790a8dd91c73ed5f5eba40f33d432fa3ae1e8402cc519231de8846f6da13
[root@zy-host ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cba9790a8dd9 nginx "/docker-entrypoin..." 4 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp vibrant_wiles
8fc624241bdc nginx "/docker-entrypoin..." 3 minutes ago Up 3 minutes 0.0.0.0:32769->80/tcp infallible_austin
44d62f93e82e centos "/bin/bash" 48 minutes ago Up 48 minutes stoic_swanson
4. Docker 容器常用的管理命令
输入docker命令查看帮助信息:
[root@zy-host ~]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a 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
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
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
wait Block until one or more containers stop, then print their exit codes
(1)docker start/stop/restart/kill
启动/停止/重启/杀掉 容器
-s 表示向容器发送信号
[root@zy-host ~]# docker start zy
zy
[root@zy-host ~]# docker stop zy
zy
[root@zy-host ~]# docker restart zy
zy
[root@zy-host ~]# docker kill -s kill zy
zy
(2)docker run
功能:创建并启动一个新的容器
常用参数如下:
-d:后台运行容器,并返回容器ID
-i:以交互模式运行容器,常与参数 -t 同时使用
-t:给容器重新分配一个伪终端
--name:给容器指定一个名称
-m:指定容器使用内存的最大值
-net:指定容器使用的网络类型
--link:链接到另一容器
实例操作:
后台启动并运行一个名为nginx的容器,运行前回自动去Docker镜像站点下载最新的镜像文件。
[root@zy-host ~]# docker run -d --name nginx nginx:latest
d52cc861584327762513d105bb6b6e5e15a401d900186645582a3596f0ce9842
以交互模式运行容器,然后在容器内执行 /bin/bash 命令
[root@zy-host ~]# docker run -it nginx:latest /bin/bash
root@2277469b159c:/#
(3)docker rm
功能:删除容器
常用参数:
-f:强制删除一个运行中的容器
-l:删除指定的链接
-v:删除与容器关联的卷
[root@zy-host ~]# docker rm -f upbeat_wing
upbeat_wing
#删除容器,并删除容器挂载的数据卷
[root@zy-host ~]# docker rm -v mydocker
mydocker
(4)docker create
功能:创建一个新的容器,但不启动它。
[root@zy-host ~]# docker create --name myserver nginx:latest
25db53db75e170211e9384e09a166a0283254f93ea845e21f47e0921e7cb07a1
(5)docker exec
功能:在运行的容器中执行命令
常用参数:
-d:在后台运行
-i:保持STDIN打开
-t:分配一个伪终端
[root@d77e6cd81fcf scripts]# pwd
/server/scripts
[root@d77e6cd81fcf scripts]# ls -l
total 4
-rwxr-xr-x 1 root root 20 Jan 2 02:10 docker.sh
[root@d77e6cd81fcf scripts]# cat docker.sh
echo "hello world!"
[root@zy-host ~]# docker exec -it zy /bin/sh /server/scripts/docker.sh
hello world!
(6)docker ps
功能:列出容器(正在运行)
常用参数:
-a:列出所有容器,包括停止的
-f:根据条件过滤显示内容
-l:列出最近创建的容器
-n:列出最近创建的指定个数的容器
-q:只显示容器ID
-s:显示总文件大小
[root@zy-host ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25db53db75e1 nginx:latest "/docker-entrypoin..." 22 minutes ago Created myserver
d52cc8615843 nginx:latest "/docker-entrypoin..." 11 hours ago Up 11 hours 80/tcp nginx
(7)docker inspect
功能:获取容器的元素据
常用参数:
-f:指定返回值格式或模板文件
-s:显示总文件大小
--type:为指定类型返回JSON
(8)docker logs
功能:获取容器的日志
常用参数:
-f:跟踪日志输出
-t:显示时间戳
--tail:只显示最新n条容器日志
--since:显示某个开始时间的所有日志
[root@zy-host ~]# docker logs -f zy
(9)docker port
功能:显示指定容器的端口映射信息
[root@zy-host ~]# docker port vibrant_wiles
80/tcp -> 0.0.0.0:81
(10)docker commit
功能:用已存在的容器重新创建一个新的镜像
常用参数:
-a:提交的镜像作者
-c:使用Dockerfile指令来创建镜像
-m:提交时附上说明文字
-p:在提交时,将容器暂停
#将容器d77e6cd81fcf重新生成一个名为newdocker_images的新镜像
[root@zy-host ~]# docker commit -a "zhangyin" -m "add a new images" d77e6cd81fcf newdocker_images:v1.0.0
sha256:5b67fc4b93344797195259733bf25dce31adfaa1f47dfecfbc2100030bf56a22
(11)docker cp
功能:用于容器与物理主机之间复制文件
拷贝物理机文件到容器的 /web目录
[root@zy-host data1]# docker cp /data1/test.txt d77e6cd81fcf:/web/
进入容器查看是否复制过去:
[root@d77e6cd81fcf /]# cat /web/test.txt
hello
复制过去并改名:
[root@zy-host data1]# docker cp /data1/test.txt d77e6cd81fcf:/web/test1.txt
复制容器目录到物理主机:
[root@zy-host data1]# docker cp d77e6cd81fcf:/web /data/
(12)docker login/logout
功能:用于登录与登出容器镜像仓库
- docker login:登录到一个Docker 镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker hub。
- docker logout:登出一个Docker镜像仓库,如果未指定镜像仓库地址,则默认为官方仓库。
常用参数:
-u:登录的用户名
-p:登录的密码
[root@zy-host ~]# docker login -u username -p password
(13)docker pull/push
docker pull:从镜像仓库中拉取或更新指定镜像
docker push:将本地的镜像上传到镜像仓库(要先登录到镜像仓库)
[root@zy-host ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
Digest: sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286
Status: Image is up to date for docker.io/nginx:latest
(14)docker images
功能:显示系统本地容器镜像文件
常用参数:
-a:列出所有的镜像(含中间映像层,默认是过滤掉中间映像层)
--digests:显示镜像的摘要信息
-f:显示满足条件的镜像
--format:指定返回值的模板文件
--no-trunc:显示完整的镜像信息
-q:只显示镜像ID
[root@zy-host ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newdocker_images v1.0.0 5b67fc4b9334 24 minutes ago 233 MB
<none> <none> 99fd2c8058f6 25 minutes ago 233 MB
docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
#显示摘要信息
[root@zy-host ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
newdocker_images v1.0.0 <none> 5b67fc4b9334 25 minutes ago 233 MB
<none> <none> <none> 99fd2c8058f6 26 minutes ago 233 MB
docker.io/nginx latest sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286 1403e55ab369 11 days ago 142 MB
docker.io/centos latest sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 5d0da3dc9764 15 months ago 231 MB
(15)docker rmi
功能:删除镜像
常用参数:
-f:强制删除
[root@zy-host ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newdocker_images v1.0.0 5b67fc4b9334 30 minutes ago 233 MB
<none> <none> 99fd2c8058f6 31 minutes ago 233 MB
docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
[root@zy-host ~]# docker rmi 99fd2c8058f6
Deleted: sha256:99fd2c8058f6594b90569b3c8af43859244a85f650a5b7ee8d0e970fa986081b
[root@zy-host ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newdocker_images v1.0.0 5b67fc4b9334 30 minutes ago 233 MB
docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
(16)docker tag
功能:标记本地镜像
[root@zy-host ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newdocker_images v1.0.0 5b67fc4b9334 31 minutes ago 233 MB
docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
[root@zy-host ~]# docker tag newdocker_images:v1.0.0 newdocker_images:v2.0.0
[root@zy-host ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newdocker_images v1.0.0 5b67fc4b9334 33 minutes ago 233 MB
newdocker_images v2.0.0 5b67fc4b9334 33 minutes ago 233 MB
docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
两个镜像的ID是一样的,类似于Linux文件中与文件的硬链接。
(17)docker build
功能:使用 Dockerfile 创建镜像
常用参数如下:
-f:指定要使用的 Dockerfile 路径
--label=[] :设置镜像使用的元数据
-m:设置最大内存值
--memory-swap:设置swap的最大值为内存+swap,“-1”表示不限swap
--no-cache:创建镜像的过程不适用缓存
--pull:尝试去更新镜像的新版本
-q:安静模式,成功后只输出镜像ID
--rm:设置镜像成功后删除中间容器
--ulimit:Ulimit配置
[root@zy-host ~]# docker build https://github.com/nginxinc/docker-nginx/
(18)docker history
功能:查看指定镜像的创建历史
常用参数:
-H:以可读的格式打印镜像大小和日期,默认为true
--no-trunc:显示完整的提交记录
-q:仅列出提交记录ID
[root@zy-host ~]# docker history newdocker_images:v2.0.0
IMAGE CREATED CREATED BY SIZE COMMENT
5b67fc4b9334 38 minutes ago /bin/bash 1.67 MB add a new images
5d0da3dc9764 15 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 15 months ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
<missing> 15 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0b... 231 MB
(19)docker info
功能:显示Docker系统信息,包括镜像和容器数。
[root@zy-host ~]# docker info
Containers: 7
Running: 5
Paused: 0
Stopped: 2
Images: 3
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 8891bca22c049cd2dcf13ba2438c0bac8d7f3343 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
WARNING: You're not using the default seccomp profile
Profile: /etc/docker/seccomp.json
Kernel Version: 3.10.0-1127.19.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.795 GiB
Name: zy-host
ID: 6NL7:3E47:B4TG:4DB4:EJT4:QJ75:H7MC:JBDR:5BX5:UOMT:EM2W:ZQVQ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
(20)docker version
功能:显示Docker的版本信息
[root@zy-host ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Experimental: false