docker之常用命令整理

1. 查看docker信息(version、info)

 
 
  1. # 查看docker版本
  2. $docker version
  3. # 显示docker系统的信息
  4. $docker info

2. 对image的操作(search、pull、images、rmi、history)

 
 
  1. # 检索image
  2. $docker search image_name
  3. # 下载image
  4. $docker pull image_name
  5. # 列出镜像列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric
  6.  
  7. IDs
  8. $docker images
  9. # 删除一个或者多个镜像; -f, --force=false Force; --no-prune=false Do not delete untagged parents
  10. $docker rmi image_name
  11. # 显示一个镜像的历史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
  12. $docker history image_name

3. 启动容器(run)
docker容器可以理解为在沙盒中运行的进程。这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。

 
 
  1. # 在容器中运行echo命令,输出hello word
  2. $docker run image_name echo hello word
  3. # 交互式进入容器中
  4. $docker run -i -t image_name bash
  5. #退出容器:exit
  6. # 在容器中安装新的程序(前提:容器能够联网)
  7. $docker run image_name apt-get install -y app_name

Note: 在执行apt-get 命令的时候,要带上-y参数,如果不指定-y参数的话,apt-get命令会进入交互模式,需要用户输入命令来进行确认,但在docker环境中是无法响应这种交互的。apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

4. 查看容器(ps)

 
 
  1.  
  2. # 列出当前所有正在运行的container
  3. $docker ps
  4. # 列出所有的container
  5. $docker ps -a
  6. # 列出最近一次启动的container
  7. $docker ps -l

5. 保存对容器的修改(commit)
当你对某一个容器做了修改之后(通过在容器中运行某一个命令),可以把对容器的修改保存下来,这样下次可以从保存后的最新状态运行该容器。

 
 
  1. # 保存对容器的修改; -a, --author= Author; -m, --message= Commit message
  2. $docker commit ID new_image_name

Note: image相当于类,container相当于实例,不过可以动态给实例安装新软件,然后把这个container用commit命令固化成一个image。

6. 对容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach、exec、exit)

 
 
  1. # 删除所有容器
  2. $docker rm `docker ps -a -q`
  3. # 删除单个容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --
  4.  
  5. volumes=false Remove the volumes associated to the container
  6. $docker rm NameID
  7. # 停止、启动、杀死一个容器
  8. $docker stop NameID
  9. $docker start NameID
  10. $docker kill NameID
  11. # 从一个容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps
  12. $docker logs NameID
  13. # 列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的
  14. $docker diff NameID
  15. # 显示一个运行的容器里面的进程信息
  16. $docker top NameID
  17. # 从容器里面拷贝文件目录到本地一个路径
  18. $docker cp Namecontainer_path to_path
  19. $docker cp IDcontainer_path to_path
  20. # 重启一个正在运行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10
  21. $docker restart NameID
  22. # 附加到一个运行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process
  23.  
  24. # 重新进入到正在运行的容器
  25. $docker attach ID
  26.  
  27. # 在docker中给容器运行一些进程:后台任务
  28. # touch创建/tmp/test.txt
  29. $docker exec -d IED touch /tmp/laofuxi.com.txt
  30. # 在docker中给容器运行一些进程:交互式任务
  31. # 交互式任务,进入容器打开shell
  32. $docker exec -t -i ID /bin/bash
  33. # 退出容器,返回host,这个时候,容器会停止运行,但容器仍然存在,可以使用start重新启动
  34. $exit

Note: attach命令允许你查看或者影响一个运行的容器。
你可以在同一时间attach同一个容器。
你也可以从一个容器中脱离出来,同时按下CTRL +P +Q(不结束容器的进程)。

7. 保存和加载镜像(save、load)
当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。

 
 
  1. # 保存镜像到一个tar包; -o, --output= Write to an file
  2. $docker save image_name -o file_path
  3. # 加载一个tar包格式的镜像; -i, --input= Read from a tar archive file
  4. $docker load -i file_path
  5. # 机器a
  6. $docker save image_name homesave.tar
  7. # 使用scp将save.tar拷到机器b上,然后:
  8. $docker load homesave.tar

8、 登录registry server(login)

 
 
  1. # 登陆registry server; -e, --email= Email; -p, --password= Password; -u, --username= Username
  2. $docker login

9. 发布image(push)

 
 
  1. # 发布docker镜像
  2. $docker push new_image_name

10. 根据Dockerfile 构建出一个容器

 
 
  1. #build
  2. --no-cache=false Do not use cache when building the image
  3. -q, --quiet=false Suppress the verbose output generated by the containers
  4. --rm=true Remove intermediate containers after a successful build
  5. -t, --tag= Repository name (and optionally a tag) to be applied to the resulting image in case of success
  6. $docker build -t image_name Dockerfile_path





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值