Java基础、中级、高级、架构面试资料

从入门到跑路总结出了这套 Docker 常用的命令大全

业余杂谈 herman 1814浏览
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

docker 已经不在是一个陌生的词汇了,程序员们已经越来越离不开它了。现在,你出去面试,说不会 docker,立马就被 pass 掉了。

docker 很重要,但是它里面有很多命令不常用,所以我时常会忘记。今天抽空总结一下,把 docker 常见的命令列出来,方便以后查看。

docker 常用命令大全
# 查看docker版本
$docker version
# 显示docker系统的信息
$docker info

# 对 image 镜像操作相关(search、pull、images、rmi、history)
# 检索image
$docker search image_name
# 下载image
$docker pull image_name
# 列出镜像列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
$docker images
# 删除一个或者多个镜像; -f, --force=false Force; --no-prune=false Do not delete untagged parents
$docker rmi image_name
# 显示一个镜像的历史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
$docker history image_name

# 查看容器
# 列出当前所有正在运行的container
$docker ps
# 列出所有的container
$docker ps -a
# 列出最近一次启动的container
$docker ps -l

# 启动容器相关
# 在容器中运行"echo"命令,输出"hello word"
$docker run image_name echo "hello word"
# 交互式进入容器中
$docker run -i -t image_name /bin/bash
# 在容器中安装新的程序
$docker run image_name apt-get install -y app_name
# 执行上面的命令需要注意,在执行apt-get 命令的时候,要带上-y参数。如果不指定-y参数的话,apt-get命令会进入交互模式,需要用户输入命令来进行确认,但在docker环境中是无法响应这种交互的。apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

# 保存对容器的修改 commit
# 保存对容器的修改; -a, --author="" Author; -m, --message="" Commit message
$docker commit ID new_image_name
#image相当于类,container相当于实例,不过可以动态给实例安装新软件,然后把这个container用commit命令固化成一个image。

#对容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)
# 删除所有容器
$docker rm `docker ps -a -q`
# 删除单个容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container
$docker rm Name/ID
# 停止、启动、杀死一个容器
$docker stop Name/ID
$docker start Name/ID
$docker kill Name/ID
# 从一个容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps
$docker logs Name/ID
# 列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的
$docker diff Name/ID
# 显示一个运行的容器里面的进程信息
$docker top Name/ID
# 从容器里面拷贝文件/目录到本地一个路径
$docker cp Name:/container_path to_path
$docker cp ID:/container_path to_path
# 重启一个正在运行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10
$docker restart Name/ID
# 附加到一个运行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process
$docker attach ID
# attach命令允许你查看或者影响一个运行的容器。你可以在同一时间attach同一个容器。你也可以从一个容器中脱离出来,是从CTRL-C。

#保存和加载镜像(save、load)。当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。
# 保存镜像到一个tar包; -o, --output="" Write to an file
$docker save image_name -o file_path
# 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file
$docker load -i file_path
$docker save image_name > /home/save.tar
# 使用scp将save.tar拷到机器b上,然后:
$docker load < /home/save.tar

#登录registry server(login)
# 登陆registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username
$docker login

#发布image(push)
# 发布docker镜像
$docker push new_image_name

# 根据Dockerfile 构建出一个容器
# build
--no-cache=false Do not use cache when building the image
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build
-t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success
$docker build -t image_name Dockerfile_path

#docker 的网络命令
docker network create
docker network connect
docker network ls 
docker network rm
docker network disconnect
docker network inspect

#导入镜像
sudo docker load --input ubuntu_14.04.tar
#导出镜像
sudo docker save -o /home/user/images/ubuntu_14.04.tar ubuntu:14.04

#查看所有启动命令
docker inspect container 
#查看容器运行日志
docker logs --tail="10" mytest
#查看容器ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
#实时查看docker容器名为xttblog的最后10行日志
sudo docker logs -f -t --tail 10 xttblog
#Dockers 服务开机启动
sudo systemctl enable docker.service

#开启/停止/重启container(start/stop/restart)
CONTAINER_ID=$(docker start <containner_id>)
docker stop $CONTAINER_ID
docker restart $CONTAINER_ID

上面的 docker 命令还不够完整,后面想到或者遇到了再补充!

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » 从入门到跑路总结出了这套 Docker 常用的命令大全