# Docker基本命令

# 基本命令

docker version 查看docker各个组件的版本信息

docker version
Client: Docker Engine - Community    # Docker客户端
 Version:           20.10.5
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:33:55 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community   # Docker服务端
 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:                        # containerd
  Version:          1.4.4           # containerd的版本
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:                              # 运行docker的引擎 核心组件
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

docker info 查看docker详细信息

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.5.1-docker)

Server:
 Containers: 34                 # 容器数量
  Running: 16                   # 运行容器数量
  Paused: 0                     # 暂停容器数量
  Stopped: 18                   # 停止容器数量
 Images: 15                     # 镜像数量
 Server Version: 19.03.15
 Storage Driver: overlay2 
 # 存储驱动overlay2的条件:文件系统是xfs并且要支持d_type
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file    
 # docker日志存储类型 存储在本地/var/lib/docker/containers
 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  # inactive没有启动 不使用Swarm进行编排
 Runtimes: runc   # 运行时 runc运行容器的标准 基于runc来创建、运行、销毁等
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc version: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.12-1.el7.elrepo.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.829GiB
 Name: k8s-master01
 ID: AKDR:LB3F:WEOE:RTI3:C7YQ:HAK4:ILLO:NN3F:M2ZB:XHQC:QICG:FCFU
 Docker Root Dir: /var/lib/docker
 # 目录可更改 最好使用单独磁盘 挂载个SSD硬盘 提高性能 
 Debug Mode: false
 Registry: https://index.docker.io/v1/  # 镜像仓库
 Labels:
 Experimental: false
 Insecure Registries:  # 加入不安全的仓库地址 一般私有镜像库是http的
  127.0.0.0/8
 Live Restore Enabled: false  
 # 已启用实时还原标识 打开后重启docker不会重启正在运行的docker
 # Live Restore Enabled 在生产环境中一定要设置为true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

docker search 搜索镜像

