镜像命令

image.png

  1. 0docker version #docekr版本信息
  2. 1docker info #详细的系统信息
  3. 2docker 命令 --help #万能命令
  4. 3dcoker -search 【镜像名】
  5. 4docker -pull 【镜像名】
  6. 5docker rmi -f 镜像id #删除指定的镜像
  7. 6docker rmi -f 镜像id 镜像id #删除多个指定的镜像
  8. 7docker rmi -f ¥(docker images -aq)#删除全部镜像

容器命令

新建容器并启动

  1. docker pull centos #下载一个centos镜像
  2. docker run [可选参数] image
  3. #参数说明
  4. --name="name"
  5. -d 后台方式运行
  6. -it 使用交互方式运行,进入容器查看内容
  7. -p 指定容器的端口 -p 8080:8080
  8. -p ip:主机端口:容器端口(常用)
  9. -p 主机端口:容器端口
  10. -p 容器端口
  11. 容器端口
  12. -p 随机指定端口

从容器退出到主机

exit#退出直接关闭容器

ctrl+P+Q #退出却不关闭容器

列出所有运行的容器

  1. docker ps #列出正在运行的容器
  2. -a #列出所有正在运行的容器+停止运行的容器
  3. -n=? #显示最近创建的容器数
  4. -q #只显示容器的id

删除容器

  1. docker rm 容器id #删除指定的容器,不能删除正在运行的容器
  2. docker rm -f $(docker ps -aq) #删除所有的容器
  3. docker ps -a -q|xargs docker rm #删除所有的容器

启动和停止容器操作

  1. docker start 容器id #启动容器
  2. docker restart 容器id #重启容器
  3. docker stop 容器id #停止当前正在运行的容器
  4. docker kill 容器id #强制停止当前容器

docker start 容器id #启动容器

其他常用命令

后台启动容器

  1. #命令 docker run -d 镜像名
  2. root@linux/】# docker run -d centor
  3. #问题 docker ps,发现centos停止了
  4. #常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止,
  5. #nginx,容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了

查看日志

  1. docker logs -f -t --tail 10 容器id #初始的时候可能没有日志
  2. #自己编写一段shell脚本
  3. docker run -d centos /bin/sh -c "while true;do echo xxx;sleep 1 ;done"
  4. docker ps
  5. CONTAINER ID IMAGE
  6. #容器名 #镜像
  7. # 显示日志
  8. #-tf #显示日志
  9. #--tail number #要显示日志的条数
  10. docker logs tf --tail 10 容器id

查看容器内部中信息

  1. docker top 容器id

查看容器内部中元数据

  1. docker insperct 容器id #查看容器内部信息

进入当前正在运行的容器

  1. # 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
  2. #命令
  3. #方式一
  4. docker exec -it 容器id #进入指定容器内部,开启一个新的终端
  5. #方式二
  6. docker attach 容器id # 正在执行当前代码,显示当前终端,不会启动新的进程
  7. #区别
  8. #docker exec
  9. #docke attach

容器内拷贝文件到主机上

  1. #容器内创建文件
  2. touch /home/test.txt
  3. #文件拷贝
  4. docker cp 容器id :/home/test.txt /home #把容器内部的文件home下的test.txt 拷贝到主机的home文件夹下
  5. #拷贝是一个手动过程,未来我们使用-v卷技术,可以实现,自动同步,

小结

image.png

常用命令排序

  1. attach Attach to a running container #当前 she11下 attach连接指定运行镜像
  2. bild Build an image from a Dockerfile #通过Dockerfile定制镜像
  3. commit Create a new image from a container changes#提交当前容器为新的镜像
  4. cp Copy files/folders from the containers filesystem to the host path #从容器中持贝指定文件或者目录到宿主机中
  5. create Create a new container #创建一个新的容器,同run,但不启动容器
  6. diff Inspect changes on a containers filesystem #查看docker容器变化
  7. events Get real time events from the server #从docker服务获取容器实时事件
  8. exec Run a command in an existing container #在已存在的容器上运行命令
  9. export stream the contents of a container as a tar archive #导出容器的内容流作为一个tar归档文件[对应import]
  10. history show the history of an image #展示一个镜像形成历史
  11. images List images #列出系统当前镜像
  12. import Create a new filesystem image from the contents of a tarball #从tar包中的内容创建一个新的文件系统映像[对应export]
  13. info Display system-wide information#显示系统相关信息
  14. inspect Return low-level information on a container #查看容器详细信息
  15. ki11 Kill a running container #kil1 指定docker容器
  16. 1oad Load an image from a tar archive #从一个tar 包中加载一个镜像[对应 save]
  17. login Register or Login to the docker registry server #注册或者登陆一个docker 源服务器
  18. logout Log out from a Docker registry server #从当前 Docker registry 退出
  19. logs Fetch the logs of a container #输出当前容器日志信息
  20. port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT #查看映射端口对应的容器内部源端
  21. pause Pause all processes within a container #暂停容器
  22. List containers #列出容器列表
  23. pu11 Pu1l an image or a repository from the docker registry server #从docker镜像源服务器拉取指定镜像或者库镜像
  24. push Push an image or a repository to the docker registry server #推送指定镜像或者库镜像至docker源服务器
  25. restart Restart a running container#重启运行的容器
  26. Remove one or more containers #移除一个或者多个容器
  27. rmi Remove one or more images #移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或-f强制删除]
  28. run Run a command in a new container #创建一个新的容器并运行一个命令
  29. save Save an image to a tar archive #保存一个镜像为一个tar包[对应load]
  30. search search for an image on the Docker Hub #在docker hub中搜索镜像
  31. start start a stopped containers #启动容器
  32. stop Stop a running containers #停止容器
  33. tag Tag an image into a repository#给源中镜像打标签
  34. top Lookup the running processes of a container#查看容器中运行的进程信息
  35. unpause Unpause a paused container#取消暂停容器
  36. version show the docker version information#查看docker版本号
  37. wait Block until a container stopsthen print its exit code#截取容器停止时的退出状态值