Docker(安装&常用命令)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

docker是什么:

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口


Docker(安装&常用命令)

提示:CentOS Linux 8的yum源配置
本人Linux镜像为CentOS 8所以需要配置yum源,其他版本可省略
yum源配置

一、Docker安装

1、安装gcc

yum -y install gcc
yum -y install gcc-c++

详细安装如下

[root@localhost yum.repos.d]# yum -y install gcc
Last metadata expiration check: 0:01:42 ago on Tue 08 Nov 2022 02:41:44 PM CST.
Package gcc-8.5.0-3.el8.x86_64 is already installed.
Dependencies resolved.
================================================================================
 Package         Architecture   Version                 Repository         Size
================================================================================
Upgrading:
 cpp             x86_64         8.5.0-4.el8_5           AppStream          10 M
 gcc             x86_64         8.5.0-4.el8_5           AppStream          23 M
 libgcc          x86_64         8.5.0-4.el8_5           base               79 k
 libgomp         x86_64         8.5.0-4.el8_5           base              206 k

Transaction Summary
================================================================================
Upgrade  4 Packages

Total download size: 34 M
Downloading Packages:
(1/4): libgcc-8.5.0-4.el8_5.x86_64.rpm          116 kB/s |  79 kB     00:00
(2/4): libgomp-8.5.0-4.el8_5.x86_64.rpm         162 kB/s | 206 kB     00:01
(3/4): cpp-8.5.0-4.el8_5.x86_64.rpm             236 kB/s |  10 MB     00:45
(4/4): gcc-8.5.0-4.el8_5.x86_64.rpm             245 kB/s |  23 MB     01:38
--------------------------------------------------------------------------------
Total                                           354 kB/s |  34 MB     01:38
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Upgrading        : cpp-8.5.0-4.el8_5.x86_64                               1/8
  Running scriptlet: cpp-8.5.0-4.el8_5.x86_64                               1/8
  Upgrading        : libgomp-8.5.0-4.el8_5.x86_64                           2/8
  Running scriptlet: libgomp-8.5.0-4.el8_5.x86_64                           2/8
  Upgrading        : libgcc-8.5.0-4.el8_5.x86_64                            3/8
  Running scriptlet: libgcc-8.5.0-4.el8_5.x86_64                            3/8
  Upgrading        : gcc-8.5.0-4.el8_5.x86_64                               4/8
  Running scriptlet: gcc-8.5.0-4.el8_5.x86_64                               4/8
  Running scriptlet: gcc-8.5.0-3.el8.x86_64                                 5/8
  Cleanup          : gcc-8.5.0-3.el8.x86_64                                 5/8
  Running scriptlet: cpp-8.5.0-3.el8.x86_64                                 6/8
  Cleanup          : cpp-8.5.0-3.el8.x86_64                                 6/8
  Cleanup          : libgcc-8.5.0-3.el8.x86_64                              7/8
  Running scriptlet: libgcc-8.5.0-3.el8.x86_64                              7/8
  Running scriptlet: libgomp-8.5.0-3.el8.x86_64                             8/8
  Cleanup          : libgomp-8.5.0-3.el8.x86_64                             8/8
  Running scriptlet: libgomp-8.5.0-3.el8.x86_64                             8/8
  Verifying        : libgcc-8.5.0-4.el8_5.x86_64                            1/8
  Verifying        : libgcc-8.5.0-3.el8.x86_64                              2/8
  Verifying        : libgomp-8.5.0-4.el8_5.x86_64                           3/8
  Verifying        : libgomp-8.5.0-3.el8.x86_64                             4/8
  Verifying        : cpp-8.5.0-4.el8_5.x86_64                               5/8
  Verifying        : cpp-8.5.0-3.el8.x86_64                                 6/8
  Verifying        : gcc-8.5.0-4.el8_5.x86_64                               7/8
  Verifying        : gcc-8.5.0-3.el8.x86_64                                 8/8

Upgraded:
  cpp-8.5.0-4.el8_5.x86_64               gcc-8.5.0-4.el8_5.x86_64
  libgcc-8.5.0-4.el8_5.x86_64            libgomp-8.5.0-4.el8_5.x86_64

Complete!
[root@localhost yum.repos.d]# yum -y install gcc-c++
Last metadata expiration check: 0:06:19 ago on Tue 08 Nov 2022 02:41:44 PM CST.
Dependencies resolved.
================================================================================
 Package               Architecture Version               Repository       Size
