Docker 3:Docker常用命令

一、容器Docker

1. docker version

docker version 查看docker 版本

docker version

注:window使用docker时,需要运行dokcer Desktop;否则未报错:error during connect: In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect.: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json": open //./pipe/docker_engine: The system cannot find the file specified.

2. docker info

查看docker 信息


docker info

二、镜像IMAGE

2.1 docker image list & docker images

docker image ls [OPTIONS] [REPOSITORY[:TAG]] 列出镜像
docker image list [OPTIONS] [REPOSITORY[:TAG]]
docker images [OPTIONS] [REPOSITORY[:TAG]] 列出本地镜像

  • options:(三个命令相同)
  -a, --all             Show all images (default hides intermediate images)  # 列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层)
      --digests         Show digests  # 显示镜像的摘要信息;
  -f, --filter filter   Filter output based on conditions provided  # 显示满足条件的镜像
      --format string   Pretty-print images using a Go template  # 指定返回值的模板文件
      --no-trunc        Don't truncate output  # 显示完整的镜像信息
  -q, --quiet           Only show image IDs  # 仅展示imge id
PS C:\Windows\system32>
  • docker image list/ls
PS C:\Users\百草> docker image list
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
docker101tutorial   latest    64d9b0977993   6 hours ago    28.8MB
alpine/git          latest    c6b70534b534   3 months ago   27.4MB
PS C:\Users\百草>
  • docker image list -a/--digests/--no-trunc/-q


    docker image list -a/--digests/--no-trunc/-q
  • 返回信息:
    -REPOSITORY仓库名
    -TAG标签:一个镜像可以对应多个标签
    -IMAGE ID镜像 ID:镜像的唯一标识
    -CREATED创建时间
    -SIZE所占用的空间:镜像到本地后展开状态;docker hub上显示的是压缩后的体积。docker多层存储,相同基础镜像,使用共同层,相同层保存一份。

  • docker system df 查看镜像硬盘占用空间

PS C:\Users\百草> docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          2         2         56.15MB   0B (0%)  # 镜像
Containers      3         2         2.205kB   0B (0%)  # 容器
Local Volumes   1         1         10.41MB   0B (0%)
Build Cache     31        0         85.49MB   85.49MB
PS C:\Users\百草>

引申:docker image list <镜像名>docker image list <镜像名>:<标签> 显示局部镜像

2.2 docker search

docker search [OPTIONS] TERM 搜索镜像

  • OPTIONS说明:
    --automated:只列出 automated build类型的镜像; # 当前版本不支持
    --no-trunc:显示完整的镜像描述;
    -f <过滤条件>:列出收藏数不小于指定值的镜像。
PS C:\Windows\system32> docker search --help
Usage:  docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

如搜索python


docker search python
  • 参数说明:
    NAME: 镜像仓库源的名称
    DESCRIPTION: 镜像的描述
    STARS: 类似 Github 里面的 star,表示点赞、喜欢的意思。
    OFFICIAL: 是否 docker 官方发布
    AUTOMATED: 自动构建。

2.3 docker pull

docker pull [OPTIONS] NAME[:TAG|@DIGEST] 从镜像仓库中拉取或者更新指定镜像

PS C:\Windows\system32> docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry  # 下载镜像

Options:
  -a, --all-tags                Download all tagged images in the repository   # 拉取所有 tagged 镜像
      --disable-content-trust   Skip image verification (default true)   # 忽略镜像的校验,默认开启
      --platform string         Set platform if server is multi-platform  capable
  -q, --quiet                   Suppress verbose output
PS C:\Windows\system32>
docker pull

2.4 docker push

docker push [OPTIONS] NAME[:TAG] 上传本地镜像到镜像仓库

PS C:\Windows\system32> docker push --help
Usage:  docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry

Options:
  -a, --all-tags                Push all tagged images in the repository
      --disable-content-trust   Skip image signing (default true)  # 忽略镜像的校验,默认开启
  -q, --quiet                   Suppress verbose output

2.5 docker bulid

docker build [OPTIONS] PATH | URL | - 使用Dockerfile创建镜像
Dockerfile是一个包含用于组合映像的命令的文本文档。可以使用在命令行中调用任何命令。 Docker通过读取Dockerfile中的指令自动生成映像。

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is
                                'PATH/Dockerfile')  # 指定要使用的镜像文件路径;默认PATH/Dockerfile
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
      --network string          Set the networking mode for the RUN
                                instructions during build (default "default")
      --no-cache                Do not use cache when building the image
  -o, --output stringArray      Output destination (format:
                                type=local,dest=path)
      --platform string         Set platform if server is multi-platform
                                capable
      --progress string         Set type of progress output (auto, plain,
                                tty). Use plain to show container output
                                (default "auto")
      --pull                    Always attempt to pull a newer version of
                                the image
  -q, --quiet                   Suppress the build output and print image
                                ID on success  # 安静模式,成功后只输出镜像ID
      --secret stringArray      Secret file to expose to the build (only
                                if BuildKit enabled):
                                id=mysecret,src=/local/secret
      --ssh stringArray         SSH agent socket or keys to expose to the
                                build (only if BuildKit enabled) (format:
                                default|<id>[=<socket>|<key>[,<key>]])
  -t, --tag list                Name and optionally a tag in the
                                'name:tag' format  # 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
      --target string           Set the target build stage to build.

2.6 docker tag

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] 创建一个tag TARGET_IMAGE[:TAG] 指向本地镜像SOURCE_IMAGE[:TAG]

docker tag

2.7 docker rmi

docker rmi [OPTIONS] IMAGE [IMAGE...] # 删除本地一个或多个镜像

