执行如下:
[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 ~]#格式: 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 ~]#REPOSITORY :表示镜像的仓库源 TAG : 镜像的标签 IMAGE ID :镜像ID CREATED:镜像创建时间 SIZE : 镜像的大小
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)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 507MBdocker 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 usrdocker run 与 docker star 的区别: https://blog.csdn.net/xiaojin21cen/article/details/84189230#4docker_run___docker_start_126
退出容器有两个方式:
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 NAMESctrl + 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删除单个容器: 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 ~]#直接进入容器启动命令的终端,不会启动新的进程。 使用格式:
docker attach 容器ID在容器中打开新的终端,启动新的进程。
进入容器:
docker exec -it 容器ID /bin/bash不进入容器,在宿主机中,打印容器的信息:
docker exec -it 容器ID ls /tmpattach 直接进入容器启动命令的终端,不会启动新的进程。 通俗来讲,此时,容器与attach是同步的,如果你发送ctrl+c 键或者输入exit,退出终端时,docker容器也随之停止运行。 正确的退出方式 --> 组合键:Ctrl+P+Q
exec 在容器中打开新的终端,启动新的进程。
而 exec 是打开新的终端, 从容器中退出,无论是ctrl+c 键,还是输入exit 退出,都不会对docker容器造成影响力。
将 宿主机上的文件 复制到 容器中:
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...