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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: 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 ~]#
目录
相关文章
|
5天前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker和Kubernetes入门
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性和效率的关键。本篇文章将引导读者了解如何利用Docker进行容器化打包及部署,以及Kubernetes集群管理的基础操作,帮助初学者快速入门云原生的世界。通过实际案例分析,我们将深入探讨这些技术在现代IT架构中的应用与影响。
23 2
|
3天前
|
运维 Cloud Native 虚拟化
一文吃透云原生 Docker 容器,建议收藏!
本文深入解析云原生Docker容器技术,涵盖容器与Docker的概念、优势、架构设计及应用场景等,建议收藏。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
一文吃透云原生 Docker 容器,建议收藏!
|
11天前
|
关系型数据库 MySQL API
|
7天前
|
运维 持续交付 Docker
深入理解Docker容器化技术
深入理解Docker容器化技术
|
23天前
|
存储 监控 安全
|
4天前
|
缓存 监控 开发者
掌握Docker容器化技术:提升开发效率的利器
在现代软件开发中,Docker容器化技术成为提升开发效率和应用部署灵活性的重要工具。本文介绍Docker的基本概念,并分享Dockerfile最佳实践、容器网络配置、环境变量和秘密管理、容器监控与日志管理、Docker Compose以及CI/CD集成等技巧,帮助开发者更高效地利用Docker。
|
5天前
|
监控 持续交付 Docker
Docker 容器化部署在微服务架构中的应用有哪些?
Docker 容器化部署在微服务架构中的应用有哪些?
|
5天前
|
监控 持续交付 Docker
Docker容器化部署在微服务架构中的应用
Docker容器化部署在微服务架构中的应用
|
5天前
|
安全 持续交付 Docker
微服务架构和 Docker 容器化部署的优点是什么?
微服务架构和 Docker 容器化部署的优点是什么?
|
7天前
|
运维 开发者 Docker
Docker Compose:简化容器化应用的部署与管理
Docker Compose:简化容器化应用的部署与管理