Skip to content

6.3 Docker 容器命令

  1. 创建容器
shell
# 常用参数列表
# -d: 后台运行容器,并返回容器 ID
# -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
# -i: 以交互模式运行容器,通常与 -t 同时使用
# -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
# --name=my_container: 为容器指定一个名称
# --dns 8.8.8.8: 指定容器使用的 DNS 服务器,默认和宿主一致
$ docker run -d --name=my_container -p 8080:8080 tomcat:latest
  1. 查看容器列表
shell
# 查看正在运行的容器列表
$ docker ps

# 查看最近一次创建的容器
$ docker ps -l

# 查看正在运行的容器 ID 列表
$ docker ps -q

# 查看全部容器(包括已经停止的容器)
$ docker ps -a

# 查看全部容器 ID 列表
$ docker ps -aq
  1. 停止运行的容器
shell
# 使用容器名停止
$ docker stop my_container

# 使用容器 ID 停止
$ docker stop container_id

# 使用容器 ID 停止多个正在运行的容器
$ docker ps
  1. 启动已停止的容器
shell
# 容器名
$ docker start my_container

# 容器 ID
$ docker start container_id

# 使用容器 ID 启动多个已停止的容器
$ docker start `docker ps -aq`
  1. 删除容器
shell
# 用容器名删除
$ docker rm my_container

# 用容器 ID 删除
$ docker rm container_id

# 删除多个未运行的容器, 运行中的无法删除
$ docker rm `docker ps -aq`
  1. 进入容器(正在运行的容器才可以进入)
shell
# 使用容器名
$ docker exec -it my_container /bin/bash

# 使用容器 ID
$ docker exec -it container_id /bin/bash
  1. 查看容器信息
shell
# 容器名
$ docker inspect my_container

# 容器 ID
$ docker inspect container_id