0%

Docker常见命令

Docker常见命令

获取版本

1
docker -v

帮助命令

1
docker command --help

镜像命令

检索镜像

1
2
3
4
5
6
docker search <镜像名.版本>

--
NAME 镜像仓库名称 DESCRIPTION 镜像仓库的描述 STARS 镜像仓库的收藏数
OFFICIAL 是否为官方仓库
AUTOMATED 是否自动构建镜像仓库

镜像下载

1
docker pull <镜像名:tag>

本地镜像列表

1
2
3
4
5
6
7
8
docker images

---
REPOSITORY 镜像仓库的名称
TAG 镜像的标签
IMAGE ID 镜像id
CREATED 镜像的创建时间
SIZE 镜像大小

删除镜像

1
docker rmi <镜像名>

查看镜像详细信息

包括容器id、容器名、环境变量、运行命令、主机配置、网络配置以及数据卷配置等信息

1
docker inspect 镜像名

导出镜像

不指定目录则导出到当前目录下

1
docker save centos > centos.tar

导入镜像

将镜像包导入到docker机器上

1
docker load < centos.tar

设置标签

1
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

创建镜像

1
docker build -t <镜像名> <Dockerfile路径>

容器命令

创建容器

创建容器的流程为 docker先去本地路径下查找是否有相应的镜像,如果没有就去docker hub上搜索,搜索到之后下载下来,利用该镜像创建一个容器并启动

1
2
3
4
docker create nginx

# 也可以指定容器的名称
docker create --name=nginx nginx

创建并运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -d表示执行完这句命令之后还可以在控制台继续输入命令
# -p 可以指定端口,如 -p 8080:80 表示容器的80端口映射到宿主机的8080端口
# -a stdin 指定标准输入输出内容类型
# --name 为容器指定一个名称
# --dns 指定容器使用DNS服务器,默认和宿主一致
docker run --name 容器名 -d -p 宿主机端口:容器端口 镜像名

# 启动centos
# -i 表示启动容器后,打开标准输入设备
# -t 表示启动容器后,分配一个伪终端
# /bin/bash 表示运行容器中的bash程序
# 执行之后会进入centos容器中,执行exit退出容器
docker run -i -t centos /bin/bash

容器列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -a  列出所有的容器,包括运行中的和已停止的
# -l 列出最近创建的容器
# -n 列出n个最近创建的容器
# -q 仅列出CONTAINER ID字段
# -s 输出中增加size字段,表示容器的大小
docker ps

--
CONTAINER ID 容器id
IMAGE 镜像名称
COMMAND 启动容器时运行的命令
CREATED 容器创建的时间
STATUS 容器运行的状态,up表示运行中,exited表示已退出
PORTS 容器对外暴露的端口号
NAMES 容器的名称

进入容器

只能进入运行中的,只针对于交互性的容器(使用it启动的,后台型的无法进入(使用-d启动的))

1
docker attach 容器id

停止容器

1
2
3
4
5
# 不会立刻终止
docker stop 容器id

# 立刻终止
docker kill 容器id

启动容器

1
2
3
4
docker start 容器id

# 重启
docker restart 容器id

端口映射

docker容器中运行的软件所使用的端口,在本机和局域网是不能访问的,需要将Docker容器中的端口映射到当前主机的端口上

1
2
# -p进行端口映射,将容器6379端口映射到本机6378端口上
docker run -d -p 6378:6379 --name 容器名 redis

删除容器

删除已停止的容器

1
docker rm 容器id

查看容器进程

1
docker top

容器日志

1
2
3
4
# -f可以查看实时日志
# --tail 日志输出行数
# -t 显示日志的输出时间
docker logs -f <容器名或ID>

使用容器执行命令

在容器中执行ls -l命令,注意只能在运行中的容器中执行,可以进入容器

1
docker exec -it 容器id ls -l

导出容器

1
docker export 容器id > centos.tar

导入容器

1
docker import centos.tar cons/centos:lastest

修改容器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
docker update [参数] 容器名

参数
--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)
--cpus decimal Number of CPUs
--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)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap:
'-1' to enable unlimited swap
--pids-limit int Tune container pids limit (set -1 for
unlimited)
--restart string Restart policy to apply when a container exits

运行web环境

1
2
# 使用training/webapp运行web环境
docker run -d -P training/webapp python app.py

数据卷

查看所有数据卷

1
docker volume ls

查看数据卷详情

1
docker volumn inspect 数据卷名称

删除数据卷

1
docker volumn rm 数据卷名称

欢迎关注我的其它发布渠道