docker version :显示 Docker 版本信息。
docker version
例如:
# docker version
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.40
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:05:12 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 19.03.15
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 99e3ed8919
Built: Sat Jan 30 03:16:33 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.7
GitCommit: 0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
runc:
Version: 1.1.3
GitCommit: v1.1.3-0-g6724737
docker-init:
Version: 0.18.0
GitCommit: fec3683
docker info : 显示 Docker 系统信息,包括镜像和容器数。
docker info
例如:
# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 86
Running: 47
Paused: 0
Stopped: 39
Images: 22
Server Version: 19.03.15
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: systemd
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: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
runc version: v1.1.3-0-g6724737
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.622GiB
Name: k8s-master
ID: IVPW:EA6H:6CKC:3HN3:7WUK:LC4G:WKBH:IRMJ:5QNN:XQZP:HB5C:PQOF
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: wangjinxiong
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://v16stybc.mirror.aliyuncs.com/
Live Restore Enabled: false
docker search : 从Docker Hub查找镜像
docker search 镜像名
例如:
# docker search centos
相关视频:https://www.ixigua.com/7140605452999787021?logTag=b15eb22303e62ed604b8
docker pull : 从镜像仓库中拉取或者更新指定镜像
docker pull 镜像名:版本号
例如:
docker pull centos:7
docker push : 将本地的镜像上传到镜像仓库,要先登陆到镜像仓库。
docker push 仓库名称/镜像名:版本号
例如,登录公有仓库可以不带地址。
# 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: wangjinxiong
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
# docker pull wangjinxiong/nginx:tools
相关视频: https://www.ixigua.com/7141005279504728607?logTag=f8f3925e38436c539e9c
docker run :创建一个新的容器并运行一个命令
docker run [OPTIONS] 镜像名
OPTIONS说明:
例如:
docker run -d -it -p 8801:8081 -p 5000:5000 --name nexus -v /home/nexus/nexus-data:/nexus-data --restart=always sonatype/nexus3
相关视频:https://www.ixigua.com/7141175079999635975?logTag=a8b15fbfc3b8bc3da59f
https://www.ixigua.com/7140606262332523044?id=7141650019991585293&logTag=5b26a9505b76f6bd41d0
docker logs : 获取容器的日志
docker logs [OPTIONS] 容器名或者容器ID
OPTIONS说明:
例如:
docker logs --since "2022-09-03" 84cf465565fd
相关视频:https://www.ixigua.com/7142125502529339911?logTag=d55cafb039ee756e37fa
docker ps : 列出容器
docker ps [OPTIONS]
OPTIONS说明:
例子:
列出所有在运行的容器信息。
$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
09b93464c2f7 nginx:latest "nginx -g 'daemon off" ... 80/tcp, 443/tcp myedu.jb51.net
96f7f14e99ab mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
列出最近创建的5个容器信息。
$ docker ps -n 5
CONTAINER ID IMAGE COMMAND CREATED
09b93464c2f7 nginx:latest "nginx -g 'daemon off" 2 days ago ...
b8573233d675 nginx:latest "/bin/bash" 2 days ago ...
b1a0703e41e7 nginx:latest "nginx -g 'daemon off" 2 days ago ...
f46fb1dec520 5c6e1090e771 "/bin/sh -c 'set -x \t" 2 days ago ...
a63b4a5597de 860c279d2fec "bash" 2 days ago ...
列出所有创建的容器ID。
$ docker ps -a -q
09b93464c2f7
b8573233d675
b1a0703e41e7
f46fb1dec520
a63b4a5597de
6a4aa42e947b
de7bb36e7968
43a432b73776
664a8ab1a585
ba52eb632bbd
...
相关视频:https://www.ixigua.com/7142332379724513800?logTag=863fd9e0fc847ee53da2
docker exec :在运行的容器中执行命令
docker exec [OPTIONS] 容器名称或者容器ID /bin/bash或者sh shell脚本(可选)
OPTIONS说明:
例子:
在容器 centos7中以交互模式执行容器内 /hello.sh 脚本:
# docker exec -it centos7 /bin/bash hello.sh
hello world
在容器 mynginx 中开启一个交互模式的终端:
[root@k8s-master ~]# docker exec -it centos7 /bin/bash
[root@f180fe9ec874 /]#
相关视频: https://www.ixigua.com/7143589639704543775?logTag=c9edb3b9108afabf13ec
docker cp :用于容器与主机之间的数据拷贝。
# 从容器拷贝文件或者目录到宿主机
docker cp CONTAINER:SRC_PATH 宿主机目录
# 从宿主机拷贝文件或者目录到容器
docker cp 宿主机目录 CONTAINER:SRC_PATH
例子:
将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。
docker cp /www/runoob 96f7f14e99ab:/www/
将主机/www/runoob目录拷贝到容器96f7f14e99ab中,目录重命名为www。
docker cp /www/runoob 96f7f14e99ab:/www
将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。
docker cp 96f7f14e99ab:/www /tmp/
docker rmi : 删除本地一个或多个镜像。
docker rmi 镜像名
例子:
# docker rmi wangjinxiong/redis:tools
Untagged: wangjinxiong/redis:tools
Untagged: wangjinxiong/redis@sha256:ecdb394dacb443884e8cf05616aef26a7b81104370af3d686ba38e7470376444
Deleted: sha256:1e26be86b65f2c58f349110f0e38f0978c70ede89fdaad2b53ff21f86b6fd0c5
Deleted: sha256:5590a62fa149dcc7f7c99b3e80624e4bdcda98d0664a0fc456221e70e16e486d
docker rm :删除一个或多个容器。 删除一个容器时要先停止容器。
# docker stop nginx0904 && docker rm nginx0904
nginx0904
nginx0904
docker tag : 标记本地镜像,将其归入某一仓库。
docker tag 源镜像名:tag REGISTRYHOST/目标镜像名:tag
例子:
busybox:1.28.4 wangjinxiong/busybox
docker images : 列出本地镜像。
docker images [OPTIONS] [REPOSITORY/镜像名:TAG]
OPTIONS说明:
例子
查看本地镜像列表。
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wangjinxiong/nginx tools 79b2d70f65d0 15 hours ago 182MB
nginx <none> 2b7d6430f78d 12 days ago 142MB
nginx latest 605c77e624dd 8 months ago 141MB
docker start :启动一个或多个已经被停止的容器
docker stop :停止一个运行中的容器
docker restart :重启容器
docker start 容器名或者容器ID
docker stop 容器名或者容器ID
docker restart 容器名或者容器ID
例子:
启动已被停止的容器nginx0905
docker start nginx0905
停止运行中的容器nginx0905
docker stop nginx0905
重启容器nginx0905
docker restart nginx0905
docker build 命令用于使用 Dockerfile 创建镜像。
docker build [OPTIONS] PATH | URL | -
OPTIONS说明:
例子:
使用当前目录的 Dockerfile 创建镜像,标签为 wangjinxiong/nginx:tools
docker build -t wangjinxiong/nginx:tools .
也可以通过 -f Dockerfile 文件的位置:
docker build -f /path/to/a/Dockerfile .
docker history : 查看指定镜像的创建历史。
docker history [OPTIONS] 镜像名
OPTIONS说明:
# docker history calico/kube-controllers:v3.15.5
IMAGE CREATED CREATED BY SIZE COMMENT
bc6083995e03 16 months ago /bin/sh -c #(nop) ENTRYPOINT ["/usr/bin/kub… 0B
<missing> 16 months ago /bin/sh -c #(nop) ADD file:ef5caf8cb05bdd166… 3.14MB
<missing> 16 months ago /bin/sh -c #(nop) ADD file:e7ce0b4cf3402a8e1… 49.7MB
<missing> 16 months ago /bin/sh -c #(nop) COPY dir:ab6ac5446356fc7b8… 11.4kB
<missing> 16 months ago /bin/sh -c #(nop) LABEL name=Calico Kuberne… 0B
<missing> 16 months ago /bin/sh -c #(nop) ARG GIT_VERSION 0B
docker commit :从容器创建一个新的镜像。
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS说明:
将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。
docker commit -a "runoob.com" -m "my apache" a404c6c174a2 mymysql:v1