Docker常用命令

Reads: 581 Edit

@ 1 显示docker的系统信息

(base) MacBook-Pro:hmc-agent xinping$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.9.1)
  compose: Docker Compose (Docker Inc., v2.12.1)
  dev: Docker Dev Environments (Docker Inc., v0.0.3)
  extension: Manages Docker extensions (Docker Inc., v0.2.13)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.21.0)

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 20.10.20
 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: 2
 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.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.49-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.675GiB
 Name: docker-desktop
 ID: 3T7J:4Y2J:MSYE:CAWK:NPQ6:X2DE:YR6J:Q6D2:NVCC:HWTC:7NPU:QKQA
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 49
  Goroutines: 52
  System Time: 2022-11-13T15:15:15.659322985Z
  EventsListeners: 4
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Registry Mirrors:
  https://e29lwva9.mirror.aliyuncs.com/
 Live Restore Enabled: false

启动docker

mac下启动dockr

open /Applications/Docker.app

常用命令

docker version   #显示docker的版本信息
docker info	     #显示docker的系统信息,包括镜像和容器的数量
docker --help     

文档地址: https://docs.docker.com/reference/

port  	  # 查看映射端口对应的容器内部源端口
pause	    # 暂停容器
ps        # 容器列表
pull      # 从docker镜像源服务器拉取指定镜像或者库镜像
push      # 推送指定镜像或者库镜像至docker源服务器
restart   # 重启运行的容器
rm        # 移除一个或多个容器
rmi       # 移除一个或多个镜像 (无容器使用该镜像才可删除,否则需要删除相关容器才可继续或 -f 强制删除)
run       # 创建一个新的容器并运行一个命令
save      # 保存一个镜像为一个 tar 包【对应 load】
search    # 在 docker hub 中搜索镜像
start     # 启动容器
stop      # 停止容器
tag       # 给源中镜像打标签
top       # 查看容器中运行的进程信息
unpause   # 取消暂停容器
version   # 查看 docker版本号
wait      # 截取容器停止时的退出状态值
attach    # Attach to a running container  #当前shell下attach连接指定运行镜像
build     # Build an image from a Dockerfile  #通过Dockerfile定制镜像
commit    # Create a new image from a containers changes  #提交当前容器为新的镜像
cp        # Copy files/folders from a container to a HOSTDIR or to STDOUT  #从容器中拷贝指定文件或者目录到宿主机中
create    # Create a new container  #创建一个新的容器,同run 但不启动容器
diff      # Inspect changes on a containers filesystem  #查看docker容器变化
events    # Get real time events from the server#从docker服务获取容器实时事件
exec      # Run a command in a running container#在已存在的容器上运行命令
export    # Export a containers filesystem as a tar archive  #导出容器的内容流作为一个tar归档文件(对应import)
history   # Show the history of an image  #展示一个镜像形成历史
inspect   # 查看容器详细信息
kill      # kill 指定的容器

镜像命令

docker images

REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
jenkins/jenkins       latest    ba8a0e5dc5cb   4 months ago   570MB
nginx                 latest    d1a364dc548d   4 months ago   133MB
portainer/portainer   latest    580c0e4e98b0   6 months ago   79.1MB
hello-world           latest    d1165f221234   6 months ago   13.3kB

# 解释
REPOSITORY: 镜像仓库源
TAG: 镜像版本信息
IMAGE ID: 镜像的ID
CREATED: 镜像的创建时间
SIZE: 镜像的大小

可选参数
docker images
	-a : (--all) 列出所有镜像
	-q : (--quiet) 只显示镜像ID

搜索镜像

docker search [xxx]

docker search mysql

NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11470     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4354      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   848                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   555       [OK]       
phpmyadmin                        phpMyAdmin - A web interface for MySQL and M…   332       [OK]       
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   91                    [OK]


docker search mysql --filter=STARS=3000 # 搜索start数大于3000的
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11470     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4354      [OK]

拉取镜像

hub 搜索

1037867-20221109170241582-1727611460

docker pull 镜像名[:tag]

docker pull 镜像名[:tag]

# eg
docker pull mysql
Using default tag: latest               # 如果不写tag, 默认就是latest
latest: Pulling from library/mysql
a330b6cecb98: Pull complete 		# 分层下载,docker image 的核心 联合文件系统
9c8f656c32b8: Pull complete 
88e473c3f553: Pull complete 
062463ea5d2f: Pull complete 
daf7e3bdf4b6: Pull complete 
1839c0b7aac9: Pull complete 
cf0a0cfee6d0: Pull complete 
1b42041bb11e: Pull complete 
10459d86c7e6: Pull complete 
b7199599d5f9: Pull complete 
1d6f51e17d45: Pull complete 
50e0789bacad: Pull complete 
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c  # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest				# 真实地址

----

docker pull mysql
等价于
docker pull docker.io/library/mysql:latest