docker search alpine
# 选择官方版本OFFICIAL[OK] stars数量最多的
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
alpine                                 A minimal Docker image based on Alpine Linux…   7262      [OK]       
mhart/alpine-node                      Minimal Node.js built on Alpine Linux           482                  
anapsix/alpine-java                    Oracle Java 8 (and 7) with GLIBC 2.28 over A…   469                  [OK]
frolvlad/alpine-glibc                  Alpine Docker image with glibc (~12MB)          256                  [OK]
gliderlabs/alpine                      Image based on Alpine Linux will help you wi…   183                  
alpine/git                             A  simple git container running in alpine li…   171                  [OK]
mvertes/alpine-mongo                   light MongoDB container                         117                  [OK]
yobasystems/alpine-mariadb             MariaDB running on Alpine Linux [docker] [am…   85                   [OK]
alpine/socat                           Run socat command in alpine container           68                   [OK]
kiasaki/alpine-postgres                PostgreSQL docker image based on Alpine Linux   45                   [OK]
davidcaste/alpine-tomcat               Apache Tomcat 7/8 using Oracle Java 7/8 with…   44                   [OK]
jfloff/alpine-python                   A small, more complete, Python Docker image …   40                   [OK]
byrnedo/alpine-curl                    Alpine linux with curl installed and set as …   34                   [OK]
hermsi/alpine-sshd                     Dockerize your OpenSSH-server with rsync and…   33                   [OK]
zenika/alpine-chrome                   Chrome running in headless mode in a tiny Al…   31                   [OK]
hermsi/alpine-fpm-php                  FPM-PHP 7.0 to 8.0, shipped along with tons …   25                   [OK]
etopian/alpine-php-wordpress           Alpine WordPress Nginx PHP-FPM WP-CLI           24                   [OK]
bashell/alpine-bash                    Alpine Linux with /bin/bash as a default she…   18                   [OK]
davidcaste/alpine-java-unlimited-jce   Oracle Java 8 (and 7) with GLIBC 2.21 over A…   13                   [OK]
spotify/alpine                         Alpine image with `bash` and `curl`.            11                   [OK]
roribio16/alpine-sqs                   Dockerized ElasticMQ server + web UI over Al…   11                   [OK]
cfmanteiga/alpine-bash-curl-jq         Docker Alpine image with Bash, curl and jq p…   6                    [OK]
ellerbrock/alpine-mysql-client         MySQL Client based on Alpine Linux              1                    [OK]
bushrangers/alpine-caddy               Alpine Linux Docker Container running Caddys…   1                    [OK]
dwdraju/alpine-curl-jq                 Alpine Docker Image with curl, jq, bash         1                    [OK]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

docker pull拉取镜像到本地

docker pull alpine:latest
latest: Pulling from library/alpine
ba3557a56b15: Pull complete 
Digest: sha256:a75afd8b57e7f34e4dad8d65e2c7ba2e1975c795ce1ee22fa34f8cf46f96a3be
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

docker pull alpine:3.12.0
3.12.0: Pulling from library/alpine
df20fa9351a1: Pull complete 
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:3.12.0
docker.io/library/alpine:3.12.0

docker pull alpine:3.12.0
3.12.0: Pulling from library/alpine
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Image is up to date for alpine:3.12.0
docker.io/library/alpine:3.12.0
# 本地有这个镜像不会重新拉取
docker pull golang:1.16.2-alpine3.13
docker pull openjdk:8-jdk-alpine3.9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

docker tag镜像设置tag

docker tag calico/pod2daemon-flexvol:v3.11.1 yyc/pod2daemon-flexvol:v3.11.1
1

docker push推送镜像到镜像仓库

# 登陆 docker官方仓库
docker login
# 登陆指定仓库
docker login xxx.com
# 推送
docker push yyc/pod2daemon-flexvol:v3.11.1
1
2
3
4
5
6

docker run启动镜像

# 前台启动 交互式启动
docker run -ti alpine:3.12.0 sh
/ # ls
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr
# 后台启动 守护进程启动
docker run -d alpine:3.12.0 sh
1129b682a94befe04acc003fa1f457f8f15198107a2a6937a4976ac7e08bafdf

docker run -tid -p 12345:80 nginx
15e11d4dbafd5d4f1c592ecd41ea8c6021c13057bc7e8e2117c9cbf381476bd5

docker ps -a | grep nginx
15e11d4dbafd   nginx                                                                 "/docker-entrypoint.…"   6 seconds ago    Up 4 seconds                0.0.0.0:12345->80/tcp   optimistic_elbakyan
# 注意在镜像Dockerfile文件里的应用进程一定要是前台启动的
# 因为要是后台启动的话 刚起来容器 就会退出的
# 例如Nginx -g daemon off;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

docker logs查看容器日志

# docker logs -f 容器ID
docker logs -f 容器ID5851ed310c9c
Flag --insecure-port has been deprecated, This flag has no effect now and will be removed in v1.24.
I0323 06:00:11.064111       1 server.go:632] external host was not specified, using 192.168.0.201
I0323 06:00:11.065101       1 server.go:182] Version: v1.20.4
I0323 06:00:11.451149       1 plugins.go:158] Loaded 12 mutating admission controller(s) successfully in the following order: NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,TaintNodesByCondition,Priority,DefaultTolerationSeconds,DefaultStorageClass,StorageObjectInUseProtection,RuntimeClass,DefaultIngressClass,MutatingAdmissionWebhook.
I0323 06:00:11.451197       1 plugins.go:161] Loaded 10 validating admission controller(s) successfully in the following order: LimitRanger,ServiceAccount,Priority,PersistentVolumeClaimResize,RuntimeClass,CertificateApproval,CertificateSigning,CertificateSubjectRestriction,ValidatingAdmissionWebhook,ResourceQuota.
I0323 06:00:11.454594       1 shared_informer.go:240] Waiting for caches to sync for node_authorizer
I0323 06:00:11.455007       1 plugins.go:158] Loaded 12 mutating admission controller(s) successfully in the following order: NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,TaintNodesByCondition,Priority,DefaultTolerationSeconds,DefaultStorageClass,StorageObjectInUseProtection,RuntimeClass,DefaultIngressClass,MutatingAdmissionWebhook.
I0323 06:00:11.455095       1 plugins.go:161] Loaded 10 validating admission controller(s) successfully in the following order: LimitRanger,ServiceAccount,Priority,PersistentVolumeClaimResize,RuntimeClass,CertificateApproval,CertificateSigning,CertificateSubjectRestriction,ValidatingAdmissionWebhook,ResourceQuota.
I0323 06:00:11.461240       1 client.go:360] parsed scheme: "endpoint"
I0323 06:00:11.461419       1 endpoint.go:68] ccResolverWrapper: sending new addresses to cc: [{https://127.0.0.1:2379  <nil> 0 <nil>}]
I0323 06:00:11.471370       1 client.go:360] parsed scheme: "endpoint"
I0323 06:00:11.471447       1 endpoint.go:68] ccResolverWrapper: sending new addresses to cc: [{https://127.0.0.1:2379  <nil> 0 <nil>}]
I0323 06:00:11.483042       1 client.go:360] parsed scheme: "passthrough"
I0323 06:00:11.483366       1 passthrough.go:48] ccResolverWrapper: sending update to cc: {[{https://127.0.0.1:2379  <nil> 0 <nil>}] <nil> <nil>}
I0323 06:00:11.483404       1 clientconn.go:948] ClientConn switching balancer to "pick_first"
I0323 06:00:11.485540       1 client.go:360] parsed scheme: "endpoint"
I0323 06:00:11.485611       1 endpoint.go:68] ccResolverWrapper: sending new addresses to cc: [{https://127.0.0.1:2379  <nil> 0 <nil>}]
I0323 06:00:11.584763       1 instance.go:289] Using reconciler: lease
I0323 06:00:11.586518       1 client.go:360] parsed scheme: "endpoint"
I0323 06:00:11.586590       1 endpoint.go:68] ccResolverWrapper: sending new addresses to cc: [{https://127.0.0.1:2379  <nil> 0 <nil>}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

docker ps查看容器列表

# 查看运行的容器列表
docker ps
# 查看所有容器列表
docker ps -a 
# 查看所有容器ID
docker ps -aq 
1
2
3
4
5
6

docker exec进入容器

# docker exec -ti 容器ID 运行命令
docker exec -ti 517cf88d3af8 bash
1
2

docker cp容器复制命令

# 查看nginx容器
docker ps -a | grep nginx
15e11d4dbafd   nginx                                                                 "/docker-entrypoint.…"   About a minute ago   Up About a minute           0.0.0.0:12345->80/tcp   optimistic_elbakyan
# 进入nginx容器
docker exec -ti 15e11d4dbafd bash
root@15e11d4dbafd:/# pwd
/
root@15e11d4dbafd:/# cd /usr/share/nginx/
root@15e11d4dbafd:/usr/share/nginx# ll
bash: ll: command not found
root@15e11d4dbafd:/usr/share/nginx# ls 
html
root@15e11d4dbafd:/usr/share/nginx# cd html
root@15e11d4dbafd:/usr/share/nginx/html# ls
50x.html  index.html
root@15e11d4dbafd:/usr/share/nginx/html# exit
exit
# 编写index.html
echo "test cp" > index.html
# 复制index.html到容器指定目录下
docker cp index.html 15e11d4dbafd:/usr/share/nginx/html
# 验证页面
curl 192.168.0.201:12345
test cp


# 复制容器下的文件到本地目录
docker cp 15e11d4dbafd:/usr/share/nginx/html/50x.html . 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

docker rmi删除镜像

# 删除镜像 docker rmi 镜像id 可用docker images 查询镜像ID
docker rmi 5296a9
1
2

docker rm删除容器

# 删除容器 docker rm 容器id 可用docker ps -a 查询已经停止的容器ID
docker rm d6355b2122e8
1
2

docker images查看镜像列表

docker images
1

docker stop停止容器

# 停止容器 docker stop 容器id 可用docker ps 查询运行的容器ID
docker stop d6355b2122e8
1
2

docker build创建镜像

# 根据Dockerfile文件创建镜像
docker build -t .
1
2

docker history查看镜像的提交记录

docker history registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
IMAGE          CREATED         CREATED BY                              SIZE      COMMENT
80d28bedfe5d   13 months ago   ENTRYPOINT ["/pause"]                   0B        buildkit.dockerfile.v0
<missing>      13 months ago   ADD bin/pause-amd64 /pause # buildkit   683kB     buildkit.dockerfile.v0
<missing>      13 months ago   ARG ARCH                                0B        buildkit.dockerfile.v0
1
2
3
4
5

docker commit提交镜像

# 提交镜像 docker commit -a 作者 -m 提交描述 镜像id 新的镜像的tags
docker commit -a "yyc" -m "add index.html" 15e11d4dbafd nginx:commit
# 运行 若是容器停止则删除容器不会产生垃圾容器
docker run -ti --rm nginx:commit bash
root@0d1c7c96f4a9:/# cd /usr/share/nginx/html
root@0d1c7c96f4a9:/usr/share/nginx/html# ls
50x.html  index.html
root@0d1c7c96f4a9:/usr/share/nginx/html# cat index.html 
test cp

1
2
3
4
5
6
7
8
9
10