Options:
  -f, --force      Force removal of the image  # 强制删除
      --no-prune   Do not delete untagged parents  # 不移除该镜像的过程镜像,默认移除
docker rmi

三、容器container

3.1 docker run

docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 创建一个新的容器并运行一个命令
Options:

-d, --detach                         Run container in background and print container ID  # 后台运行并输出容器id
 -e, --env list                       Set environment variables  #设置环境变量,如 -e username="ritchie"
 --name string                    Assign a name to the container  # 为容器指定一个名字,需要唯一
-i, --interactive                    Keep STDIN open even if not attached  # 以交互模式运行容器,通常与 -t 同时使用;
-t, --tty                            Allocate a pseudo-TTY  # 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P, --publish-all                    Publish all exposed ports to random ports  #随机端口映射,容器内部端口随机映射到主机的端口
-p, --publish list                   Publish a container's port(s) to the host  #指定端口映射,格式为:主机(宿主)端口:容器端口;注意确定所设置的端口是否在阿里云上开端口?不然是连接不到的,只有开放端口才能正常访问
-v, --volume list                    Bind mount a volume  # 映射容器安装目录,方便管理;如-v ~/mysql/conf:/etc/mysql/conf.d
  • 报错信息
PS C:\Windows\system32> docker run -d -p 84:84 --name container004 docker101tutorial
docker: Error response from daemon: Conflict. The container name "/container004" is already in use by container "e8c3c99a461c2801dced5a388c11a5df8ba5f1f9257664cf6804d00216d76855". You have to remove (or rename) that container to be able to reuse that name.  # 容器名重复;容器创建失败
See 'docker run --help'.
PS C:\Windows\system32> docker run -d -p 84:84 --name container005 docker101tutorial
d9401085b44d7cef7bf8c973ffab2af751151dd7f59e3c2644ebdeca164292df  # 容器创建成功
docker: Error response from daemon: driver failed programming external connectivity on endpoint container005 (429ee6cce6fdfd3095a0653fe99c20bb41e58e412e88a65bde4868e441863ea4): Bind for 0.0.0.0:84 failed: port is already allocated.  # 端口被占用;但实际容器已被创建,只不过没有启动
PS C:\Windows\system32> docker run -d -p 86:86 --name container006 docker101tutorial
d747d5efd37d6a709786e88cc27b60d9febf0a1fa0247f9e85be52d7f4cb5f4f
PS C:\Windows\system32>

docker run -d -i -t --name xxx
也等同于 docker -dit --name xxx

3.2 docker ps

docker ps查看在运行的容器
参数添加 -a,则展示所有容器

PS C:\Windows\system32> docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED          STATUS          PORTS                        NAMES
d747d5efd37d   docker101tutorial          "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    80/tcp, 0.0.0.0:86->86/tcp   container006
e8c3c99a461c   docker101tutorial          "/docker-entrypoint.…"   4 minutes ago    Up 4 minutes    80/tcp, 0.0.0.0:84->84/tcp   container004
deb39d6e5c63   docker101tutorial:latest   "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes   80/tcp                       zealous_kepler
9e91864d7058   docker101tutorial          "/docker-entrypoint.…"   6 hours ago      Up 27 minutes   0.0.0.0:80->80/tcp           docker-tutorial
PS C:\Windows\system32>
docker ps

输出详情介绍:

  • CONTAINER ID: 容器 ID。
  • IMAGE: 使用的镜像。
  • COMMAND: 启动容器时运行的命令。
  • CREATED: 容器的创建时间。
    -STATUS: 容器状态。
    状态有7种:
    created(已创建)、restarting(重启中)、running(运行中)、removing(迁移中)、paused(暂停)、exited(停止)、dead(死亡)
  • PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。
  • NAMES: 自动分配的容器名称。

3.3 docker stop/kill/start/restart

  • docker start
PS C:\Windows\system32> docker start --help

Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]  # 容器名或容器id;容器id支持模糊查询

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
  -i, --interactive          Attach container's STDIN
PS C:\Windows\system32>
docker start
  • docker restart xxx # 重启
  • docker stop
PS C:\Windows\system32> docker stop --help

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)  
# 关闭容器的限时,如果超时未能关闭则用kill强制关闭,默认值10s,这个时间用于容器的自己保存状态
PS C:\Windows\system32>
docker stop
  • docker kill xxx # 强制关闭

3.4 docker cp

PS C:\Windows\system32> 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  # 文件与主机之间的数据拷贝

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
PS C:\Windows\system32>

3.5 docker clone

3.6 docker rm

docker rm xxx 删除一个或多个容器

PS C:\Windows\system32> docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)  # 强制删除,通过 SIGKILL 信号删除正在运行中的容器
  -l, --link      Remove the specified link  # 移除容器间的网络链接,并非容器
  -v, --volumes   Remove anonymous volumes associated with the container  # 删除与容器关联的卷
PS C:\Windows\system32>

3.8 docker rename

PS C:\Windows\system32> docker rename --help
Usage:  docker rename CONTAINER NEW_NAME  # 使用容器id
Rename a container  # 更新容器名
docker rename

四、参考

  1. Docker学习2-Docker的基本命令与使用
  2. docker常规操作——启动、停止、重启容器实例
  3. Dokcer rm命令
  4. Docker push命令
  5. Docker build命令
  6. Docker cp命令
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 230,501评论 6 544
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,673评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,610评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,939评论 1 318
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,668评论 6 412
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 56,004评论 1 329
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 44,001评论 3 449
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 43,173评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,705评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,426评论 3 359
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,656评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,139评论 5 364
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,833评论 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,247评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,580评论 1 295
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,371评论 3 400
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,621评论 2 380

推荐阅读更多精彩内容