docker 入门 —— docker 命令

mac2023-06-10  10

文章目录

一、docker --help 的使用1.1、docker --help 命令查询1.2、docker Commond --help 命令参数查询 二、docker 命令2.1、info | version2.2、本地镜像2.2.1、docker images 查看本地的镜像2.2.2、docker rmi 删除镜像 2.3、镜像仓库2.3.1、docker search 从仓库中搜索镜像2.3.2、docker pull 从仓库中拉取镜像到本地 2.4、容器生命周期管理2.4.1、docker run 通过一个镜像,创建并运行容器2.4.2、退出容器2.4.3、docker rm 删除容器2.4.6、docker attach2.4.5、docker exec2.4.6、docker attach 与 docker exec 的区别 2.5、容器操作2.6、容器rootfs命令2.6.1、docker cp 在 宿主机 和 容器 之间复制文件

一、docker --help 的使用

1.1、docker --help 命令查询

docker --help

执行如下:

[root@hbase01 ~]# docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: config Manage Docker configs container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. [root@hbase01 ~]#

1.2、docker Commond --help 命令参数查询

格式: docker Commond --help ,例如:

docker ps --help

查询 docker ps 命令参数,执行如下:

[root@hbase01 ~]# docker ps --help Usage: docker ps [OPTIONS] List containers Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display numeric IDs -s, --size Display total file sizes [root@hbase01 ~]#

二、docker 命令

2.1、info | version

info 显示系统范围的信息 version 显示Docker的版本信息

2.2、本地镜像

images 查看本地主机上的镜像列表 rmi 删除镜像 tag 标记本地镜像,将其归入某一仓库 build 从一个DockerFile构建镜像 history 显示镜像的历史记录 save 将指定镜像保存成 tar 归档文件 load 从存档或者STDIN加载镜像 import 从归档文件中创建镜像

2.2.1、docker images 查看本地的镜像

