详解Docker 平台基本使用方法

本篇文章重点为大家讲解一下Docker 平台基本使用方法,有需要的小伙伴可以参考一下。

开启动网络转发功能,默认会自动开启

[root@Docker  ]# vim /etc/sysctl.conf #插入以下内容
net.ipv4.ip_forward = 1
[root@Docker  ]# sysctl -p #生效
net.ipv4.ip_forward = 1
[root@Docker  ]# cat /proc/sys/net/ipv4/ip_forward
1

例 1:运行一个 container 并加载镜像 centos,运行起来这个实例后,在实例中执行 /bin/bash 命令

docker 常用参数:
run 运行
-i 以交互模式运行容器,通常与 -t 同时使用
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用
[root@Docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        4 months ago        237MB
[root@Docker ~]# docker run -it centos:latest /bin/bash  #启动一个实例,秒级响应

[root@efb4af688330 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

[root@efb4af688330 /]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

安装软件

[root@0a297ff99af8 /]# yum install -y httpd
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                      5.7 MB/s | 7.0 MB     00:01    
CentOS-8 - Base                           2.2 MB/s | 2.2 MB     00:00    
CentOS-8 - Extras                         8.1 kB/s | 5.9 kB     00:00    
Dependencies resolved.
==========================================================================
Package     Arch   Version                               Repo       Size
==========================================================================
Installing:
httpd       x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 M
Installing dependencies:
apr         x86_64 1.6.3-9.el8                           AppStream 125 k
apr-util    x86_64 1.6.1-6.el8                           AppStream 105 k
centos-logos-httpd
            noarch 80.5-2.el8                            AppStream  24 k
httpd-filesystem
            noarch 2.4.37-16.module_el8.1.0+256+ae790463 AppStream  35 k
httpd-tools x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 103 k
mod_http2   x86_64 1.11.3-3.module_el8.1.0+213+acce2796  AppStream 158 k
brotli      x86_64 1.0.6-1.el8                           BaseOS    323 k
mailcap     noarch 2.1.48-3.el8                          BaseOS     39 k
Installing weak dependencies:
apr-util-bdb
            x86_64 1.6.1-6.el8                           AppStream  25 k
apr-util-openssl
            x86_64 1.6.1-6.el8                           AppStream  27 k
Enabling module streams:
httpd              2.4                                                  

Transaction Summary
==========================================================================
Install  11 Packages

Total download size: 2.6 M
Installed size: 8.1 M
Downloading Packages:
(1/11): apr-util-bdb-1.6.1-6.el8.x86_64.r 142 kB/s |  25 kB     00:00    A
(2/11): apr-1.6.3-9.el8.x86_64.rpm        588 kB/s | 125 kB     00:00    
(3/11): apr-util-openssl-1.6.1-6.el8.x86_ 346 kB/s |  27 kB     00:00    
(4/11): apr-util-1.6.1-6.el8.x86_64.rpm   340 kB/s | 105 kB     00:00    
(5/11): centos-logos-httpd-80.5-2.el8.noa 187 kB/s |  24 kB     00:00    
(6/11): httpd-filesystem-2.4.37-16.module 398 kB/s |  35 kB     00:00    
(7/11): mod_http2-1.11.3-3.module_el8.1.0 992 kB/s | 158 kB     00:00    
(8/11): httpd-2.4.37-16.module_el8.1.0+25 4.8 MB/s | 1.7 MB     00:00    
(9/11): httpd-tools-2.4.37-16.module_el8. 387 kB/s | 103 kB     00:00    
(10/11): mailcap-2.1.48-3.el8.noarch.rpm  289 kB/s |  39 kB     00:00    
(11/11): brotli-1.0.6-1.el8.x86_64.rpm    826 kB/s | 323 kB     00:00    
--------------------------------------------------------------------------
Total                                     290 kB/s | 2.6 MB     00:09    
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/apr-1.6.3-9.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream                      1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
Userid     : "CentOS (CentOS Official Signing Key) "
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
 Preparing        :                                                  1/1
 Installing       : apr-1.6.3-9.el8.x86_64                          1/11
 Running scriptlet: apr-1.6.3-9.el8.x86_64                          1/11
 Installing       : apr-util-bdb-1.6.1-6.el8.x86_64                 2/11
 Installing       : apr-util-openssl-1.6.1-6.el8.x86_64             3/11
 Installing       : apr-util-1.6.1-6.el8.x86_64                     4/11
 Running scriptlet: apr-util-1.6.1-6.el8.x86_64                     4/11
 Installing       : httpd-tools-2.4.37-16.module_el8.1.0+256+ae7    5/11
 Installing       : mailcap-2.1.48-3.el8.noarch                     6/11
 Installing       : brotli-1.0.6-1.el8.x86_64                       7/11
 Running scriptlet: httpd-filesystem-2.4.37-16.module_el8.1.0+25    8/11
 Installing       : httpd-filesystem-2.4.37-16.module_el8.1.0+25    8/11
 Installing       : centos-logos-httpd-80.5-2.el8.noarch            9/11
 Installing       : mod_http2-1.11.3-3.module_el8.1.0+213+acce27   10/11
 Installing       : httpd-2.4.37-16.module_el8.1.0+256+ae790463.   11/11
 Running scriptlet: httpd-2.4.37-16.module_el8.1.0+256+ae790463.   11/11
 Verifying        : apr-1.6.3-9.el8.x86_64                          1/11
 Verifying        : apr-util-1.6.1-6.el8.x86_64                     2/11
 Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                 3/11
 Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64             4/11
 Verifying        : centos-logos-httpd-80.5-2.el8.noarch            5/11
 Verifying        : httpd-2.4.37-16.module_el8.1.0+256+ae790463.    6/11
 Verifying        : httpd-filesystem-2.4.37-16.module_el8.1.0+25    7/11
 Verifying        : httpd-tools-2.4.37-16.module_el8.1.0+256+ae7    8/11
 Verifying        : mod_http2-1.11.3-3.module_el8.1.0+213+acce27    9/11
 Verifying        : brotli-1.0.6-1.el8.x86_64                      10/11
 Verifying        : mailcap-2.1.48-3.el8.noarch                    11/11

Installed:
 httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                      
 apr-util-bdb-1.6.1-6.el8.x86_64                                        
 apr-util-openssl-1.6.1-6.el8.x86_64                                    
 apr-1.6.3-9.el8.x86_64                                                  
 apr-util-1.6.1-6.el8.x86_64                                            
 centos-logos-httpd-80.5-2.el8.noarch                                    
 httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch          
 httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                
 mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64                  
 brotli-1.0.6-1.el8.x86_64                                              
 mailcap-2.1.48-3.el8.noarch                                            

Complete!

退出容器

[root@efb4af688330 /]# exitexit
[root@Docker ~]#

例 2:在 container 中启动一个长久运行的进程,不断向 stdin 输出 hello world 模拟一个后台运行的服务

docker 常用参数:
-d 后台运行容器,并返回容器 ID
-c 后面跟待完成的命令
[root@Docker ~]# docker run -d centos:latest /bin/bash -c "while true;do echo hello world;sleep 1;done"
607752360adf7e9ebfdbae01d09b2fd9ed2f350ad2d87e3e868d63144b966104
#容器的 ID

从一个容器中取日志,查看输出的内容

[root@Docker ~]# docker logs 607752360ad #容器的 ID 可以写全,也可以不写全,只要唯一就可以了
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
[root@Docker ~]#

查看正在运行的容器

[root@Docker  ]# docker ps #列出所有运行中容器
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
607752360adf        centos:latest       "/bin/bash -c 'while…"   About a minute ago   Up About a minute                       brave_fermi

也可以使用短 ID 或 docker 实例的名字查看日志输出

[root@Docker ~]# docker logs 607752360adf
或
[root@Docker ~]# docker logs brave_fermi
[root@Docker ~]# docker ps -a #-a 列出所有容器(包含沉睡/退出状态的容器)
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
607752360adf        centos:latest       "/bin/bash -c 'while…"   4 minutes ago       Up 4 minutes                                   brave_fermi
0a297ff99af8        centos:latest       "/bin/bash"              7 minutes ago       Exited (1) 5 minutes ago                       ecstatic_yonath
efb4af688330        centos:latest       "/bin/bash"              9 minutes ago       Exited (0) 8 minutes ago                       epic_mcclintock

[root@Docker ~]# docker images #列出所有本地镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        4 months ago        237MB

例 3:杀死一个容器 比如:杀死一个正在后台运行的容器

查看要杀死容器的 ID

[root@Docker ~]# docker ps -a #-a 列出所有容器(包含沉睡/退出状态的容器);
杀死 ID 为 607752360adf 的容器
[root@Docker ~]# docker kill 607752360adf #杀死一个容器
607752360adf

查看结果

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

例 4:启动、停止、重启 container 容器实例

启动: run # 创建并运行 docker 实例

[root@Docker  ]# docker run -d centos:latest /bin/bash -c "while true;do echo hello world;sleep 1;done"

查看容器

[root@Docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bea2aaa70725        centos:latest       "/bin/bash -c 'while…"   4 seconds ago       Up 3 seconds                            flamboyant_keller

关闭容器

[root@Docker ~]# docker stop bea2aaa70725
bea2aaa70725

查看

[root@Docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@Docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS               NAMES
bea2aaa70725        centos:latest       "/bin/bash -c 'while…"   About a minute ago   Exited (137) 35 seconds ago                       flamboyant_keller
607752360adf        centos:latest       "/bin/bash -c 'while…"   9 minutes ago        Exited (137) 3 minutes ago                        brave_fermi
0a297ff99af8        centos:latest       "/bin/bash"              12 minutes ago       Exited (1) 10 minutes ago                         ecstatic_yonath
efb4af688330        centos:latest       "/bin/bash"              15 minutes ago       Exited (0) 13 minutes ago                         epic_mcclintock
[root@Docker ~]# docker start bea2aaa70725
bea2aaa70725
[root@Docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bea2aaa70725        centos:latest       "/bin/bash -c 'while…"   2 minutes ago       Up 31 seconds                           flamboyant_keller
[root@Docker ~]# docker restart bea2aaa70725
bea2aaa70725

删除指定 container : rm

[root@Docker ~]# docker rm bea2aaa70725
Error response from daemon: You cannot remove a running container bea2aaa707259a80c474e7d43c5648af0a5cfe16c4e50470fb6c5c84a0490d8b. Stop the container before attempting removal or force remove

解决:可以先把容器 1a63ddea6571 关闭,然后再删除或加-f 强制删除

[root@Docker ~]# docker rm -f bea2aaa70725
bea2aaa70725

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/222699.html<

(0)
管理的头像管理
上一篇2025-04-15 15:06
下一篇 2025-04-15 15:07

相关推荐

  • 云服务器和云虚拟主机怎么选?云服务器和虚拟主机区别

    云服务器适合业务增长快、需弹性扩展的场景,而云虚拟主机适合预算有限、技术门槛低的小型静态网站或测试环境,二者核心区别在于资源独享性与运维复杂度,核心差异解析:从底层架构到使用体验很多人容易混淆这两者,觉得它们都是“买空间建站”,它们的底层逻辑完全不同,云服务器(ECS)就像是你租了一整栋别墅,水电网络独立,你想……

    2026-06-29
    0
  • 赣州智慧旅游招聘是真的吗?赣州旅游人才招聘信息

    中级岗位(3-5年经验)月薪范围通常在6000-10000元,这类岗位需要独立负责项目模块,如独立运营一个抖音账号,或维护一个景区小程序的功能迭代,具备成功案例的候选人议价能力较强,高级岗位(5年以上经验)月薪范围通常在10000-20000元,部分核心管理岗可达更高,这类人才需要具备战略规划能力,如制定整个景……

    2026-06-29
    0
  • 赣州智能物联网车位锁如何管理?智能车位锁管理系统多少钱

    赣州智能物联网车位锁管理的核心在于通过云端平台实现远程控锁、状态实时监控及自动计费,彻底解决传统车位“被占难管”与“找位难”的痛点,在赣州这样的城市,随着机动车保有量的持续增长,老旧小区、商业综合体以及私人固定车位的资源矛盾日益凸显,传统的机械地锁或简易遥控锁,不仅操作繁琐,更无法实现数据化管理,引入智能物联网……

    2026-06-29
    0
  • 赣州智能消防栓好用吗,智能消防栓多少钱一个

    赣州智能消防栓通过物联网技术实现实时监测与远程报警,能显著降低火灾响应时间并提升城市消防安全管理水平,是目前智慧城市建设中不可或缺的基础设施,赣州智能消防栓的核心价值与应用场景传统消防栓往往存在“看不见、摸不着、用不了”的痛点,在赣州这样地形复杂、老城区与新城区并存的区域,传统设施的管理难度极大,智能消防栓的出……

    2026-06-29
    0
  • 云服务器和物理机到底有啥区别?

    云服务器本质上是虚拟化资源池中的弹性实例,而传统物理服务器是独占的硬件实体,前者胜在弹性与运维便捷,后者强在物理隔离与性能稳定,具体选择取决于业务对成本、扩展性及安全合规的权衡,很多人初次接触服务器时,容易把“云服务器”和“传统物理服务器”混为一谈,觉得它们都是用来跑网站或存数据的盒子,这两者的底层逻辑完全不同……

    2026-06-29
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注