跳到主要内容

docker命令

docker命令

目录

[toc]

1、docker服务管理命令

docker 帮助命令

注意:用docker --help可以发现,docker命令可以分为2部分,一部分是Management Commands,一部分是Commands

那么这2部分命令有什么不一样的呢?

在早期,是只有Commands这一部分的,当切到docker-ce之后,它对docker客户端做了一定的改造和梳理,也就是它将以前的命令做了一定的分类,那么Management Commands这部分都是分类之后的指令,其实这算是说对之前的指令做了一定的划分,也就是说后面官方建议我们按docker + Management Commands + Commands 这样的方式来使用docker命令。不过,之前的这种docker + Commands这种命令也是保留着的。

这边给大家简单举一个例子:

docker run -d nginx 
docker run container -d nginx

#说明:这2种用法都是可以的,没什么区别,但大家一般习惯使用`docker run -d nginx `,下面那种反而显得冗余了些……

查看docker详细信息

[root@xyy admin]#docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
scan: Docker Scan (Docker Inc., v0.9.0)

Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 3
Server Version: 20.10.10
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.21.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.518GiB
Name: xyy
ID: BTTP:6NDM:A6IO:7P5M:7XSK:DE5L:7CVO:XENP:FSEG:YN6J:4YVS:5D4P
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://kvuwuws2.mirror.aliyuncs.com/
http://hub-mirror.c.163.com/
https://dockerhub.azk8s.cn/
Live Restore Enabled: false

[root@xyy admin]#

查看dcoker版本

[root@xyy admin]#docker version
Client: Docker Engine - Community
Version: 20.10.10
API version: 1.41
Go version: go1.16.9
Git commit: b485636
Built: Mon Oct 25 07:44:50 2021
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.10
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: e2f740d
Built: Mon Oct 25 07:43:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.11
GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@xyy admin]#

启动/停止/重启docker服务

systemctl start docker
systemctl enable docker

默认的 docker 的家目录:/var/lib/docker

注意:/var/lib/docker #关注这里!!!默认的 docker 的家目录,后期镜像也在这个目录下

[root@xyy ~]#cd /var/lib/docker/
[root@xyy docker]#ls
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@xyy docker]#

修改 docker 服务启动脚本(不推荐使用)

#以下只是提供了一种方法,本次并未采用此种方式。
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
改:14 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
为:14 ExecStart=/usr/bin/dockerd -H fd:// --registry-mirror=https://kvuwuws2.mirror.aliyuncs.com --containerd=/run/containerd/containerd.sock

重启 docker 服务使 daemon.json 生效
[root@docker ~]# systemctl daemon-reload #叧有修改了 systemd 服务脚本,才需要重新加载,修改配服务的置文件,才需要执行这个。
[root@docker ~]# systemctl restart docker

2、管理镜像命令

命令汇总

命令格式docker image COMMAND

指令描述
ls列出镜像
build构建镜像来自Dockerfile
history查看镜像历史
inspect显示一个或多个镜像详细信息
pull从镜像仓库拉取镜像
push推送一个镜像到镜像仓库
rm移除一个或多个镜像
prune移除没有被标记或者没有被任何容器引用的镜像
tag创建一个引用源镜像标记目标镜像
save保存一个或多个镜像到一个tar归档文件
load加载镜像来自tar归档或标准输入

搜索images:search命令

[root@docker ~]# docker search centos #从 Docker Hub 中搜索符合条件的镜像。
#说明:项目名字/docker镜像名字 描述 受欢迎受欢迎程度 是否官方提供 如果 OFFICIAL 为[ok] ,说明可以放心使用;如果使用国内源的话:还可以见到标签;

到dockerhub网站查找相应镜像

hub.docker.com网站,输入nginx,点击Tag,寻找相应版本的nginx镜像,找到后点击复制,直接粘贴在linux下即可拉取nginx镜像:

https://hub.docker.com/

本次安装nginx:1.16.1版本:docker pull nginx:1.16.1

docker pull nginx:1.16.1

查看docker镜像加速器地址

cat /etc/docker/daemon.json
或者
docker info
方法1:
[root@xyy docker]#cat /etc/docker/daemon.json
{
"registry-mirrors":["https://kvuwuws2.mirror.aliyuncs.com","http://hub-mirror.c.163.com","https://dockerhub.azk8s.cn"]
}
[root@xyy docker]#

方法2:
[root@xyy docker]#docker info
……
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://kvuwuws2.mirror.aliyuncs.com/
http://hub-mirror.c.163.com/
https://dockerhub.azk8s.cn/
Live Restore Enabled: false
……

列出镜像:ls命令

命令:
docker images 或者 docker image ls

