Docker----Docker容器操作常用命令

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: Docker----Docker容器操作常用命令

【原文链接】

一、环境准备

(1)使用docker pull centos 下载一个centos的最新版本的镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   3 weeks ago   231MB
[root@localhost ~]#

二、docker操作常用的命令

2.1 docker run 命令

(1)docker run --help 帮助信息

(2)docker run [可选参数] 镜像名称
--name 设置docker容器的名称
-d 在后台运行docker
-it 使用交互的方式运行docker
-p 执行容器的端口,如 -p 80:80
-P 随机指定端口

如下为以交互的方式启动并进入docker容器

[root@localhost ~]# docker run -it centos /bin/bash
[root@3e394ae7eda7 /]#

(3)docker run -d 镜像名称 后台启动docker
如下为后台启动centos容器,这里需要注意的是,使用-d后台启动docker镜像后,如果docker镜像中没有前台进程,则docker容器停止,即当docker发现没有前台进程时就会自动停止

[root@localhost ~]# docker run -d centos
3cab830ec97c4f964b62a42726e3e541f689f906e00ee2a24289f2ce44a2b94e
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                          PORTS     NAMES
3cab830ec97c   centos    "/bin/bash"   About a minute ago   Exited (0) About a minute ago             inspiring_benz
[root@localhost ~]#

(4)docker run -d 镜像名称 /bin/bash -C "xxx" 后台启动一个docker容器,并且执行xxx的shell脚本
如下为后台启动一个docker,并且docker容器中循环打印hello_docker,此时可以发现docker处于运行状态

[root@localhost ~]# docker run -d centos  /bin/sh -c "while true;do echo hello_docker;sleep 1;done"
9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             nice_almeida
[root@localhost ~]#

2.2 退出容器

(1)使用exit 命令,注意如果使用exit命令进入退出后,docker就停止了

[root@3e394ae7eda7 /]# exit
exit

[root@localhost ~]#
(2)使用快捷键 Ctrl+p Ctrl + q 快捷键可以做到退出docker容器,同时docker容器不会停止

2.3 docker ps 命令

(1)docker ps 帮助信息

[root@localhost ~]# 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 container IDs
  -s, --size            Display total file sizes
[root@localhost ~]#

(2)docker ps 列出正在运行的容器
如下,表示当前没有docker在运行

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#

(3)docker ps -a 列出正在运行和历史运行过的docker容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
3e394ae7eda7   centos         "/bin/bash"   6 minutes ago   Exited (0) 4 minutes ago             unruffled_lederberg
c2f1f112cbbe   feb5d9fea6a5   "/hello"      10 hours ago    Exited (0) 10 hours ago              loving_morse
0150f4c51f21   feb5d9fea6a5   "/hello"      10 hours ago    Exited (0) 10 hours ago              optimistic_albattani
[root@localhost ~]#

(4)docker ps -n 个数 列出最近创建的容器
如下表示最近创建的1个容器

[root@localhost ~]# docker ps -n 1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
3e394ae7eda7   centos    "/bin/bash"   7 minutes ago   Exited (0) 5 minutes ago             unruffled_lederberg
[root@localhost ~]#

(5)docker ps -aq 显示所有运行过的容器的id

[root@localhost ~]# docker ps -aq
3e394ae7eda7
c2f1f112cbbe
0150f4c51f21
[root@localhost ~]#

2.4 docker rm -f 容器id 命令

(1)docker rm -f 容器id 删除容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                    PORTS     NAMES
3e394ae7eda7   centos         "/bin/bash"   14 minutes ago   Up 3 minutes                        unruffled_lederberg
c2f1f112cbbe   feb5d9fea6a5   "/hello"      10 hours ago     Exited (0) 10 hours ago             loving_morse
0150f4c51f21   feb5d9fea6a5   "/hello"      10 hours ago     Exited (0) 10 hours ago             optimistic_albattani
[root@localhost ~]# docker rm -f 0150f4c51f21
0150f4c51f21
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                    PORTS     NAMES
3e394ae7eda7   centos         "/bin/bash"   14 minutes ago   Up 3 minutes                        unruffled_lederberg
c2f1f112cbbe   feb5d9fea6a5   "/hello"      10 hours ago     Exited (0) 10 hours ago             loving_morse
[root@localhost ~]#

(2)docker rm -f $(docker ps -aq) 删除所有docker容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                    PORTS     NAMES
3e394ae7eda7   centos         "/bin/bash"   17 minutes ago   Up 6 minutes                        unruffled_lederberg
c2f1f112cbbe   feb5d9fea6a5   "/hello"      10 hours ago     Exited (0) 10 hours ago             loving_morse
[root@localhost ~]# docker rm -f $(docker ps -aq)
3e394ae7eda7
c2f1f112cbbe
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#