================================================================================
Installing:
 gcc-c++               x86_64       8.5.0-4.el8_5         AppStream        12 M
Upgrading:
 libstdc++             x86_64       8.5.0-4.el8_5         base            453 k
Installing dependencies:
 libstdc++-devel       x86_64       8.5.0-4.el8_5         AppStream       2.0 M

Transaction Summary
================================================================================
Install  2 Packages
Upgrade  1 Package

Total download size: 15 M
Downloading Packages:
(1/3): libstdc++-8.5.0-4.el8_5.x86_64.rpm       168 kB/s | 453 kB     00:02
(2/3): libstdc++-devel-8.5.0-4.el8_5.x86_64.rpm 217 kB/s | 2.0 MB     00:09
(3/3): gcc-c++-8.5.0-4.el8_5.x86_64.rpm         219 kB/s |  12 MB     00:57
--------------------------------------------------------------------------------
Total                                           263 kB/s |  15 MB     00:57
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Upgrading        : libstdc++-8.5.0-4.el8_5.x86_64                         1/4
  Running scriptlet: libstdc++-8.5.0-4.el8_5.x86_64                         1/4
  Installing       : libstdc++-devel-8.5.0-4.el8_5.x86_64                   2/4
  Installing       : gcc-c++-8.5.0-4.el8_5.x86_64                           3/4
  Cleanup          : libstdc++-8.5.0-3.el8.x86_64                           4/4
  Running scriptlet: libstdc++-8.5.0-3.el8.x86_64                           4/4
  Verifying        : gcc-c++-8.5.0-4.el8_5.x86_64                           1/4
  Verifying        : libstdc++-devel-8.5.0-4.el8_5.x86_64                   2/4
  Verifying        : libstdc++-8.5.0-4.el8_5.x86_64                         3/4
  Verifying        : libstdc++-8.5.0-3.el8.x86_64                           4/4

Upgraded:
  libstdc++-8.5.0-4.el8_5.x86_64
Installed:
  gcc-c++-8.5.0-4.el8_5.x86_64       libstdc++-devel-8.5.0-4.el8_5.x86_64

Complete!

2、安装docker依赖

yum install -y yum-utils
[root@localhost yum.repos.d]#  yum install -y yum-utils
Last metadata expiration check: 0:00:52 ago on Tue 08 Nov 2022 02:41:44 PM CST.
Dependencies resolved.
================================================================================
 Package            Architecture    Version                 Repository     Size
================================================================================
Installing:
 yum-utils          noarch          4.0.21-3.el8            base           73 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 73 k
Installed size: 22 k
Downloading Packages:
yum-utils-4.0.21-3.el8.noarch.rpm               151 kB/s |  73 kB     00:00
--------------------------------------------------------------------------------
Total                                           151 kB/s |  73 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : yum-utils-4.0.21-3.el8.noarch                          1/1
  Running scriptlet: yum-utils-4.0.21-3.el8.noarch                          1/1
  Verifying        : yum-utils-4.0.21-3.el8.noarch                          1/1

Installed:
  yum-utils-4.0.21-3.el8.noarch

Complete!

3、设置镜像仓库

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 yum-config-manager --add-repo http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
Adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、安装docker

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
[root@localhost yum.repos.d]# sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Docker CE Stable - x86_64                                                                                                                                 37 kB/s |  31 kB     00:00
Dependencies resolved.
=========================================================================================================================================================================================
 Package                                         Architecture                 Version                                                       Repository                              Size
=========================================================================================================================================================================================
Installing:
 containerd.io                                   x86_64                       1.6.9-3.1.el8                                                 docker-ce-stable                        33 M
 docker-ce                                       x86_64                       3:20.10.21-3.el8                                              docker-ce-stable                        21 M
 docker-ce-cli                                   x86_64                       1:20.10.21-3.el8                                              docker-ce-stable                        30 M
 docker-compose-plugin                           x86_64                       2.12.2-3.el8                                                  docker-ce-stable                        10 M