举例:
[root@xyy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
[root@xyy ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
[root@xyy ~]#

查看镜像历史:history命令

命令:
docker history 镜像名/镜像ID

例子:

[root@xyy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
[root@xyy ~]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
04661cdce581 5 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 5 days ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 5 days ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 5 days ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 5 days ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 5 days ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 5 days ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 5 days ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 5 days ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 5 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 5 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.0 0B
<missing> 5 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.4 0B
<missing> 5 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ADD file:16dc2c6d1932194ed… 80.4MB
[root@xyy ~]#

显示一个或多个镜像的详细信息:inspect命令

命令:
docker inspect 镜像明或者镜像ID #说明:这个inspect指令比较通用,镜像和容器的详细信息都可以查看的!

举例:
[root@xyy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
[root@xyy ~]# docker inspect nginx
[
{
"Id": "sha256:04661cdce5812210bac48a8af672915d0719e745414b4c322719ff48c7da5b83",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:dfef797ddddfc01645503cef9036369f03ae920cac82d344d58b637ee861fda1"
],
"Parent": "",
"Comment": "",
"Created": "2021-11-10T01:37:48.339919326Z",
"Container": "3413c3ba78f227779d058881a3c295f060fdb434fd4d12dfc76f03caa38a1499",
"ContainerConfig": {
"Hostname": "3413c3ba78f2",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:adf31590f026f172117cf6a21418523d4e8fe40f910255a0ba848eedd4a679f2",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:adf31590f026f172117cf6a21418523d4e8fe40f910255a0ba848eedd4a679f2",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 141468644,
"VirtualSize": 141468644,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/24b8c489b39275d73e1cc6086a47be5f0e6c4d1a38e05142d97ddff69843f360/diff:/var/lib/docker/overlay2/1af7769685d68453610cdd9c5472ca567545dc157681c67dc2cb78705ab77ddd/diff:/var/lib/docker/overlay2/a990848929ee383bab11e446073eed6442ea7cf13bd8992f653c9a6765849e33/diff:/var/lib/docker/overlay2/185af95f1714f64429e7b1c64baf5bfa57ddc9447702679ed68074cdf88dd9cc/diff:/var/lib/docker/overlay2/4568f368c641deb0175dd1f86446bc9bf6290491546163c9e5215f637e9b3049/diff",
"MergedDir": "/var/lib/docker/overlay2/4074dba2823fc0d2c27ba706eb96c57bd4fec34f6a49a6932c7b25ba3069c47f/merged",
"UpperDir": "/var/lib/docker/overlay2/4074dba2823fc0d2c27ba706eb96c57bd4fec34f6a49a6932c7b25ba3069c47f/diff",
"WorkDir": "/var/lib/docker/overlay2/4074dba2823fc0d2c27ba706eb96c57bd4fec34f6a49a6932c7b25ba3069c47f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576e",
"sha256:ea56d6ebf7e573953c89f2e0a0a30e825cd6bd18843a3d07cabf056ea4b3e5b6",
"sha256:38aec0f8e5ed9d566721f8a8fa24aeb65b2e9e3643e788f6edaee28b12a83ffc",
"sha256:fc199aaed79a9fc0685fb04a7c6d799e94a65790eaab74e129b34470a6f677c1",
"sha256:921ee7f55927436cca232d123d448915c359129c19d678f7c83aa2008aa1c5ca",
"sha256:280fbd619253bce6a85998c4609713d407ee15ef8469e891ac90a17e93a8293d"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
[root@xyy ~]#

问题:如何查看标签为latest的镜像具体版本是什么?

要查看 Docker 镜像标签为 "latest" 的具体版本,可以使用以下命令:

docker inspect --format='{{index .RepoDigests 0}}' <image_name>:latest

其中,<image_name> 是你要查询的镜像名称。

这个命令使用 docker inspect 命令并指定 --format 参数,以获取有关镜像的详细信息。在这里,我们使用 {{index .RepoDigests 0}} 来提取第一个摘要信息,这通常包含镜像的版本信息。

请注意,"latest" 标签实际上是一个指向最新版本的引用,因此你可能会在不同的时间点得到不同的结果,特别是如果镜像作者更新了 "latest" 标签。最好的做法是查看容器注册表(Container Registry)或者相关文档,以获取确切的 "latest" 标签对应的版本信息。


测试过程:

查看:

[root@docusaurus-wiki ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest a3d385fc29f9 2 years ago 201MB
grafana/grafana latest 9b957e098315 2 years ago 275MB
prom/node-exporter latest 1dbe0e931976 2 years ago 20.9MB
algolia/docsearch-scraper latest 04e04eaa5c7d 2 years ago 1.74GB
[root@docusaurus-wiki ~]#

再查看:

docker inspect --format='{{index .RepoDigests 0}}' prom/prometheus:latest

[root@docusaurus-wiki ~]# docker inspect --format='{{index .RepoDigests 0}}' prom/prometheus:latest
prom/prometheus@sha256:cb9817249c346d6cfadebe383ed3b3cd4c540f623db40c4ca00da2ada45259bb
[root@docusaurus-wiki ~]#

从镜像仓库拉取镜像:pull命令

命令:
docker pull 镜像名
或者
docker pull 镜像名:tag

举例:
[root@xyy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
[root@xyy ~]# docker pull nginx:1.7.9
1.7.9: Pulling from library/nginx
Image docker.io/library/nginx:1.7.9 uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
a3ed95caeb02: Pull complete
6f5424ebd796: Pull complete
d15444df170a: Pull complete
e83f073daa67: Pull complete
a4d93e421023: Pull complete
084adbca2647: Pull complete
c9cec474c523: Pull complete
Digest: sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Status: Downloaded newer image for nginx:1.7.9
docker.io/library/nginx:1.7.9
[root@xyy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 04661cdce581 5 days ago 141MB
nginx 1.7.9 84581e99d807 6 years ago 91.7MB
[root@xyy ~]#

上传镜像到镜像仓库:push命令

例子:

[root@myserver ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 2622e6cca7eb 4 weeks ago 132MB
centos 7 b5b4d78bc90c 2 months ago 203MB
devopsvip/docker latest bf756fb1ae65 6 months ago 13.3kB
hello-world latest bf756fb1ae65 6 months ago 13.3kB

[root@myserver ~]# docker push devopsvip/docker:latest
The push refers to repository [docker.io/devopsvip/docker]
9c27e219663c: Preparing
denied: requested access to the resource is denied

[root@myserver ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: devopsvip
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@myserver ~]# docker push devopsvip/docker:latest
The push refers to repository [docker.io/devopsvip/docker]
9c27e219663c: Mounted from library/hello-world
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525

给镜像打tag:tag命令

命令:(创建一个引用源镜像标记目标镜像)
docker tag old_image_name new_image_name

例子:

举例:
[root@xyy ~]#docker tag registry.aliyuncs.com/google_containers/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0

删除镜像:rmi命令

移除一个或多个镜像:docker image rm -f 镜像名/镜像ID

docker image  rm -f redis

或者:
docker rmi devopsvip/docker:latest -f

清除<none>镜像

一旦删除了<none>容器,我们可以继续清除废弃的镜像,这些镜像可能不再使用,但是在某些场景下却无法删除。使用以下命令可以清除废弃的镜像:

docker rmi $(docker images -f "dangling=true" -q)

上述命令将从列表中选择打下标签的所有镜像,并将其删除。

image-20230716110255398

移除没有被标记或者没有被任何容器引用的镜像:prune命令

docker image prune
docker image prune -a #这个需要谨慎使用!!!这个命令会清除all未与容器关联的本地镜像和残留的镜像,这个命令清除的镜像就多了。
-a, --all Remove all unused images, not just dangling(悬挂的) ones
-f, --force Do not prompt for confirmation

相当于做一个清理吧,因为你本地镜像多的话,可能会消耗你的本地磁盘空间。这时,如果你的磁盘空间快满了,你可以通过这个指令快速清理你本地的空间。

(1)docker image prune

会移除一些悬挂的镜像,也就说这个镜像没有被明确地标识为被某个容器引用着,或者说这个镜像是残留的,不是一个完整的镜像。

docker image prune

(2)docker image prune -a

docker image prune -a  #这个需要谨慎使用!!!这个命令会清除all未与容器关联的本地镜像和残留的镜像,这个命令清除的镜像就多了。

导出容器文件系统到本地归档:export命令

[root@myserver ~]# docker export -o test.tar  webserver
[root@myserver ~]# mkdir docker
[root@myserver ~]# mv test.tar docker/

[root@myserver ~]# cd docker/
[root@myserver docker]# ls
test.tar
[root@myserver docker]# tar xf test.tar
l[root@myserver docker]# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv test.tar usr
boot docker-entrypoint.d etc lib media opt root sbin sys tmp var
[root@myserver docker]# cd tmp/
[root@myserver tmp]# ls
hosts

导入压缩文件到镜像:import命令

[root@myserver docker]# ls
bin dev docker-entrypoint.sh etc lib media opt root sbin sys tmp var
boot docker-entrypoint.d Dockerfile home lib64 mnt proc run srv test.tar usr
[root@myserver docker]# docker import test.tar webserver:v2
sha256:5aa7a154ce2f07e8dc94d331d8809ea40ea5fab4ffa8935e2092771f863e3c0f
[root@myserver docker]# docker history webserver:v2
IMAGE CREATED CREATED BY SIZE COMMENT
5aa7a154ce2f 15 seconds ago 130MB Imported from -
[root@myserver docker]#

Docker registry:登录/退出仓库

docker 登入私有仓库/退出私有仓库

1.docker in

  1. 用法

docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub

docker login -u 用户名 -p 密码 仓库名称
  1. 举例
(1)登录到个人私有仓库(阿里云镜像仓库)
[root@docker ~]#docker login --username=执次一生为寻爱zxl registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

2.docker logout

  1. 用法

docker logout : 登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub

语法:    docker logout [SERVER]
  1. 举例
(1)从docker hub退出
[root@docker ~]#docker logout
Removing login credentials for https://index.docker.io/v1/

(2)从私有仓库退出
[root@docker ~]#docker logout registry.cn-hangzhou.aliyuncs.com
Removing login credentials for registry.cn-hangzhou.aliyuncs.com

3、管理容器命令

命令汇总

**命令格式:**docker container COMMAND

选项描述
ls列出容器
inspect查看一个或多个容器详细信息
exec在运行容器中执行命令
commit创建一个新镜像来自一个容器
cp拷贝文件/文件夹到一个容器
logs获取一个容器日志
port列出或指定容器端口映射
top显示一个容器运行的进程
stat显示容器资源使用统计
stop/start/restart停止/启动一个或多个容器
rm删除一个或多个容器
prune移除已停止的容器

启动 docker:run命令

**命令格式:**docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

选项描述
-i, –interactive交互式
-t, –tty分配一个伪终端
-d, –detach运行容器到后台
-e,-env设置环境变量
-p, –publish list端口映射:发布容器端口到主机
-P, –publish-all发布容器所有EXPOSE的端口到宿主机随机端口
--name string指定容器名称
-h, –hostname设置容器主机名,  -h 主机名,一般主机名都不怎么设置;
--ip string指定容器IP,只能用于自定义网络
--network连接容器到一个网络
-v, –volume list
--mount mount(新方式)磁盘挂载:将文件系统附加到容器
--restart string容器退出时重启策略,默认no,可选值:[always
-m,–memory容器可以使用的最大内存量
–memory-swap允许交换到磁盘的内存量
–memory-swappiness=<0-100>容器使用SWAP分区交换的百分比(0-100,默认为-1) –oom-kill-disable 禁用OOM Killer
--cpus可以使用的CPU数量
–cpuset-cpus限制容器使用特定的CPU核心,如(0-3, 0,1)
–cpu-sharesCPU共享(相对权重)

案例:-it参数测试

所以一般-it的作用有2种作用:
1.分配一个伪终端用来夯住容器:`docker run -it -d centos`
2.分配一个伪终端进入容器里面:`docker exec -it d30e92a163cb bash`

-it参数小演示测试过程

我们首先来看下以下4条命令执行后的区别:

docker run -d nginx
docker run -d busybox

docker run -it -d nginx
docker run -it -d busybox
step1:我们先来检查下当前环境,是没有任何容器的
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#

stpe2:只加一个-d参数来创建nginx和centos容器,并观察显现
[root@docker ~]#docker run -d nginx
1d74b316872e7616efd5f3fd2c7bae18771e773c44d5c3981a865f4cf0880d43
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d74b316872e nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp silly_mcnulty
[root@docker ~]#docker run -d centos
eb355181c2c78976c2ef69f602cb895b20170e9fce4a617346324c60217f087c
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb355181c2c7 centos "/bin/bash" 3 seconds ago Exited (0) 1 second ago kind_faraday
1d74b316872e nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 80/tcp silly_mcnulty
[root@docker ~]#

step3:我们删除上述2个容器,再使用-it -d参数来创建下这2个容器,再观察下现象
[root@docker ~]#docker rm -f $(docker ps -a -q)
eb355181c2c7
1d74b316872e
[root@docker ~]#docker run -it -d nginx
e1402c02f85b5856da677a5cd59d14e885e6a0ade7c6c3a2db79299c1f6c5eb3
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1402c02f85b nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 80/tcp intelligent_swartz
[root@docker ~]#docker run -it -d centos
c174604f00b3f204e085f3c0e97d9e9e55a330746629073dbfe424b47255fb23
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c174604f00b3 centos "/bin/bash" 2 seconds ago Up 1 second stupefied_lehmann
e1402c02f85b nginx "/docker-entrypoint.…" 12 seconds ago Up 11 seconds 80/tcp intelligent_swartz
[root@docker ~]#


step4:执行后结果如下:
docker run -d nginx #能启动
docker run -d centos #退出

docker run -it -d nginx #能启动
docker run -it -d centos #能启动
原因分析:
centos是一个纯净的系统,里面并没有任何的启动1号进程的,正常来说,我们构建一个镜像,都是要为这个镜像启动一个守护进程的,就是它的1号进程,否则是没有这个进程能夯住这个容器的。因为这个容器本身就是在宿主机上的一个进程,这个进程只要是持续工作的,它必须要带有一个能夯住它的,不被退出的进程。

像这centos,它是一个纯净的系统,里面并没有任何的进程,因此你直接使用这个-d(只是放后台启动),它是直接就退出了,没有进程能夯住它。

而nginx,它是有一个进程能夯住的,`docker top 容器id` ,是用来查看容器里面进程的。nginx里面是有个主进程,会夯住容器,不被退出的。

我们再来看下nginx的进程:
加了-it之后,其实就是为了让这个伪终端成为1号进程来夯住容器;

我们也可以用-it参数分配一个伪终端进而到容器里面去:
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c174604f00b3 centos "/bin/bash" 4 minutes ago Up 4 minutes stupefied_lehmann
e1402c02f85b nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp intelligent_swartz
[root@docker ~]#docker exec -it c174604f00b3 bash
[root@c174604f00b3 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@c174604f00b3 /]#

step5:最后总结:
当我们在后期制作镜像时,也是要考虑到,当你使用你dockerfile构建成的镜像时,里面必须有个进程能够夯住它,不被退出的,否则你构建的镜像也会出现和我执行`docker run -d centos`命令相同的现象。

所以,一般情况下,只要它里面有进程夯住,大部分镜像只加上-d就可以了。

所以一般-it的作用有2种作用:
1.分配一个伪终端用来夯住容器:`docker run -it -d centos`
2.分配一个伪终端进入容器里面:`docker exec -it d30e92a163cb bash`

案例:-d -p参数演示,后台启动一个容器,并映射宿主机的8080端口到容器的80端口

docker run -d -p 8080:80 nginx
step1:创建容器
[root@docker ~]#docker run -d -p 8080:80 nginx # (宿主机端口:容器服务端口号)
13d78bd205e5972b376ef61128fdb11f0a183afc3788d6a711f6052dedd1de6d

setp2:查看容器
[root@docker ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13d78bd205e5 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp focused_babbage

step3:测试容器
[root@docker ~]#curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@docker ~]#

也可以在自己笔记本的浏览器输入:虚机ip:8080也可以直接访问nginx了:

  • 注意:一般都是把容器放在后台运行的,如果没放在后台,例如,nginx容器就会出现如下现象:
[root@docker ~]#docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/11/20 03:10:10 [notice] 1#1: using the "epoll" event method
2021/11/20 03:10:10 [notice] 1#1: nginx/1.21.4
2021/11/20 03:10:10 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/11/20 03:10:10 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2021/11/20 03:10:10 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/11/20 03:10:10 [notice] 1#1: start worker processes
2021/11/20 03:10:10 [notice] 1#1: start worker process 31
2021/11/20 03:10:10 [notice] 1#1: start worker process 32

案例:多个参数结合使用小演示

docker run -d -e env=prod -p 88:80 --name=web -h web --restart=always nginx

docker run -d -e love=xyy -p 88:80 --name=web -h web --restart=always nginx

测试过程:

step1:查看下当前环境
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

step2:创建容器
[root@docker ~]#docker run -d -e env=prod -p 88:80 --name=web -h web --restart=always nginx
f4972757af030fd92582277205275ab0a8488d52bc6a09a3f5273323d3c98736

step3:验证
#查看容器
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4972757af03 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp web

#用curl命令验证nginx服务是否可访问:
[root@docker ~]#curl 172.29.9.10:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#进到容器内部验证上面参数是否符合要求:
[root@docker ~]#docker exec -it web bash
root@web:/# hostname
web
root@web:/# echo $env
prod
root@web:/#

#如何知道这个就是我刚才创建的容器:
通过修改容器nginx服务主页来进行验证:
root@web:/# echo "i love you xyy" > /usr/share/nginx/html/index.html
root@web:/# exit
exit
[root@docker ~]#curl 172.29.9.10:88
i love you xyy #符合预期

step4:还有个问题:重启重启后,里面的数据会丢失吗?不会的!
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4972757af03 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp web
[root@docker ~]#docker restart web
web
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4972757af03 nginx "/docker-entrypoint.…" 3 minutes ago Up 4 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp web
[root@docker ~]#curl 172.29.9.10:88
i love you xyy
[root@docker ~]#
#我们可以看看这个容器的数据在宿主机里的存储位置信息:
[root@docker ~]#docker inspect web
……
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/af2c6c0ff43b63574e5d91c866f7a83e9762510df4c1d478651b0aafb6ee7f8f-init/diff:/var/lib/docker/overlay2/742d9dc47d123f263fc1fb004c07d695ba2dfe6d7618e19a39ca7f6c79989be7/diff:/var/lib/docker/overlay2/d41f068e67da25767c0ec7b7f715749a0eef18aa2fa638649178ff2cc20d1b7c/diff:/var/lib/docker/overlay2/1b1d6b0302b6aadf76a9292acac6a84f5214312d8c74ee9b0cde05c204bf34a6/diff:/var/lib/docker/overlay2/2113f22b3791825ce8baef8b6438d5c073dd8e83f2262de459d94da57d12ee21/diff:/var/lib/docker/overlay2/34069e431f4af13cdbf35e936f2a5c8e12bc8c376af3d7ff392e8f9f39c118c2/diff:/var/lib/docker/overlay2/7fcae0e3a83b93665444c0639b3b7b22dff58b7383ddb9c62a6156592464309a/diff",
"MergedDir": "/var/lib/docker/overlay2/af2c6c0ff43b63574e5d91c866f7a83e9762510df4c1d478651b0aafb6ee7f8f/merged",
"UpperDir": "/var/lib/docker/overlay2/af2c6c0ff43b63574e5d91c866f7a83e9762510df4c1d478651b0aafb6ee7f8f/diff",#读写层
"WorkDir": "/var/lib/docker/overlay2/af2c6c0ff43b63574e5d91c866f7a83e9762510df4c1d478651b0aafb6ee7f8f/work"
},
"Name": "overlay2"
},
……
[root@docker ~]#cd /var/lib/docker/overlay2/af2c6c0ff43b63574e5d91c866f7a83e9762510df4c1d478651b0aafb6ee7f8f/diff
[root@docker diff]#ls
etc root run usr var
[root@docker diff]#cat usr/share/nginx/html/index.html
i love you xyy
[root@docker diff]#

案例:限制容器使用内存和cpu的上限

docker run -d -m="500m" --cpus="1" nginx
举例:
[root@docker ~]#docker run -d -m="500m" --cpus="1" nginx
0749c6106ffa55d2b3235ca1c7a4794e4f66d074b9d0b1784be5a59baa38806a
[root@docker ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0749c6106ffa nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp admiring_galois
[root@docker ~]#

#查看:
[root@docker ~]#cat /sys/fs/cgroup/cpu/docker/0749c6106ffa55d2b3235ca1c7a4794e4f66d074b9d0b1784be5a59baa38806a/cpu.cfs_quota_us
100000
[root@docker ~]#cat /sys/fs/cgroup/cpu/docker/0749c6106ffa55d2b3235ca1c7a4794e4f66d074b9d0b1784be5a59baa38806a/cpu.cfs_period_us
100000
[root@docker ~]#cat /sys/fs/cgroup/memory/docker/0749c6106ffa55d2b3235ca1c7a4794e4f66d074b9d0b1784be5a59baa38806a/memory.limit_in_bytes
524288000
[root@docker ~]#

问题:docker容器创建后是否可以再继续添加启动参数?-2022.6.25(待解决)

1、问题需求

自己用docker起的容器实例,当时未使用--restart=always选项,导致宿主机重启后容器无法起来。那么现在有没有在不重建容器的情况下添加这个选项呢?

gitlabcisonarqube服务当时是使用terraform来部署的:

当时配置文件里好像没添加开启启动参数;

按理说,terraform应该也有这种配置参数的;

2、讨论过程

其实,docker容器如果做了数据持久化的话,那么按理说是可以重建容器的,这个应该问题不大的;

但是还是要慎重!!!

那么,还有其他办法了没???

案例:--net参数

image-20230213165334563

启动/停止/重启容器:start/stop/restart命令

  1. 语法
docker start/restart/stop 容器name/ID
  1. 举例
docker restart web

删除容器:rm命令

  1. 语法
1.一次性删除一个容器
docker rm -f 容器name/ID

2.一次性删除所有容器(包括正在运行的容器和停止的容器)
docker rm -f $(docker ps -a -q)
  1. 举例
1.查看当前所有容器
[root@xyy ~]#docker run -d --name web1 nginx
c98f4f1a00edb820d4713ab3f81a84c3b2969149fdb0906ec90d47b0cdfc4969
[root@xyy ~]#docker run -d --name web2 nginx
b2045ffb13f3e8b60f032179032e977b0d35c289113659d490134bb8fd9cfa89
[root@xyy ~]#docker run -d --name web3 nginx
974948064e4bc32fe1e96aba7f71435ca2c080dbc1682b370db59a770c7b9507
[root@xyy ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
974948064e4b nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp web3
b2045ffb13f3 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp web2
c98f4f1a00ed nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 80/tcp web1
[root@xyy ~]#

2.删除容器
a.一次性删除一个容器
[root@xyy ~]#docker rm -f web1
web1
[root@xyy ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
974948064e4b nginx "/docker-entrypoint.…" 36 seconds ago Up 36 seconds 80/tcp web3
b2045ffb13f3 nginx "/docker-entrypoint.…" 39 seconds ago Up 39 seconds 80/tcp web2

b.一次性删除所有容器
[root@xyy ~]#docker rm -f $(docker ps -a -q)
974948064e4b
b2045ffb13f3
[root@xyy ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#docker一次性删除多个容器
docker rm -f $(docker ps -a -q)

移除已停止的容器:prune命令

  1. 语法
docker container prune #会删除已经停止的容器
输入y
  1. 举例
1.查看当前容器
[root@xyy admin]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68e2d92e429d nginx "/docker-entrypoint.…" 27 seconds ago Exited (0) 20 seconds ago web2
288379b4cab8 nginx "/docker-entrypoint.…" 30 seconds ago Up 29 seconds 80/tcp web1

2.删除已经停止的容器
[root@xyy admin]#docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
68e2d92e429da3d3d49db4c53388b87d755405b0b71baac4380b7a32ba5e6757

Total reclaimed space: 1.093kB

3.验证:符合预期
[root@xyy admin]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
288379b4cab8 nginx "/docker-entrypoint.…" 45 seconds ago Up 44 seconds 80/tcp web1

列出容器:ps命令

查看当前正在运行的容器

[root@xyy ~]#docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
288379b4cab8 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp web1

查看all容器

[root@xyy ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
698ee3a7ff1d nginx "/docker-entrypoint.…" 21 seconds ago Exited (0) 4 seconds ago web2
288379b4cab8 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp web1

查看最近创建的一个容器

[root@xyy ~]#docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
698ee3a7ff1d nginx "/docker-entrypoint.…" 25 seconds ago Exited (0) 8 seconds ago web2

#选项说明:
-a, --all Show all containers (default shows just running)
-q, --quiet Only display container IDs
-l, --latest Show the latest created container (includes all states)

查看当前正在运行的容器的容器名称

[root@harbor harbor]#docker ps --format "{{.Names}}" 
nginx
harbor-jobservice
harbor-ui
redis
harbor-db
harbor-adminserver
registry
harbor-log

查看当前正在运行的容器的容器ID

[root@harbor harbor]#docker ps -q
dbc4a825422e
45980b249a3d
d866f69bd443
7f3b28563c26
09d68429f4fa
c09291959dc3
4f2f5852f4f9
26c13aa1326b

列出容器细节:inspect命令

查看容器细节
docker inspect <containerid>

进入容器:exec命令

  1. 语法
1、进入到容器命令
docker exec -it 容器ID/容器名 bash #或者这里写/bin/bash
或者
docker exec -it 容器ID/容器名 sh #或者这里写/bin/sh

2、在宿主机上直接执行容器内的命令
docker exec 容器ID/容器名 要执行的命令
  1. 举例 举例1:进入到容器命令
[root@xyy admin]#docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b08eed640daa nginx "/docker-entrypoint.…" 2 days ago Up 2 days 0.0.0.0:8080->80/tcp gallant_taussig

[root@xyy admin]#docker exec -it b08eed640daa bash
root@b08eed640daa:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@b08eed640daa:/# exit
exit

[root@xyy admin]#docker exec -it b08eed640daa sh
# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
# exit
[root@xyy admin]#

举例2:在宿主机上直接执行容器内的命令

创建一个新镜像来自一个容器:commit命令

  1. 用法
docker commit 容器id 镜像name:tag
  1. 举例
step1:运行一个centos容器
[root@xyy admin]#docker run -it -d centos
608078af64734e4a199b75ce337959e22c2944464a4a9f723fcc3ad2e8d42d85
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
608078af6473 centos "/bin/bash" 3 seconds ago Up 2 seconds boring_meninsky

step2:进到这个centos容器里面,构建好我们需要的环境
[root@xyy admin]#docker exec -it 608078af6473 bash
[root@608078af6473 /]# yum install nginx
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 2.3 MB/s | 8.1 MB 00:03
CentOS Linux 8 - BaseOS 5.8 MB/s | 3.5 MB 00:00
^CCentOS Linux 8 - Extras 1.9 kB/s | 6.6 kB 00:03
Error: Failed to download metadata for repo 'extras': Librepo was interrupted by a signal
[root@608078af6473 /]# exit
exit

step3:使用commit命令将容器转换成我们需要的镜像
#先来查看下当前本地镜像信息
[root@xyy admin]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB

#开始构建镜像
[root@xyy admin]#docker commit 608078af6473 test-commit-command:v1
sha256:8fa14905fbf663736f24188e2d2a1a1ca08621c2672b7e64ac7c58adf46b66ac

step4:构建完成后我们验证下
[root@xyy admin]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test-commit-command v1 8fa14905fbf6 9 seconds ago 257MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@xyy admin]#docker commit

拷贝文件到容器内部:cp命令

  1. 语法
拷贝文件至容器内:
docker cp file1 <containerid>:/file-to-path

拷贝容器内文件到宿主机:
docker cp <containerid>:/file-to-path/file1 file1
  1. 举例
step1:先运行一个nginx容器
[root@xyy admin]#docker run -d -p 80:80 --name web nginx
52afe57b159ff5aab9ce081a92879e1f9672f13dde67972ea5c23b9181ea73cb
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52afe57b159f nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp web

step2:执行cp命令
[root@xyy admin]#docker cp 1.ttx web:/

step3:验证容器里是否存在文件
[root@xyy admin]#docker exec web ls /
1.ttx
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@xyy admin]#

获取一个容器日志:logs命令

  1. 语法
docker logs 容器名称/容器ID -f
  1. 举例
step1:创建一个nginx容器
[root@xyy admin]#docker run -d -p 80:80 --name web nginx
693e5783acaaa45561f74a75a091525017d4509265e2b8f055b65eaa168dd390
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
693e5783acaa nginx "/docker-entrypoint.…" 2 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp web

step2:查看ninx容器的log
[root@xyy admin]#docker logs web
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/11/23 04:40:51 [notice] 1#1: using the "epoll" event method
2021/11/23 04:40:51 [notice] 1#1: nginx/1.21.4
2021/11/23 04:40:51 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/11/23 04:40:51 [notice] 1#1: OS: Linux 3.10.0-957.21.3.el7.x86_64
2021/11/23 04:40:51 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/11/23 04:40:51 [notice] 1#1: start worker processes
2021/11/23 04:40:51 [notice] 1#1: start worker process 31
2021/11/23 04:40:51 [notice] 1#1: start worker process 32
[root@xyy admin]#

列出或指定容器端口映射:port参数

  1. 语法
1、启动容器时指定宿主机映射给容器的端口:-p选项
docker run -d -e love=xyy -p 88:80 --name=web -h web --restart=always nginx

2、查看宿主机映射给容器的端口号
docker port 容器name/容器ID
  1. 举例
step1:启动一个容器
[root@xyy admin]#docker run -d -e love=xyy -p 88:80 --name=web -h web --restart=always nginx
4a2fdda7d1a377897230fc7ea658de022c7f06dcfe21924d0a4383e3413d8fe8
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a2fdda7d1a3 nginx "/docker-entrypoint.…" 3 seconds ago Up 3 seconds 0.0.0.0:88->80/tcp web

step2:查看宿主机映射给容器的端口
[root@xyy admin]#docker port web
80/tcp -> 0.0.0.0:88
[root@xyy admin]#

显示一个容器运行的进程(注意:是宿主机上的进程):top命令

  1. 语法
docker top 容器name/容器ID
  1. 举例
1.查看当前运行的容器
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a2fdda7d1a3 nginx "/docker-entrypoint.…" 23 hours ago Up 23 hours 0.0.0.0:88->80/tcp web

2.查看容器运行的进程:
[root@xyy admin]#docker top web
UID PID PPID C STIME TTY TIME CMD
root 31318 31299 0 Nov23 ? 00:00:00 nginx: master process nginx -g daemon off;
101 31371 31318 0 Nov23 ? 00:00:00 nginx: worker process
101 31372 31318 0 Nov23 ? 00:00:00 nginx: worker process
[root@xyy admin]#ps -ef|grep nginx
root 1038 867 0 12:12 pts/0 00:00:00 grep --color=auto nginx
root 31318 31299 0 Nov23 ? 00:00:00 nginx: master process nginx -g daemon off;
101 31371 31318 0 Nov23 ? 00:00:00 nginx: worker process
101 31372 31318 0 Nov23 ? 00:00:00 nginx: worker process
[root@xyy admin]#ps -ef|grep containerd
root 1040 867 0 12:12 pts/0 00:00:00 grep --color=auto containerd
root 9163 1 0 Nov15 ? 00:26:55 /usr/bin/containerd
root 9382 1 0 Nov15 ? 00:01:29 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 31299 1 0 Nov23 ? 00:00:03 /usr/bin/containerd-shim-runc-v2 -namespace moby -id 4a2fdda7d1a377897230fc7ea658de022c7f06dcfe21924d0a4383e3413d8fe8 -address /run/containerd/containerd.sock

显示容器资源使用统计:stats命令

  1. 语法
1.默认输出
docker stats 容器name/容器ID #注意:默认这个命令是一个stream输出,我们可以加上参数来直接不使用stream进行输出

2.不使用stream流输出
docker stats 容器name/容器ID --no-stream
  1. 举例
1.查看当前容器
[root@xyy admin]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a2fdda7d1a3 nginx "/docker-entrypoint.…" 24 hours ago Up 24 hours 0.0.0.0:88->80/tcp web

2.使用stats命令来显示容器资源使用统计
a.默认输出
[root@xyy ~]#docker stats web
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4a2fdda7d1a3 web 0.00% 1.973MiB / 3.518GiB 0.05% 0B / 0B 0B / 4.1kB 3

b.不使用stream流输出
[root@xyy ~]#docker stats web --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4a2fdda7d1a3 web 0.00% 1.973MiB / 3.518GiB 0.05% 0B / 0B 0B / 4.1kB 3

c.输出容器的cpu使用率
[root@xyy ~]#docker stats web --no-stream|awk '{print $3}'
NAME
0.00%
[root@xyy ~]#docker stats web --no-stream|awk 'NR==2{print $3}'
0.00%

暂停状态:pause命令/unpause

把容器变成一个暂停的状态,然后利用commit再转换成镜像。

[root@myserver ~]# docker pause mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d485b358cc6 nginx "/docker-entrypoint.…" 27 minutes ago Up 5 minutes (Paused) 80/tcp mywebserver
[root@myserver ~]# docker unpause mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d485b358cc6 nginx "/docker-entrypoint.…" 27 minutes ago Up 6 minutes 80/tcp mywebserver
[root@myserver ~]#

重命名:rename命令

docker rename CONTAINER NEW_NAME

[root@myserver ~]# docker rename mywebserver webserver
[root@myserver ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d485b358cc6 nginx "/docker-entrypoint.…" 29 minutes ago Up 8 minutes 80/tcp webserver

关闭容器进程:kill命令

[root@myserver ~]# docker kill webserver
webserver
[root@myserver ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@myserver ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d485b358cc6 nginx "/docker-entrypoint.…" 33 minutes ago Exited (137) 7 seconds ago webserver
[root@myserver ~]#

容器文件系统发生的变化:diff命令

[root@myserver ~]# docker diff webserver
C /etc
C /etc/nginx
C /etc/nginx/conf.d
C /etc/nginx/conf.d/default.conf
C /run
A /run/nginx.pid
C /tmp
A /tmp/hosts
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
A /var/cache/nginx/client_temp

更新容器的配置:update命令

例子:

[root@myserver docker]# docker update --memory 1G --memory-swap 1G  webaserver

[root@myserver docker]# docker stats webaserver
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
bd8375d39858 webaserver 0.00% 2.445MiB / 1GiB 0.24% 3.69kB / 0B

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

🍀 微信公众号 《云原生架构师实战》

🍀 csdn https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

🍀 博客 www.onlyyou520.com

🍀 知乎

https://www.zhihu.com/people/foryouone

🍀 语雀

https://www.yuque.com/books/share/34a34d43-b80d-47f7-972e-24a888a8fc5e?# 《云笔记最佳实践》

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!


0%