3.操作Docker容器
3.1.创建容器
3.1.1.新建容器
命令:docker [container] create
新建的容器处于停止状态,可以使用docker [container] start
命令来启动它。 输入命令 --help
即可查看相关用法
3.1.2.启动容器
命令:docker [container] start
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
3.1.3.新建并启动容器
命令: docker [container]run
相当于先执行docker [container] create
新建容器再执行docker [container] start
该命令执行的操作:
- 检查本地是否存在指定的镜像,不存在就从公有仓库下载;
- 利用镜像创建一个容器,并启动该容器; 口分配一个文件系统给容器,并在只读的镜像层外面挂载一层可读写层;
- 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去;
- 从网桥的地址池配置一个 IP 地址给容器;
- 执行用户指定的应用程序;
- 执行完毕后容器被自动终止。
下面的命令启动一个 bash 终端,允许用户进行交互:
其中,-t 选项让 Docker 分配一个伪终端并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。
3.1.4.守护态运行
以-d
参数可以让Docker容器在后台以守护态运行:
3.1.5.查看容器输出
命令:docker [container] logs
Usage: docker container logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--help Print usage
--since string Show logs since timestamp
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
3.2.停止容器
3.2.1.暂停容器
命令:docker [container] pause 容器ID/容器名
,可以用 docker [container] unpause 容器ID/容器名
来恢复
3.2.2.终止容器
命令:docker [containter] stop
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing it (default 10)
可以通过docker [containter] start
命令重新启动,docker [containter] restart
则会使正在运行的容器先停止,然后再重新启动。
3.3.进入容器
在使用-d
参数时,容器启动后会进入后台,用户无法看到容器中的信息,也无法进行操作。如果要进入容器进行操作,可以使用attach
或exec
命令
3.3.1.attach命令
命令:docker attach 容器名/容器ID
Usage: docker attach [OPTIONS] CONTAINER
Attach to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--help Print usage
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
如果多个窗口同时attach到同一个容器的时候,所有的窗口都会同步显示;当某个窗口因为命令阻塞时,其他窗口也无法进行操作。
3.3.2.exec命令
命令:docker [container] exec 容器名/容器ID 命令
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables (default [])
--help Print usage
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: [:])
3.4.删除容器
命令:docker [container] rm 容器名/容器ID
可以用来删除处于终止状态或退出状态的容器,-f
参数可以强行删除
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
--help Print usage
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
3.5.导入和导出容器
3.5.1.导出容器
命令:docker [container] export
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
3.5.2.导入容器
命令:docker [container] import
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Set commit message for imported image
3.6.查看容器
3.6.1.查看容器详情
命令:docker container inspect [OPTIONS] 容器名/容器ID
Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Display detailed information on one or more containers
Options:
-f, --format string Format the output using the given Go template
--help Print usage
-s, --size Display total file sizes
内容太多,下面的部分就省略了...
3.6.2.查看容器内进程
命令:docker top CONTAINER [ps OPTIONS]
3.6.3.查看统计信息
命令:docker stats [OPTIONS] [CONTAINER...]
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--help Print usage
--no-stream Disable streaming stats and only pull the first result
3.7.其他容器命令
3.7.1.复制文件
命令:docker cp
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
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
--help Print usage
3.7.2.查看变更
命令:docker [container] diff 容器名/容器ID
3.7.3.查看端口映射
命令:docker container port 容器名/容器ID [PRIVATE_PORT[/PROTO]]
3.7.4.更新配置
命令:docker update [OPTIONS] 容器名/容器ID
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--help Print usage
--kernel-memory string Kernel memory limit
-m, --memory string Memory limit
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--restart string Restart policy to apply when a container exits