Installing dependencies:
 container-selinux                               noarch                       2:2.167.0-1.module_el8.5.0+911+f19012f9                       AppStream                               54 k
 docker-ce-rootless-extras                       x86_64                       20.10.21-3.el8                                                docker-ce-stable                       4.6 M
 fuse-overlayfs                                  x86_64                       1.7.1-1.module_el8.5.0+890+6b136101                           AppStream                               73 k
 fuse3                                           x86_64                       3.2.1-12.el8                                                  base                                    50 k
 fuse3-libs                                      x86_64                       3.2.1-12.el8                                                  base                                    94 k
 libslirp                                        x86_64                       4.4.0-1.module_el8.5.0+890+6b136101                           AppStream                               70 k
 slirp4netns                                     x86_64                       1.1.8-1.module_el8.5.0+890+6b136101                           AppStream                               51 k
Installing weak dependencies:
 docker-scan-plugin                              x86_64                       0.21.0-3.el8                                                  docker-ce-stable                       3.8 M
Enabling module streams:
 container-tools                                                              rhel8

Transaction Summary
=========================================================================================================================================================================================
Install  12 Packages

Total download size: 103 M
Installed size: 394 M
Is this ok [y/N]: y
Downloading Packages:
(1/12): fuse3-3.2.1-12.el8.x86_64.rpm                                                                                                                     85 kB/s |  50 kB     00:00
(2/12): container-selinux-2.167.0-1.module_el8.5.0+911+f19012f9.noarch.rpm                                                                                88 kB/s |  54 kB     00:00
(3/12): fuse3-libs-3.2.1-12.el8.x86_64.rpm                                                                                                               113 kB/s |  94 kB     00:00
(4/12): fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64.rpm                                                                                    162 kB/s |  73 kB     00:00
(5/12): libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64.rpm                                                                                          131 kB/s |  70 kB     00:00
(6/12): slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64.rpm                                                                                       124 kB/s |  51 kB     00:00
(7/12): docker-ce-20.10.21-3.el8.x86_64.rpm                                                                                                              167 kB/s |  21 MB     02:09
(8/12): docker-ce-rootless-extras-20.10.21-3.el8.x86_64.rpm                                                                                              156 kB/s | 4.6 MB     00:30
(9/12): docker-ce-cli-20.10.21-3.el8.x86_64.rpm                                                                                                          160 kB/s |  30 MB     03:08
(10/12): docker-scan-plugin-0.21.0-3.el8.x86_64.rpm                                                                                                      164 kB/s | 3.8 MB     00:23
(11/12): containerd.io-1.6.9-3.1.el8.x86_64.rpm                                                                                                          158 kB/s |  33 MB     03:33
(12/12): docker-compose-plugin-2.12.2-3.el8.x86_64.rpm                                                                                                   162 kB/s |  10 MB     01:04
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                    466 kB/s | 103 MB     03:45
Docker CE Stable - x86_64                                                                                                                                5.8 kB/s | 1.6 kB     00:00
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
 From       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                 1/1
  Installing       : docker-scan-plugin-0.21.0-3.el8.x86_64                                                                                                                         1/12
  Running scriptlet: docker-scan-plugin-0.21.0-3.el8.x86_64                                                                                                                         1/12
  Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch                                                                                               2/12
  Installing       : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch                                                                                               2/12
  Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch                                                                                               2/12
  Installing       : containerd.io-1.6.9-3.1.el8.x86_64                                                                                                                             3/12
  Running scriptlet: containerd.io-1.6.9-3.1.el8.x86_64                                                                                                                             3/12
  Installing       : docker-ce-cli-1:20.10.21-3.el8.x86_64                                                                                                                          4/12
  Running scriptlet: docker-ce-cli-1:20.10.21-3.el8.x86_64                                                                                                                          4/12
  Installing       : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64                                                                                                            5/12
  Installing       : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64                                                                                                         6/12
  Installing       : fuse3-libs-3.2.1-12.el8.x86_64                                                                                                                                 7/12
  Running scriptlet: fuse3-libs-3.2.1-12.el8.x86_64                                                                                                                                 7/12
  Installing       : fuse3-3.2.1-12.el8.x86_64                                                                                                                                      8/12
  Installing       : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64                                                                                                      9/12
  Running scriptlet: fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64                                                                                                      9/12
  Installing       : docker-ce-3:20.10.21-3.el8.x86_64                                                                                                                             10/12
  Running scriptlet: docker-ce-3:20.10.21-3.el8.x86_64                                                                                                                             10/12
  Installing       : docker-ce-rootless-extras-20.10.21-3.el8.x86_64                                                                                                               11/12
  Running scriptlet: docker-ce-rootless-extras-20.10.21-3.el8.x86_64                                                                                                               11/12
  Installing       : docker-compose-plugin-2.12.2-3.el8.x86_64                                                                                                                     12/12
  Running scriptlet: docker-compose-plugin-2.12.2-3.el8.x86_64                                                                                                                     12/12
  Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch                                                                                              12/12
  Running scriptlet: docker-compose-plugin-2.12.2-3.el8.x86_64                                                                                                                     12/12
  Verifying        : fuse3-3.2.1-12.el8.x86_64                                                                                                                                      1/12
  Verifying        : fuse3-libs-3.2.1-12.el8.x86_64                                                                                                                                 2/12
  Verifying        : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch                                                                                               3/12
  Verifying        : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64                                                                                                      4/12
  Verifying        : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64                                                                                                            5/12
  Verifying        : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64                                                                                                         6/12
  Verifying        : containerd.io-1.6.9-3.1.el8.x86_64                                                                                                                             7/12
  Verifying        : docker-ce-3:20.10.21-3.el8.x86_64                                                                                                                              8/12
  Verifying        : docker-ce-cli-1:20.10.21-3.el8.x86_64                                                                                                                          9/12
  Verifying        : docker-ce-rootless-extras-20.10.21-3.el8.x86_64                                                                                                               10/12
  Verifying        : docker-compose-plugin-2.12.2-3.el8.x86_64                                                                                                                     11/12
  Verifying        : docker-scan-plugin-0.21.0-3.el8.x86_64                                                                                                                        12/12