---
# 指定tag
docker pull mysql:5.7
5.7: Pulling from library/mysql
a330b6cecb98: Already exists 			# 联合文件系统,已存在的不再重复下载
9c8f656c32b8: Already exists 
88e473c3f553: Already exists 
062463ea5d2f: Already exists 
daf7e3bdf4b6: Already exists 
1839c0b7aac9: Already exists 
cf0a0cfee6d0: Already exists 
fae7a809788c: Pull complete 			# 下载不存在的
dae5a82a61f0: Pull complete 
7063da9569eb: Pull complete 
51a9a9b4ef36: Pull complete 
Digest: sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

---
#查看
docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
mysql                 5.7       1d7aba917169   3 weeks ago    448MB
mysql                 latest    0716d6ebcc1a   3 weeks ago    514MB
jenkins/jenkins       latest    ba8a0e5dc5cb   4 months ago   570MB
nginx                 latest    d1a364dc548d   4 months ago   133MB
portainer/portainer   latest    580c0e4e98b0   6 months ago   79.1MB
hello-world           latest    d1165f221234   6 months ago   13.3kB

删除镜像

docker rmi -f [镜像名、镜像ID]

ocker rmi -f [镜像名、镜像ID]

docker rmi -f 1d7aba917169
Untagged: mysql:5.7
Untagged: mysql@sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Deleted: sha256:1d7aba9171693947d53f474014821972bf25d72b7d143ce4af4c8d8484623417
Deleted: sha256:94ebbead5c58282fef91cc7d0fb56e4006a72434b4a6ae2cd5be98f369cb8c21
Deleted: sha256:989da5efad29ec59bd536cd393d277bc777f8b9b34b8e3ad9593a4b0a83b40f4
Deleted: sha256:7457ee6817c678da3cb383d27a3d79d5f3f25fbcb92958d5e8d5709e7631e23c
Deleted: sha256:fe7dac53adebe33519b4e4fc577bfcddd7372cc313c35fae681fc82fb325fdc0

容器命令

有了镜像才可以创建容器,下载centos镜像进行学习

docker pull centos

新建容器并启动

ocker run [可选参数] imageName/imageId
# 参数说明
--name "NAME"  容器名字,tomcat01, tomcat02, 用来区分容器
-d  后台方式运行
-i  , -t   #使用交互模式运行,可以进入容器查看内容
-p         #指定容器端口 -p 8080   
	   -p ip:主机端口:容器端口  -p 3344:80
	   -p 主机端口:容器端口   (常用)
           -p 容器端口
           容器端口
-P  	   #随机指定端口

启动容器,并进入容器

[root@iZm5e----- ~]# docker run -it centos /bin/bash
[root@c4a1504a9cf9 /]# ls  #查看容器内的centos
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@c4a1504a9cf9 /]# exit # 退出容器
exit

查看所有运行的容器

docker ps     # 查看运行的容器  exit之后就停止了容器
docker ps -a  # 查看所有运行过的以及在运行的容器

docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS                      PORTS     NAMES
c4a1504a9cf9   centos                "/bin/bash"              7 minutes ago   Exited (0) 3 minutes ago              hungry_euler
dc0c5076048e   hello-world           "/hello"                 2 months ago    Exited (0) 2 months ago               busy_cori


docker ps -a -n=[数字]  # 最近创建的容器
docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                     PORTS     NAMES
c4a1504a9cf9   centos    "/bin/bash"   11 minutes ago   Exited (0) 7 minutes ago             hungry_euler

退出容器

exit          #容器停止并退出
ctrl + p + q  #容器不停止退出

删除容器

docker rm 容器ID                  # 删除指定容器  不能删除正在运行的容器
docker rm -f $(docker ps -aq)     # 递归删除所有容器
docker ps -a -q|xargs docker rm   # 删除所有容器

启动和停止容器

docker start 容器ID	# 启动容器
docker restart 容器ID 	# 重启容器
docker stop 容器ID      # 停止当前正在运行的容器
docker kill 容器ID      # 强制停止当前容器

进入当前正在运行的容器

docker exec -it 容器ID bashShell (常用)

docker exec -it d5033adf13b4 /bin/bash
[root@d5033adf13b4 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@d5033adf13b4 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 07:01 ?        00:00:00 /bin/sh -c while true;do echo xiao;sleep 1;done
root      1895     0  0 07:33 pts/0    00:00:00 /bin/bash
root      1937     1  0 07:33 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root      1938  1895  0 07:33 pts/0    00:00:00 ps -ef
[root@d5033adf13b4 /]# 

docker attach 容器ID

docker attach d5033adf13b4

  • docker exec # 进入容器后,开启一个新的终端,可以在里面操作
  • docker attach # 进入容器正在执行的终端,不会开启新的进程。

Comments

Make a comment

www.ultrapower.com ,王硕的博客,专注于研究互联网产品和技术,提供中文精品教程。 本网站与其它任何公司及/或商标无任何形式关联或合作。