a : 列出本地所有镜像(含中间映像层)q :只显示镜像ID--digests :显示镜像的摘要信息--no-trunc:显示完整的镜像信息 [root@hbase01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE harisekhon/hbase 1.3 61abfdf6f7a4 10 months ago 482MB atguigu/mytomcat 1.2 b105c1f52eb5 11 months ago 463MB redis latest c188f257942c 11 months ago 94.9MB tomcat latest ca9e2fccef98 12 months ago 463MB [root@hbase01 ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE harisekhon/hbase 1.3 61abfdf6f7a4 10 months ago 482MB <none> <none> b49f99cf2001 11 months ago 200MB <none> <none> 8ec90a7d9d43 11 months ago 200MB atguigu/mytomcat 1.2 b105c1f52eb5 11 months ago 463MB redis latest c188f257942c 11 months ago 94.9MB tomcat latest ca9e2fccef98 12 months ago 463MB [root@hbase01 ~]# docker images -q 61abfdf6f7a4 b105c1f52eb5 c188f257942c ca9e2fccef98

REPOSITORY :表示镜像的仓库源 TAG : 镜像的标签 IMAGE ID :镜像ID CREATED:镜像创建时间 SIZE : 镜像的大小

2.2.2、docker rmi 删除镜像

docker rmi -f 镜像名 或者 image_id : 强制删除已运行的镜像

[root@hbase01 ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f Status: Downloaded newer image for hello-world:latest [root@hbase01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 10 months ago 1.84kB [root@hbase01 ~]# docker rmi -f hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3 ## 批量删除所有的本地镜像 [root@hbase01 ~]# docker rmi -f $(docker images -qa)

2.3、镜像仓库

login 登陆docker镜像仓库 logout 退出docker镜像仓库 pull 从镜像仓库拉取镜像 push 将本地的镜像上传到镜像仓库,要先登陆到镜像仓库 search 从 Docker Hub 搜索镜像

2.3.1、docker search 从仓库中搜索镜像

[root@hbase01 ~]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2548 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 69 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK] kubeguide/tomcat-app Tomcat image for Chapter 1 28 consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK] cloudesire/tomcat Tomcat server, 6/7/8 15 [OK] # 搜索 点赞数超过30的 tomcat 镜像 [root@hbase01 ~]# docker search -s 30 tomcat Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2548 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 69 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]

2.3.2、docker pull 从仓库中拉取镜像到本地

docker pull tomcat 等价于 docker pull tomcat:latest

[root@hbase01 ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 9a0b0ce99936: Pull complete db3b6004c61a: Pull complete f8f075920295: Pull complete 6ef14aff1139: Pull complete 962785d3b7f9: Pull complete 631589572f9b: Pull complete c55a0c6f4c7b: Pull complete 379605d88e88: Pull complete e056aa10ded8: Pull complete 6349a1c98d85: Pull complete Digest: sha256:77e41dbdf7854f03b9a933510e8852c99d836d42ae85cba4b3bc04e8710dc0f7 Status: Downloaded newer image for tomcat:latest # 再次查看本地镜像 [root@hbase01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 882487b8be1d 10 days ago 507MB

2.4、容器生命周期管理

run 通过镜像,创建一个新的容器并运行 start 启动容器 restart 重启容器 stop (温柔地)停止容器 kill kill (暴力)运行中的容器 exec 在正在运行的容器中运行命令 rm 删除容器 pause 暂停一个或多个容器中的所有进程 unpause 恢复容器中所有的进程 create 创建一个容器 rename 重命名容器 update 更新容器配置 stats 实时显示容器资源使用情况的统计信息

2.4.1、docker run 通过一个镜像,创建并运行容器

docker run:创建和启动一个新的容器实例,操作对象是镜像,选项较多,如果你要创建和启动一个容器,只能用 run 。

运行一个 centos 容器

[root@hbase01 ~]# docker run -it --name centos01 centos [root@363a3911877d /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp var bin etc lib media opt root sbin sys usr

docker run 与 docker star 的区别: https://blog.csdn.net/xiaojin21cen/article/details/84189230#4docker_run___docker_start_126

2.4.2、退出容器

退出容器有两个方式:

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

exit: 从容器中退出后,容器停止运行

[root@hbase01 ~]# docker run -it --name centos01 centos [root@363a3911877d /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp var bin etc lib media opt root sbin sys usr [root@363a3911877d /]# pwd / [root@363a3911877d /]# exit exit [root@hbase01 ~]# docker ps ### 容器停止运行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ctrl + P + Q: 从容器退出后,容器依然在运行

[root@hbase01 ~]# docker run -it --name centos02 centos [root@206d9e49888d /]# [root@hbase01 ~]# [root@hbase01 ~]# [root@hbase01 ~]# [root@hbase01 ~]# docker ps ### 容器依然在运行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 206d9e49888d centos "/bin/bash" 18 seconds ago Up 18 seconds centos02

2.4.3、docker rm 删除容器

删除单个容器: docker rm CONTAINER_ID :

删除所有的容器,有两种方式:

docker rm -f $( docker ps -qa )docker ps -qa | xargs docker rm [root@hbase01 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 206d9e49888d centos "/bin/bash" 14 hours ago Exited (255) 4 minutes ago centos02 [root@hbase01 ~]# [root@hbase01 ~]# docker rm 206d9e49888d ## 删除单个容器 206d9e49888d [root@hbase01 ~]#

2.4.6、docker attach

直接进入容器启动命令的终端,不会启动新的进程。 使用格式:

docker attach 容器ID

2.4.5、docker exec

在容器中打开新的终端,启动新的进程。

进入容器:

docker exec -it 容器ID /bin/bash

不进入容器,在宿主机中,打印容器的信息:

docker exec -it 容器ID ls /tmp

2.4.6、docker attach 与 docker exec 的区别

attach 直接进入容器启动命令的终端,不会启动新的进程。 通俗来讲,此时,容器与attach是同步的,如果你发送ctrl+c 键或者输入exit,退出终端时,docker容器也随之停止运行。 正确的退出方式 --> 组合键:Ctrl+P+Q

exec 在容器中打开新的终端,启动新的进程。

而 exec 是打开新的终端, 从容器中退出,无论是ctrl+c 键,还是输入exit 退出,都不会对docker容器造成影响力。

2.5、容器操作

ps 查看容器列表 inspect 查看容器内部细节 top 展示一个容器中运行的进程 attach 进入一个运行的容器 events 从服务器获取实时事件 logs 获取一个容器的日志 wait 阻塞直到容器停止,然后打印退出代码 export 将容器的文件系统导出为tar存档 port 查看端口映射或容器的特定映射列表

2.6、容器rootfs命令

commit 从容器创建一个镜像 cp 从容器和主机文件系统之间拷贝文件 diff 检查容器文件系统上的更改

2.6.1、docker cp 在 宿主机 和 容器 之间复制文件

将 宿主机上的文件 复制到 容器中:

docker cp [OPTIONS] dest_path container:src_path

从 容器中 向 宿主机 复制文件 :

docker cp [OPTIONS] container:src_path dest_path

示例:

本次示例的容器是 centos

[root@hbase01 test]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 363a3911877d centos "/bin/bash" 20 hours ago Up 23 minutes centos01

将 宿主机上的文件 复制到 容器中

[root@hbase01 test]# echo 'I,m host...'>> a.txt ## 向 a.txt 中写入文件 [root@hbase01 test]# ls a.txt [root@hbase01 test]# docker exec -it 363a3911877d ls /tmp ## 查年容器中 /tmp 目录下的文件 total 0 [root@hbase01 test]# docker cp a.txt 363a3911877d:/tmp/ ## 将文件复制到 容器中 #### 验证 #### [root@hbase01 test]# docker exec -it 363a3911877d /bin/bash ## 进入容器中 [root@363a3911877d /]# cd tmp/ [root@363a3911877d tmp]# ls ## 查看tmp下的文件 a.txt [root@363a3911877d tmp]# cat a.txt ## 查看文件内容 I,m host...

从 容器中 向 宿主机 复制文件

[root@hbase01 test]# docker exec -it 363a3911877d /bin/bash ## 进入容器中 [root@363a3911877d /]# cd tmp/ [root@363a3911877d tmp]# ls a.txt [root@363a3911877d tmp]# vi b.txt ## 在容器中编辑b.txt [root@hbase01 test]# cat b.txt ## 查看 b.txt中的内容 I,m in container... [root@363a3911877d tmp]# ls a.txt b.txt [root@363a3911877d tmp]# exit ## 退出容器 exit [root@hbase01 test]# docker cp 363a3911877d:/tmp/b.txt b.txt ## 从容器中复制文件到宿主机上 #### 验证 #### [root@hbase01 test]# ls a.txt b.txt [root@hbase01 test]# cat b.txt ## 查看b.txt 内容 I'm in container...
最新回复(0)