Installed:
  container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch   containerd.io-1.6.9-3.1.el8.x86_64                          docker-ce-3:20.10.21-3.el8.x86_64
  docker-ce-cli-1:20.10.21-3.el8.x86_64                              docker-ce-rootless-extras-20.10.21-3.el8.x86_64             docker-compose-plugin-2.12.2-3.el8.x86_64
  docker-scan-plugin-0.21.0-3.el8.x86_64                             fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64   fuse3-3.2.1-12.el8.x86_64
  fuse3-libs-3.2.1-12.el8.x86_64                                     libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64         slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64

Complete

CentOS 8 中安装 Docker 出现和 Podman 冲突的问题

root@monitor ~]# yum install docker-ce docker-ce-cli containerd.io
CentOS Linux 8 - AppStream                      669 kB/s | 6.2 MB     00:09    
CentOS Linux 8 - BaseOS                         351 kB/s | 2.3 MB     00:06    
Docker CE Stable - x86_64                       1.1 kB/s | 7.1 kB     00:06    
Docker CE Nightly - x86_64                      327  B/s | 2.6 kB     00:08    
Error:
 Problem 1: problem with installed package podman-2.0.5-5.module_el8.3.0+512+b3b58dca.x86_64
  - package podman-2.0.5-5.module_el8.3.0+512+b3b58dca.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
  - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - cannot install the best candidate for the job
  - package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering
 Problem 2: problem with installed package buildah-1.15.1-2.module_el8.3.0+475+c50ce30b.x86_64
  - package buildah-1.15.1-2.module_el8.3.0+475+c50ce30b.x86_64 requires runc >= 1.0.0-26, but none of the providers can be installed
  - package docker-ce-3:20.10.1-3.el8.x86_64 requires containerd.io >= 1.4.1, but none of the providers can be installed
  - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.1-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.1-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.3-3.el8.x86_64 conflicts with runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - package containerd.io-1.4.3-3.el8.x86_64 obsoletes runc provided by runc-1.0.0-68.rc92.module_el8.3.0+475+c50ce30b.x86_64
  - cannot install the best candidate for the job
  - package runc-1.0.0-56.rc5.dev.git2abd837.module_el8.3.0+569+1bada2e4.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering

解决方法

yum erase podman buildah

img

img

5.配置阿里云镜像加速器

1)登录阿里云
阿里云URL
2)控制台->容器镜像服务
执行命令

img

测试:

[root@localhost yum.repos.d]# sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

5、启动docker

[root@localhost yum.repos.d]# sudo systemctl start docker

6、测试

[root@localhost yum.repos.d]#  sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f352a06974245fe7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

二、常用命令

1、帮助启动类命令

1、启动docker

systemctl start docker

2、停止docker

systemctl stop docker

3、查看docker状态

systemctl status docker

4、重启

systemctl restart docker

5、开机启动

systemctl enable docker

6、查看信息