(3)docker ps -aq|xargs docker rm -f 也可以删除所有的容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
2fe6ca22d978   centos    "/bin/bash"   4 seconds ago    Exited (0) 3 seconds ago              centos02
40dbe1daff99   centos    "/bin/bash"   12 seconds ago   Exited (0) 12 seconds ago             centos01
[root@localhost ~]# docker ps -aq|xargs docker rm -f
2fe6ca22d978
40dbe1daff99
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#

2.5 启动和停止容器

(1)docker start 容器id 启动容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   19 seconds ago   Exited (0) 15 seconds ago             blissful_kirch
[root@localhost ~]# docker start aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   33 seconds ago   Up 2 seconds             blissful_kirch
[root@localhost ~]#

(2)docker restart 容器id 重启重启

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   7 minutes ago   Up 6 minutes             blissful_kirch
[root@localhost ~]# docker restart aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS        PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   7 minutes ago   Up 1 second             blissful_kirch
[root@localhost ~]#

(3)docker stop 容器id 停止容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS          PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   8 minutes ago   Up 30 seconds             blissful_kirch
[root@localhost ~]# docker stop aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   8 minutes ago   Exited (0) 4 seconds ago             blissful_kirch
[root@localhost ~]#

(4)docker kill 容器id 停止容器(当使用docker stop 无法停止时,可以使用docker kill 直接杀掉)

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   9 minutes ago   Up 9 seconds             blissful_kirch
[root@localhost ~]# docker kill aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                       PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   9 minutes ago   Exited (137) 5 seconds ago             blissful_kirch
[root@localhost ~]#

2.6 启动和停止容器

(1)docker start 容器id 启动容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   19 seconds ago   Exited (0) 15 seconds ago             blissful_kirch
[root@localhost ~]# docker start aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   33 seconds ago   Up 2 seconds             blissful_kirch
[root@localhost ~]#

(2)docker restart 容器id 重启重启

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   7 minutes ago   Up 6 minutes             blissful_kirch
[root@localhost ~]# docker restart aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS        PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   7 minutes ago   Up 1 second             blissful_kirch
[root@localhost ~]#

(3)docker stop 容器id 停止容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS          PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   8 minutes ago   Up 30 seconds             blissful_kirch
[root@localhost ~]# docker stop aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   8 minutes ago   Exited (0) 4 seconds ago             blissful_kirch
[root@localhost ~]#

(4)docker kill 容器id 停止容器(当使用docker stop 无法停止时,可以使用docker kill 直接杀掉)

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   9 minutes ago   Up 9 seconds             blissful_kirch
[root@localhost ~]# docker kill aa2976a9dbb1
aa2976a9dbb1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                       PORTS     NAMES
aa2976a9dbb1   centos    "/bin/bash"   9 minutes ago   Exited (137) 5 seconds ago             blissful_kirch
[root@localhost ~]#

2.7 docker logs 查看日志命令

(1)docker logs --help 查看docker logs 的帮助信息

[root@localhost ~]# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or
                       relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or
                       relative (e.g. 42m for 42 minutes)
[root@localhost ~]#

(2)docker logs -tf --tail num 容器id 可以查看启动容器的最新num行的日志
如下查看10行日志,并且会一直更新。类似linux中的tail -f 的效果

[root@localhost ~]# docker run -d centos  /bin/sh -c "while true;do echo hello_docker;sleep 1;done"
9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             nice_almeida
[root@localhost ~]# docker logs -tf --tail 10 9f427c9ff601
2021-10-10T15:27:02.215242337Z hello_docker
2021-10-10T15:27:03.218393417Z hello_docker
2021-10-10T15:27:04.220812877Z hello_docker
2021-10-10T15:27:05.223520640Z hello_docker
2021-10-10T15:27:06.226058326Z hello_docker
2021-10-10T15:27:07.229459499Z hello_docker
2021-10-10T15:27:08.232441670Z hello_docker
2021-10-10T15:27:09.235056718Z hello_docker
2021-10-10T15:27:10.238007922Z hello_docker
2021-10-10T15:27:11.240768549Z hello_docker

2.8 docker top 查看容器的进程信息

(1)docker top 容器id 查看容器中的进程信息

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   8 minutes ago   Up 8 minutes             nice_almeida
[root@localhost ~]# docker top 9f427c9ff601
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                68199               68179               0                   23:24               ?                   00:00:00            /bin/sh -c while true;do echo hello_docker;sleep 1;done
root                70194               68199               0                   23:32               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@localhost ~]#

