Docker常用命令

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

  1. # 查看docker版本  
  2. $docker version  
  3. #如果没有显示server的版本,请启动docker服务
  4. service docker start
  5.   
  6. # 显示docker系统的信息  
  7. $docker info 

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

  1. # 检索image  
  2. $docker search image_name  
  3.   
  4. # 下载image  
  5. $docker pull image_name  
  6.   
  7. # 列出镜像列表; -a, --all=false Show all images; -q, --quiet=false Only show numeric IDs  
  8. $docker images  
  9.   
  10. # 删除一个或者多个镜像; -f, --force=false Force; --no-prune=false Do not delete untagged parents  
  11. $docker rmi image_ID
  12. #要删除的镜像不能有任何镜像或者容器依赖它,如果有请先删除它们
  13.   
  14. # 显示一个镜像的历史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
  15. $docker history image_name 

3. 启动容器(run)

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

  1. # 在容器中运行"echo"命令,输出"hello word"  
  2. $docker run image_name echo "hello word"  
  3.   
  4. # 交互式进入容器中(exit退出),用户退出后容器也停止
  5. $docker run -it image_name /bin/bash  
  6.   
  7. # 以后台方式启动容器  -d
  8. $docker run [options] -d image_name
  9. # 启动容器时的附加选项
  10. --name string    指定容器启动后的名称
  11. -p host_port:contaier_port    将容器的端口映射为主机的某个端口,类似于NAT转换
  12. -v host_dir_or_file:contaier_dir_or_file    将主机的某个文件或文件夹挂在到容器中,使得容器可以访问该文件。-v可以有多个
  13. -d 后台方式启动
  14. # 在容器中安装新的程序  
  15. $docker run image_name apt-get install -y app_name  

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

4. 查看容器(ps)

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

5. 保存对容器的修改(commit

  1. # 保存对容器的修改; -a, --author="" Author; -m, --message="" Commit message  
  2. $docker commit -a 'Author' -m 'input your image message' contaier_name image_name[:tag]
  3. example: $docker commit -a 'yyq' -m 'nginx picture server' mynginx picture_server:v1.0

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

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

[plain] view plain copy
  1. # 删除所有容器  
  2. $docker rm $(docker ps -aq) 
  3.   
  4. # 删除单个容器; -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  
  5. $docker rm Name/ID  
  6.   
  7. # 停止、启动、杀死一个容器  
  8. $docker stop Name/ID  #推荐这种形式关闭
  9. $docker start Name/ID  
  10. $docker kill Name/ID  
  11.   
  12. # 从一个容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps  
  13. $docker logs Name/ID  
  14.   
  15. # 列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的  
  16. $docker diff Name/ID  
  17.   
  18. # 显示一个运行的容器里面的进程信息  
  19. $docker top Name/ID  
  20.   
  21. # 从容器里面拷贝文件/目录到本地一个路径  
  22. $docker cp Name:/container_path to_path  
  23. $docker cp ID:/container_path to_path  
  24.   
  25. # 重启一个正在运行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10  
  26. $docker restart Name/ID  
  27.   
  28. # 附加到一个运行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process  
  29. $docker attach ID  

Note: attach命令允许你查看或者影响一个运行的容器。你可以在同一时间attach同一个容器。你也可以从一个容器中脱离出来,是从CTRL-C。

7. 保存和加载镜像(save、load)

当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。

[plain] view plain copy
  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.   
  6. # 机器a  
  7. $docker save image_name > /home/save.tar  
  8. # 使用scp将save.tar拷到机器b上,然后:  
  9. $docker load < /home/save.tar  

8、 登录registry server(login)

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

9. 发布image(push)

[plain] view plain copy
  1. # 发布docker镜像  
  2. $docker push new_image_name  

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

[plain] view plain copy
  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  

转自 https://www.cnblogs.com/webyyq/p/8890655.html

原文地址:https://www.cnblogs.com/go2anywhere/p/11235949.html