docker info

7、查看帮助文档

docker --help

8、查看具体帮助文档

docker  命令 --help

2、镜像命令

1、列出本地主机上的镜像

docker images [-a] [-q]
[root@localhost yum.repos.d]# docker images -a
仓库源        镜像的版本    镜像id     镜像创建时间     镜像大小
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   13 months ago   13.3kB

2、查询镜像

[root@localhost /]# docker search --limit 5 redis
名字                      说明描述                                        点赞        是否官方 
NAME                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED(自动构建)
redis                    Redis is an open source key-value store that…   11537     [OK]
redislabs/redisinsight   RedisInsight - The GUI for Redis                69
redislabs/redisearch     Redis With the RedisSearch module pre-loaded…   55
redislabs/redis          Clustered in-memory database engine compatib…   35
redislabs/rebloom        A probablistic datatypes module for Redis       21                   [OK]

3.拉取镜像

[root@localhost /]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
tomcat        latest    fb5657adc892   10 months ago   680MB
redis         latest    7614ae9453d1   10 months ago   113MB
hello-world   latest    feb5d9fea6a5   13 months ago   13.3kB

4.查看镜像、容器、数据卷所占空间

[root@localhost /]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          3         1         792.3MB   792.3MB (99%)
Containers      2         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B

5.删除镜像

[root@localhost /]# docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f352a06974245fe7
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@localhost /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       latest    fb5657adc892   10 months ago   680MB
redis        latest    7614ae9453d1   10 months ago   113MB

3、容器命令

1、拉取CentOS镜像

[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
tomcat       latest    fb5657adc892   10 months ago   680MB
redis        latest    7614ae9453d1   10 months ago   113MB
centos       latest    5d0da3dc9764   13 months ago   231MB
[root@localhost /]#

2、启动一个CentOS容器

[root@localhost /]# docker run -it  centos
[root@7b11c8d2b38d /]# ls -al
total 0
drwxr-xr-x.   1 root root   6 Nov  9 06:10 .
drwxr-xr-x.   1 root root   6 Nov  9 06:10 ..
-rwxr-xr-x.   1 root root   0 Nov  9 06:10 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Nov  9 06:10 dev
drwxr-xr-x.   1 root root  66 Nov  9 06:10 etc
drwxr-xr-x.   2 root root   6 Nov  3  2020 home
lrwxrwxrwx.   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------.   2 root root   6 Sep 15  2021 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 221 root root   0 Nov  9 06:10 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 Nov  9 03:00 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var
[root@7b11c8d2b38d /]# ^C
[root@7b11c8d2b38d /]# exit
exit
[root@localhost /]#

-i:以交互模式运行容器,通常与 -t 同时使用;

-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;

也即启动交互式容器(前台有伪终端,等待交互);

3、列出当前正在运行的容器

docker ps [options]

OPTIONS说明(常用):

-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。

4、退出容器

1) exit  退出容器的同事通知容器
2) ctrl+p+q   退出不停止

5.停止容器

docker stop [容器ID或者容器名字]
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
b911f3712135   centos    "/bin/bash"   2 minutes ago   Up 2 minutes             agitated_jepsen
[root@localhost ~]# docker stop b911f3712135
b911f3712135
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#

6.启动一个已经停止的容器

docker start [容器ID]

7、重启容器

docker restart [容器ID]

8.强制停止容器

docker kill [容器ID]

9、删除已经停止的容器

docker rm [容器ID]

10.启动一个守护式容器

[root@localhost ~]# docker run -d --name tomcat-2 tomcat
1189b4a1f588438a25d05bb4a4f06e0216c8e7ad7dd196b6e56711a17a39fd15
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS      NAMES
1189b4a1f588   tomcat    "catalina.sh run"   5 seconds ago   Up 5 seconds   8080/tcp   tomcat-2

11.查看容器日志

docker logs [容器ID]

12.进入到容器内部

docker exec -it [容器ID] /bin/bash

13.拷贝容器内部文件到宿主机

docker exec -it [容器ID] /bin/bash
[root@localhost /]# docker cp b8bf10e3500e:/usr/local/tomcat/a.txt /a.txt

14.导入导出容器