2.9 docker inspect 查看容器内部信息

(1)docker inspect 容器id 查看容器的内部信息

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   11 minutes ago   Up 11 minutes             nice_almeida
[root@localhost ~]# docker inspect 9f427c9ff601
# 这里显示内容比较多,就不在此显示了

2.10 进入docker容器的命令

(1)docker exec -it 容器id bash 重新打开一个终端的方式进入docker
如下为进入docker后重新打开一个终端,这种方式可以通过exit方式退出docker

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   17 minutes ago   Up 17 minutes             nice_almeida
[root@localhost ~]# docker exec -it 9f427c9ff601 bash
[root@9f427c9ff601 /]#

(2)docker attach
进入容器中正在运行的终端

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
9f427c9ff601   centos    "/bin/sh -c 'while t…"   19 minutes ago   Up 19 minutes             nice_almeida
[root@localhost ~]# docker attach 9f427c9ff601
hello_docker
hello_docker
hello_docker
hello_docker
hello_docker
hello_docker
hello_docker
hello_docker
hello_docker
hello_docke

2.11 从容器内拷贝文件到主机上

(1)docker cp 容器id:容器文件路径 本地文件路径

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d1df9f4c810d   centos    "/bin/sh -c 'while t…"   16 minutes ago   Up 16 minutes             confident_franklin
[root@localhost ~]# docker exec -it d1df9f4c810d bash
[root@d1df9f4c810d /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@d1df9f4c810d /]# cd /opt
[root@d1df9f4c810d opt]# ls
[root@d1df9f4c810d opt]# echo 'hello docker' >> test.txt
[root@d1df9f4c810d opt]# ls
test.txt
[root@d1df9f4c810d opt]# exit
exit
[root@localhost ~]# docker cp d1df9f4c810d:/opt/test.txt /opt/test.txt
[root@localhost ~]# cat /opt/test.txt
hello docker
[root@localhost ~]#
目录
相关文章
|
3天前
|
弹性计算 运维 持续交付
探索Docker容器化技术及其在生产环境中的应用
探索Docker容器化技术及其在生产环境中的应用
16 5
|
14天前
|
Ubuntu Linux pouch
Docker容器管理工具
文章介绍了Docker容器管理工具,以及早期使用的LXC容器管理工具,包括它们的安装、使用和相关技术特点。
42 10
Docker容器管理工具
|
14天前
|
监控 数据管理 pouch
Docker容器技术概览
关于Docker容器技术的概览,包括Docker的优势、劣势、核心技术、容器所依赖的技术,以及Docker容器的管理和编排工具。
44 7
Docker容器技术概览
|
14天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
14天前
|
NoSQL 应用服务中间件 Redis
Docker跨宿主机容器通信-通过网络跨宿主机互联
这篇文章介绍了Docker容器跨宿主机通信的实现方法,包括Docker的四种网络模式(host、none、container、bridge)以及如何通过修改网络配置和添加路由规则来实现不同宿主机上的容器之间的互联。
40 0
Docker跨宿主机容器通信-通过网络跨宿主机互联
|
14天前
|
应用服务中间件 nginx Docker
Docker同一台宿主机容器通信-通过容器名称互联
本文详细介绍了如何通过容器名称实现同一宿主机上容器间的互联,并提供了实战案例。首先,文章解释了容器间通过自定义名称访问的原理,随后演示了创建并连接Tomcat与Nginx容器的具体步骤。此外,还讨论了配置中可能出现的问题及解决方案,包括避免硬编码IP地址和使用自定义容器别名来增强系统的灵活性与可维护性。通过这些实践,展示了如何高效地配置容器间通信,确保服务稳定可靠。
21 1
Docker同一台宿主机容器通信-通过容器名称互联
|
3天前
|
运维 Docker 微服务
掌握 Docker Compose:简化你的多容器应用部署
在微服务架构和容器化技术普及的今天,管理多容器部署变得颇具挑战。Docker Compose 通过一个 YAML 文件定义和运行多容器应用,简化了部署和运维。本文介绍其基本概念、使用方法及优势,包括服务、项目、卷和网络等核心概念,并提供从安装到管理服务的详细步骤,助你轻松掌握 Docker Compose,提高开发效率和应用运维质量。
|
4天前
|
Cloud Native 持续交付 Docker
探索Docker容器化技术及其在软件开发中的应用
探索Docker容器化技术及其在软件开发中的应用
16 7
|
4天前
|
存储 虚拟化 开发者
深入理解Docker容器化技术
深入理解Docker容器化技术
25 6
|
1天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。