Docker run

  1. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  2. Run a command in a new container
  3. -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
  4. -d: 后台运行容器,并返回容器ID
  5. -i: 以交互模式运行容器,通常与 -t 同时使用;
  6. -P: 随机端口映射,容器内部端口随机映射到主机的端口
  7. -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
  8. -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
  9. --name="nginx-lb": 为容器指定一个名称;
  10. --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
  11. --dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
  12. -h "mars": 指定容器的hostname
  13. -e username="ritchie": 设置环境变量;
  14. --env-file=[]: 从指定文件读入环境变量;
  15. --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
  16. -m :设置容器使用内存最大值;
  17. --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
  18. --link=[]: 添加链接到另一个容器;
  19. --expose=[]: 开放一个端口或一组端口;
  20. --volume , -v: 绑定一个卷

Docker ps

  1. Usage: docker ps [OPTIONS]
  2. List containers
  3. Options:
  4. -a, --all Show all containers (default shows just running)
  5. -f, --filter filter Filter output based on conditions provided
  6. --format string Pretty-print containers using a Go template
  7. -n, --last int Show n last created containers (includes all
  8. states) (default -1)
  9. -l, --latest Show the latest created container (includes all
  10. states)
  11. --no-trunc Don't truncate output
  12. -q, --quiet Only display numeric IDs
  13. -s, --size Display total file sizes

Docker exec

  1. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  2. Run a command in a running container
  3. Options:
  4. -d, --detach Detached mode: run command in the background
  5. --detach-keys string Override the key sequence for detaching a
  6. container
  7. -e, --env list Set environment variables
  8. -i, --interactive Keep STDIN open even if not attached
  9. --privileged Give extended privileges to the command
  10. -t, --tty Allocate a pseudo-TTY
  11. -u, --user string Username or UID (format:
  12. <name|uid>[:<group|gid>])
  13. -w, --workdir string Working directory inside the container

Docker attach

  1. Usage: docker attach [OPTIONS] CONTAINER
  2. Attach local standard input, output, and error streams to a running container
  3. Options:
  4. --detach-keys string Override the key sequence for detaching a container
  5. --no-stdin Do not attach STDIN
  6. --sig-proxy Proxy all received signals to the process (default true)

exec和attach区别

  • exec 开启一个新的终端
  • attach 开启正在运行的终端

    Docker cp

    ```shell “docker cp” requires exactly 2 arguments. See ‘docker cp —help’.

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

  1. 从容器内拷贝文件到主机
  2. - `docker cp containerID:/home/test.txt ./home`
  3. <a name="usN9w"></a>
  4. ### Docker inspect
  5. ```shell
  6. Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
  7. Return low-level information on Docker objects
  8. Options:
  9. -f, --format string Format the output using the given Go template
  10. -s, --size Display total file sizes if the type is container
  11. --type string Return JSON for specified type

退出容器

  • exit 退出容器,容器停止
  • ctrl+P+Q 退出容器,容器不停止

    删除容器

  • docker rm containerID 删除指定的容器

  • docker rm $(docker ps -aq) 删除所有的容器
  • docker rm -f container 强制删除容器

    启动停止容器

  • docker start containerID/NAME 启动指定容器

  • docker stop containerID/NAME 停止指定容器
  • docker restart containerID/NAME 重启指定容器
  • docker kill containerID/NAME 杀死指定容器