docker export b8bf10e3500e > tomcat-1.tar.gz
cat ./tomcat-1.tar.gz | docker import - qlx/tomcat-1:1.0.0
docker run -d  --name tomcat1-0 tomcat1 /bin/bash
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                                 NAMES
3cc1a39443d9   tomcat    "catalina.sh run"   32 minutes ago   Up 32 minutes   8080/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   tomcat-2
b8bf10e3500e   tomcat    "catalina.sh run"   34 minutes ago   Up 34 minutes   8080/tcp                                              tomcat-1
[root@localhost /]# docker export b8bf10e3500e > tomcat-1.tar.gz
[root@localhost /]# ls -al
total 667368
dr-xr-xr-x.  18 root root      4096 Nov  9 15:38 .
dr-xr-xr-x.  18 root root      4096 Nov  9 15:38 ..
-rw-r--r--.   1 root root         4 Oct 28 16:06 a
-rw-r--r--.   1 root root         7 Nov  9 15:11 a.txt
-rw-r--r--.   1 root root         4 Oct 28 16:07 b
lrwxrwxrwx.   1 root root         7 Jun 22  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root      4096 Oct 25 14:20 boot
drwxr-xr-x.   6 root root       219 Oct 27 11:45 data
drwxr-xr-x.  19 root root      3160 Nov  9 11:01 dev
drwxr-xr-x. 151 root root      8192 Nov  9 11:01 etc
drwxr-xr-x.   3 root root        17 Oct 25 14:19 home
lrwxrwxrwx.   1 root root         7 Jun 22  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root         9 Jun 22  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root         6 Jun 22  2021 media
drwxr-xr-x.   3 root root        18 Oct 25 14:11 mnt
drwxr-xr-x.   3 root root        24 Nov  8 15:12 opt
dr-xr-xr-x. 265 root root         0 Nov  9 11:00 proc
dr-xr-x---.  16 root root      4096 Nov  9 14:53 root
drwxr-xr-x.  42 root root      1260 Nov  9 15:26 run
lrwxrwxrwx.   1 root root         8 Jun 22  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root         6 Jun 22  2021 srv
dr-xr-xr-x.  13 root root         0 Nov  9 11:00 sys
drwxrwxrwt.  17 root root      4096 Nov  9 15:37 tmp
-rw-r--r--.   1 root root 683334144 Nov  9 15:40 tomcat-1.tar.gz
drwxr-xr-x.  13 root root       158 Oct 25 14:10 usr
drwxr-xr-x.  22 root root      4096 Oct 25 14:20 var
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                                 NAMES
3cc1a39443d9   tomcat    "catalina.sh run"   34 minutes ago   Up 34 minutes   8080/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   tomcat-2
b8bf10e3500e   tomcat    "catalina.sh run"   36 minutes ago   Up 36 minutes   8080/tcp                                              tomcat-1
[root@localhost /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       latest    fb5657adc892   10 months ago   680MB
redis        latest    7614ae9453d1   10 months ago   113MB
centos       latest    5d0da3dc9764   13 months ago   231MB
[root@localhost /]# docker stop b8bf10e3500e
b8bf10e3500e
[root@localhost /]# docker rm b8bf10e3500e
b8bf10e3500e
[root@localhost /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                                 NAMES
3cc1a39443d9   tomcat    "catalina.sh run"   34 minutes ago   Up 34 minutes   8080/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   tomcat-2
[root@localhost /]# cat ./tomcat-1.tar.gz | docker import - qlx/tomcat-1:1.0.0
sha256:1803b1bc12e55fd211b871de704a123224f211fb4d9431c5de41d336902d3da4
[root@localhost /]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
qlx/tomcat-1   1.0.0     1803b1bc12e5   10 seconds ago   672MB
tomcat         latest    fb5657adc892   10 months ago    680MB
redis          latest    7614ae9453d1   10 months ago    113MB
centos         latest    5d0da3dc9764   13 months ago    231MB
[root@localhost /]#

三.linux下docker的卸载!

1、首先关闭docker容器

#systemctl status docker 查看docker的状态 
#systemctl stop  docker  停止docker容器 

在这里插入图片描述

2、查看安装docker的文件包

#docker list installed |grep docker 

在这里插入图片描述

利用rpm -qa查看docker的源文件

#rpm -qa |grep docker 

在这里插入图片描述

3、删除所有docker包

#yum -y remove docker*.x86_64 

在这里插入图片描述

4、删除docker的镜像文件!

默认存在目录是:cd /var/lib/docker

#cd /var/lib/docker 
#rm -rf /var/lib/docker 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值