docker常用命令
1.镜像命令:
docker images
[root@suaxi ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 12 days ago 13.3kB
REPOSITORY --仓库源
TAG --标签
IMAGE ID --镜像id
CREATED --创建时间
SIZE --大小
-a, --all # 显示所有镜像
-f, --filter filter # Filter output based on conditions provided
--format string # Pretty-print images using a Go template
--no-trunc # Don't truncate output
-q, --quiet # 只显示id
docker search 搜索
[root@suaxi ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10624 [OK]
mariadb MariaDB Server is a high performing open sou… 3982 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 779 [OK]
# 可选项 通过搜索来过滤
# 过滤STARS大于3000的镜像
docker search mysql --filter=STARS=3000
docker pull 拉取(下载)
# 下载镜像
docker pull 镜像名[:tag]
[root@suaxi ~]# docker pull mysql
Using default tag: latest # 不写tag,默认拉取最新版
latest: Pulling from library/mysql
a076a628af6f: Pull complete # 分层下载
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
docker pull mysql 等价于 docker.io/library/mysql:latest
# 指定版本下载
[root@suaxi ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists # 分层文件系统,已经存在的版本不重复下载
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
[root@suaxi ~]# docker rmi -f 容器id #删除指定容器id的镜像
[root@suaxi ~]# docker rmi -f 容器id 容器id 容器id #删除多个指定容器id的镜像
[root@suaxi ~]# docker rmi -f $(docker images -aq) #删除全部镜像
2.容器命令
docker run [可选参数] image
# 参数说明
--name="name" 容器名字
-d 后台方式运行
-it 交互方式运行,进入容器查看内容
-p 端口号 -p 8088:8088
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-p 随机指定端口
测试
# 启动并进入容器
[root@suaxi ~]# docker run -it centos /bin/bash
[root@1c4af5d6c9fd /]# ls #查看容器中的centos(基础版本,功能不完善)
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1c4af5d6c9fd /]# exit
exit
[root@suaxi /]# ls # 退出容器,回到主机(完整的centos)
bin boot CloudResetPwdUpdateAgent CloudrResetPwdAgent dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
列出所有运行中的容器
# 参数说明
# docker ps 列出当前正在运行的容器
-a # 当前正在运行的容器 + 历史运行的容器
-n=? # 显示最近创建的容器
-q # 只显示容器编号
[root@suaxi ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@suaxi ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c4af5d6c9fd centos "/bin/bash" 8 minutes ago Exited (0) 5 minutes ago bold_pascal
705a3df22c9b hello-world "/hello" About an hour ago Exited (0) About an hour ago sharp_jepsen
d978003a153b d1165f221234 "/hello" 22 hours ago Exited (0) 22 hours ago beautiful_engelbart
45a55574058f d1165f221234 "/hello" 27 hours ago Exited (0) 27 hours ago charming_cerf
938e1fed5677 d1165f221234 "/hello" 27 hours ago Exited (0) 27 hours ago compassionate_matsumoto
[root@suaxi ~]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c4af5d6c9fd centos "/bin/bash" 8 minutes ago Exited (0) 6 minutes ago bold_pascal
[root@suaxi ~]# docker ps -aq
1c4af5d6c9fd
705a3df22c9b
d978003a153b
45a55574058f
938e1fed5677
退出容器
exit # 停止并退出
ctrl + P + Q # 退出,但容器不停止
删除容器
docker rm 容器id # 删除指定的容器(不能删除正在运行的,强制删除运行中的容器,需加docker rm -f)
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 # 强制停止
3.常用其他命令
后台启动容器
# docker run -d 镜像名
[root@suaxi ~]# docker run -d centos
注:docker容器使用后台运行,必须要有一个前台进程,如果docker发现没有运行的应用,就会自动停止;
同理nginx容器启动后,发现没有提供服务,就会立刻停止
查看日志
docker logs -tf --tail [number]
# 参数说明
-tf # 带时间戳、行数显示日志
--tail [number] # 查看几行日志
[root@suaxi ~]# docker run -d centos /bin/sh -c "while true;do echo hello;sleep 2;done"
bb0ac965992f3a0d14c7198a79ada368bbb3331da7bfca88d4e235eb4482a3b5
[root@suaxi ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb0ac965992f centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds nice_hermann
[root@suaxi ~]# docker logs -tf --tail 5 bb0ac965992f
2021-03-20T15:15:58.658886157Z hello
2021-03-20T15:16:00.660610164Z hello
2021-03-20T15:16:02.662305052Z hello
2021-03-20T15:16:04.663980021Z hello
2021-03-20T15:16:06.666010717Z hello
查看容器中的进程信息
[root@suaxi ~]# docker top bb0ac965992f
UID PID PPID C STIME TTY TIME CMD
root 21507 21487 0 23:15
root 21901 21507 0 23:20
查看镜像的元数据
docker inspect 容器id
进入当前正在运行的容器
# 方式一:
docker exec -it 容器id bashshell
[root@suaxi ~]# docker exec -it bb0ac965992f bin/bash
[root@bb0ac965992f /]# uname -a
Linux bb0ac965992f 3.10.0-1160.15.2.el7.x86_64 #1 SMP Wed Feb 3 15:06:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@bb0ac965992f /]# uptime
15:28:08 up 3 days, 14:33, 0 users, load average: 0.00, 0.02, 0.05
# 方式二:
docker attach 容器id
# docker exec # 进入容器后开启一个新的终端(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器中拷贝文件到主机
docker cp 容器id:容器内路径 主机路径
# 启动容器
[root@suaxi ~]# docker run -it centos
[root@112a084e0bd3 /]# cd /home
[root@112a084e0bd3 home]# ls
# 创建一个新的文件
[root@112a084e0bd3 home]# touch sunxiaochuan.txt
[root@112a084e0bd3 home]# exit
exit
[root@suaxi ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
112a084e0bd3 centos "/bin/bash" 36 seconds ago Exited (0) 8 seconds ago sleepy_tereshkova
# 拷贝文件到主机
[root@suaxi ~]# docker cp 112a084e0bd3:/home/sunxiaochuan.txt /home
[root@suaxi ~]# cd /home
[root@suaxi home]# ls
sunxiaochuan.txt test www
[root@suaxi home]#
4.docker常用命令小结
attach Attach to a running container #当前shell下attach连接指定运行镜像
build Build an image from a Dockerfile #通过Dockerfile定制镜像
commit Create a new image from a container's 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 container's filesystem #查看docker容器变化
events Get real time events from the server#从docker服务获取容器实时事件
exec Run a command in a running container#在已存在的容器上运行命令
export Export a container's filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import)
history Show the history of an image #展示一个镜像形成历史
images List images #列出系统当前镜像
import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export)
info Display system-wide information #显示系统相关信息
inspect Return low-level information on a container or image #查看容器详细信息
kill Kill a running container #kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save)
login Register or log in to a Docker registry#注册或者登陆一个docker源服务器
logout Log out from a Docker registry #从当前Docker registry退出
logs Fetch the logs of a container #输出当前容器日志信息
pause Pause all processes within a container#暂停容器
port List port mappings or a specific mapping for the CONTAINER #查看映射端口对应的容器内部源端口
ps List containers #列出容器列表
pull Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器
rename Rename a container #重命名容器
restart Restart a running container #重启运行的容器
rm Remove one or more containers #移除一个或者多个容器
rmi Remove one or more images #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
run Run a command in a new container #创建一个新的容器并运行一个命令
save Save an image(s) to a tar archive#保存一个镜像为一个tar包(对应load)
search Search the Docker Hub for images #在docker
hub中搜索镜像
start Start one or more stopped containers#启动容器
stats Display a live stream of container(s) resource usage statistics #统计容器使用资源
stop Stop a running container #停止容器
tag Tag an image into a repository #给源中镜像打标签
top Display the running processes of a container #查看容器中运行的进程信息
unpause Unpause all processes within a container #取消暂停容器
version Show the Docker version information#查看容器版本号
wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值
评论 (0)