常用命令

docker run

命令 说明
-a 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
-d 后台运行容器,并返回容器ID
-i 以交互模式运行容器,通常与 -t 同时使用
-p(小写) 端口映射,格式为:主机(宿主)端口:容器端口
-P(大写) 随机映射一个主机(宿主)的端口到内部容器网络端口
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用
—name 为容器指定一个名称,—name=”y-tag”
—dns 指定容器使用的DNS服务器,默认和宿主一致,—dns 8.8.8.8
-h 指定容器的hostname
-e 设置环境变量
-m 设置容器使用内存最大值
—cpuset 绑定容器到指定CPU运行,cpuset=”0,1,2”
—net 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型,—net=”bridge”
—link 添加链接到另一个容器
—expose 开放一个端口或一组端口
—env-file=[] 从指定文件读入环境变量
—rm 这个参数是说容器退出后随之将其删除。默认情况下,为了排障需 求,退出的容器并不会立即删除,除非手动 docker rm, 我们这里只是随便 执行个命令,看看结果,不需要排障和保留结果,因此使用 —rm 可以避免 浪费空间。

docker exec -it

  1. $ eg:
  2. $ docker exec -it yjct-tag /bin/bash
  3. OPTIONS说明:
  4. -d : 分离模式: 在后台运行
  5. -i : 即使没有附加也保持STDIN 打开
  6. -t : 分配一个伪终端

通过exit退出当前交互式容器
-c可以在容器外执行-c 后面的shell命令

  1. docker exec -it go /bin/bash -c "go --help"
  2. Go is a tool for managing Go source code.
  3. Usage:
  4. go <command> [arguments]
  5. The commands are:
  6. bug start a bug report
  7. build compile packages and dependencies
  8. clean remove object files and cached files
  9. doc show documentation for package or symbol
  10. env print Go environment information
  11. fix update packages to use new APIs
  12. fmt gofmt (reformat) package sources
  13. generate generate Go files by processing source
  14. get add dependencies to current module and install them
  15. install compile and install packages and dependencies
  16. list list packages or modules
  17. mod module maintenance
  18. run compile and run Go program
  19. test test packages
  20. tool run specified go tool
  21. version print Go version
  22. vet report likely mistakes in packages
  23. Use "go help <command>" for more information about a command.
  24. Additional help topics:
  25. buildmode build modes
  26. c calling between Go and C
  27. cache build and test caching
  28. environment environment variables
  29. ...

docker create

与docker run 区别:docker run=docker create + docker start

docker ps

docker inspect

  1. docker inspect [NAME]/[CONTAINER ID]
  2. eg
  3. docker inspect nginx

docker logs

查看守护容器后台日志,-t显示时间 -f更新日志记录 —tail 显示日志数量—tail 0 显示最新的日志记录
注意:对于运行很久的服务千万不要 docker logs -f ,一定加—tail 。
OPTIONS说明:

  • -f : 跟踪日志输出
  • —since :显示某个开始时间的所有日志
  • -t : 显示时间戳
  • —tail :仅列出最新N条容器日志

    docker attach

    与exec 的区别 :Docker attach可以attach到一个已经运行的容器的stdin,然后进行命令执行的动作。
    但是需要注意的是,如果从这个stdin中exit,会导致容器的停止。exec 不会。
    1. docker attach 容器ID/容器名

    docker commit

    docker commit 命令,可以将容器的存储层保存下来成为镜像。换句话说,就是在原有镜像的基础上,再叠加上容器的存储层,并构成新的镜像。以后我们运行这个新镜像的时候,就会拥有原有容器最后的文件变化。
    1. docker commit [选项] <容器ID或容器名> [<仓库名>[:<标签>]]

    终止

    docker stop

    将容器退出
    一般安全的方法是docker stop docker rm/start

    docker kill

    强制容器退出

    启动

    docker start

    启动已经停止的容器

    docker restart

    重启:启动正在运行的容器
    我的理解是docker restart = docker stop + docker start

    删除

    docker rm

    1. $docker rm [NAME]/[CONTAINER ID]
    注意:不能够删除一个正在运行的容器,会报错。需要先停止容器(docker stop)。
  1. 删除所有停止的容器
  2. $docker stop $(docker ps -a -q)

docker container prune

docker container prune = docker stop $(docker ps -a -q)
删除所有停止的容器

  1. $docker container prune
  2. WARNING! This will remove all stopped containers.
  3. Are you sure you want to continue? [y/N] y
  4. Deleted Containers:
  5. 80c6d9902fb87bbf3fd73cc57ec1568d4b7e02934a6a0fba33a05e382d252705

拷贝

docker cp

  1. docker cp 容器名:文件在容器里的路径 宿主机的路径
  2. docker cp 宿主机的路径 容器名:文件在容器里的路径

docker 命令总汇

  1. docker container --help
  2. Usage: docker container COMMAND
  3. Manage containers
  4. Commands:
  5. attach Attach local standard input, output, and error streams to a running container
  6. commit Create a new image from a container's changes
  7. cp Copy files/folders between a container and the local filesystem
  8. create Create a new container
  9. diff Inspect changes to files or directories on a container's filesystem
  10. exec Run a command in a running container
  11. export Export a container's filesystem as a tar archive
  12. inspect Display detailed information on one or more containers
  13. kill Kill one or more running containers
  14. logs Fetch the logs of a container
  15. ls List containers
  16. pause Pause all processes within one or more containers
  17. port List port mappings or a specific mapping for the container
  18. prune Remove all stopped containers
  19. rename Rename a container
  20. restart Restart one or more containers
  21. rm Remove one or more containers
  22. run Run a command in a new container
  23. start Start one or more stopped containers
  24. stats Display a live stream of container(s) resource usage statistics
  25. stop Stop one or more running containers
  26. top Display the running processes of a container
  27. unpause Unpause all processes within one or more containers
  28. update Update configuration of one or more containers
  29. wait Block until one or more containers stop, then print their exit codes
  30. Run 'docker container COMMAND --help' for more information on a command.