回到顶部

Contents

centos docker 安装和使用

卸载旧版本

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

使用 Docker 仓库进行安装

阿里云源

sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

清华源

sudo yum-config-manager \
    --add-repo \
    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

官方源(可能很慢)

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker Engine-Community

中途若提示您接受 GPG 密钥,请选是。

sudo yum install docker-ce docker-ce-cli containerd.io

存储库中列出可用版本

此示例按版本号(从高到低)对结果进行排序。

yum list docker-ce --showduplicates | sort -r
Installed Packages
docker-ce.x86_64            3:19.03.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.12-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.12-3.el7                   @docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.10-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
Available Packages

安装特定版本

该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如: docker-ce-18.06.3.ce-3.el7

sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
sudo yum install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io

启动 docker

sudo systemctl start docker

验证安装了 Docker Engine-Community

通过运行 hello-world 映像来验证

sudo docker run hello-world

安装日志

[root@izj6c5nf8wcjt6n2zcxwegz ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        111M        339M        380K        1.4G        1.5G
Swap:            0B          0B          0B
[root@izj6c5nf8wcjt6n2zcxwegz ~]# sudo yum install -y yum-utils \
>   device-mapper-persistent-data \
>   lvm2
Loaded plugins: fastestmirror
base                                                                                               | 3.6 kB  00:00:00     
docker-ce-stable                                                                                   | 3.5 kB  00:00:00     
epel                                                                                               | 4.7 kB  00:00:00     
extras                                                                                             | 2.9 kB  00:00:00     
updates                                                                                            | 2.9 kB  00:00:00     
Loading mirror speeds from cached hostfile
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package device-mapper-persistent-data.x86_64 0:0.7.3-3.el7 will be updated
---> Package device-mapper-persistent-data.x86_64 0:0.8.5-2.el7 will be an update
---> Package lvm2.x86_64 7:2.02.180-10.el7_6.2 will be updated
---> Package lvm2.x86_64 7:2.02.186-7.el7_8.2 will be an update
--> Processing Dependency: lvm2-libs = 7:2.02.186-7.el7_8.2 for package: 7:lvm2-2.02.186-7.el7_8.2.x86_64
--> Running transaction check
---> Package lvm2-libs.x86_64 7:2.02.180-10.el7_6.2 will be updated
---> Package lvm2-libs.x86_64 7:2.02.186-7.el7_8.2 will be an update
--> Processing Dependency: device-mapper-event = 7:1.02.164-7.el7_8.2 for package: 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.149-10.el7_6.2 will be updated
---> Package device-mapper-event.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Processing Dependency: device-mapper-event-libs = 7:1.02.164-7.el7_8.2 for package: 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64
--> Processing Dependency: device-mapper = 7:1.02.164-7.el7_8.2 for package: 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64
--> Running transaction check
---> Package device-mapper.x86_64 7:1.02.149-10.el7_6.2 will be updated
--> Processing Dependency: device-mapper = 7:1.02.149-10.el7_6.2 for package: 7:device-mapper-libs-1.02.149-10.el7_6.2.x86_64
---> Package device-mapper.x86_64 7:1.02.164-7.el7_8.2 will be an update
---> Package device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.2 will be updated
---> Package device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Running transaction check
---> Package device-mapper-libs.x86_64 7:1.02.149-10.el7_6.2 will be updated
---> Package device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================
 Package                                   Arch               Version                           Repository           Size
==========================================================================================================================
Updating:
 device-mapper-persistent-data             x86_64             0.8.5-2.el7                       base                422 k
 lvm2                                      x86_64             7:2.02.186-7.el7_8.2              updates             1.3 M
Updating for dependencies:
 device-mapper                             x86_64             7:1.02.164-7.el7_8.2              updates             295 k
 device-mapper-event                       x86_64             7:1.02.164-7.el7_8.2              updates             191 k
 device-mapper-event-libs                  x86_64             7:1.02.164-7.el7_8.2              updates             190 k
 device-mapper-libs                        x86_64             7:1.02.164-7.el7_8.2              updates             324 k
 lvm2-libs                                 x86_64             7:2.02.186-7.el7_8.2              updates             1.1 M

Transaction Summary
==========================================================================================================================
Upgrade  2 Packages (+5 Dependent packages)

Total download size: 3.8 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/7): device-mapper-event-1.02.164-7.el7_8.2.x86_64.rpm                                           | 191 kB  00:00:00     
(2/7): device-mapper-1.02.164-7.el7_8.2.x86_64.rpm                                                 | 295 kB  00:00:00     
(3/7): device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64.rpm                                      | 190 kB  00:00:00     
(4/7): device-mapper-libs-1.02.164-7.el7_8.2.x86_64.rpm                                            | 324 kB  00:00:00     
(5/7): lvm2-libs-2.02.186-7.el7_8.2.x86_64.rpm                                                     | 1.1 MB  00:00:00     
(6/7): lvm2-2.02.186-7.el7_8.2.x86_64.rpm                                                          | 1.3 MB  00:00:00     
(7/7): device-mapper-persistent-data-0.8.5-2.el7.x86_64.rpm                                        | 422 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------
Total                                                                                      16 MB/s | 3.8 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 7:device-mapper-1.02.164-7.el7_8.2.x86_64                                                             1/14 
  Updating   : 7:device-mapper-libs-1.02.164-7.el7_8.2.x86_64                                                        2/14 
  Updating   : 7:device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64                                                  3/14 
  Updating   : 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64                                                       4/14 
  Updating   : 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64                                                                 5/14 
  Updating   : device-mapper-persistent-data-0.8.5-2.el7.x86_64                                                      6/14 
  Updating   : 7:lvm2-2.02.186-7.el7_8.2.x86_64                                                                      7/14 
  Cleanup    : 7:lvm2-2.02.180-10.el7_6.2.x86_64                                                                     8/14 
  Cleanup    : 7:lvm2-libs-2.02.180-10.el7_6.2.x86_64                                                                9/14 
  Cleanup    : 7:device-mapper-event-1.02.149-10.el7_6.2.x86_64                                                     10/14 
  Cleanup    : 7:device-mapper-event-libs-1.02.149-10.el7_6.2.x86_64                                                11/14 
  Cleanup    : 7:device-mapper-1.02.149-10.el7_6.2.x86_64                                                           12/14 
  Cleanup    : 7:device-mapper-libs-1.02.149-10.el7_6.2.x86_64                                                      13/14 
  Cleanup    : device-mapper-persistent-data-0.7.3-3.el7.x86_64                                                     14/14 
  Verifying  : device-mapper-persistent-data-0.8.5-2.el7.x86_64                                                      1/14 
  Verifying  : 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64                                                                 2/14 
  Verifying  : 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64                                                       3/14 
  Verifying  : 7:device-mapper-libs-1.02.164-7.el7_8.2.x86_64                                                        4/14 
  Verifying  : 7:lvm2-2.02.186-7.el7_8.2.x86_64                                                                      5/14 
  Verifying  : 7:device-mapper-1.02.164-7.el7_8.2.x86_64                                                             6/14 
  Verifying  : 7:device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64                                                  7/14 
  Verifying  : device-mapper-persistent-data-0.7.3-3.el7.x86_64                                                      8/14 
  Verifying  : 7:device-mapper-event-1.02.149-10.el7_6.2.x86_64                                                      9/14 
  Verifying  : 7:device-mapper-1.02.149-10.el7_6.2.x86_64                                                           10/14 
  Verifying  : 7:lvm2-libs-2.02.180-10.el7_6.2.x86_64                                                               11/14 
  Verifying  : 7:lvm2-2.02.180-10.el7_6.2.x86_64                                                                    12/14 
  Verifying  : 7:device-mapper-libs-1.02.149-10.el7_6.2.x86_64                                                      13/14 
  Verifying  : 7:device-mapper-event-libs-1.02.149-10.el7_6.2.x86_64                                                14/14 

Updated:
  device-mapper-persistent-data.x86_64 0:0.8.5-2.el7                   lvm2.x86_64 7:2.02.186-7.el7_8.2                  

Dependency Updated:
  device-mapper.x86_64 7:1.02.164-7.el7_8.2                      device-mapper-event.x86_64 7:1.02.164-7.el7_8.2          
  device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2           device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2           
  lvm2-libs.x86_64 7:2.02.186-7.el7_8.2                         

Complete!
[root@izj6c5nf8wcjt6n2zcxwegz ~]# sudo yum-config-manager \
>     --add-repo \
>     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@izj6c5nf8wcjt6n2zcxwegz ~]# sudo yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.0-3.el7 will be updated
---> Package containerd.io.x86_64 0:1.2.13-3.2.el7 will be an update
---> Package docker-ce.x86_64 3:18.09.0-3.el7 will be updated
---> Package docker-ce.x86_64 3:19.03.12-3.el7 will be an update
---> Package docker-ce-cli.x86_64 1:18.09.0-3.el7 will be updated
---> Package docker-ce-cli.x86_64 1:19.03.12-3.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================
 Package                      Arch                  Version                         Repository                       Size
==========================================================================================================================
Updating:
 containerd.io                x86_64                1.2.13-3.2.el7                  docker-ce-stable                 25 M
 docker-ce                    x86_64                3:19.03.12-3.el7                docker-ce-stable                 24 M
 docker-ce-cli                x86_64                1:19.03.12-3.el7                docker-ce-stable                 38 M

Transaction Summary
==========================================================================================================================
Upgrade  3 Packages

Total download size: 88 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/3): docker-ce-19.03.12-3.el7.x86_64.rpm                                                         |  24 MB  00:00:01     
(2/3): docker-ce-cli-19.03.12-3.el7.x86_64.rpm                                                     |  38 MB  00:00:03     
(3/3): containerd.io-1.2.13-3.2.el7.x86_64.rpm                                                     |  25 MB  00:00:05     
--------------------------------------------------------------------------------------------------------------------------
Total                                                                                      17 MB/s |  88 MB  00:00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:docker-ce-cli-19.03.12-3.el7.x86_64                                                                  1/6 
  Updating   : containerd.io-1.2.13-3.2.el7.x86_64                                                                    2/6 
  Updating   : 3:docker-ce-19.03.12-3.el7.x86_64                                                                      3/6 
/usr/bin/dockerd has not been configured as an alternative for dockerd
  Cleanup    : 3:docker-ce-18.09.0-3.el7.x86_64                                                                       4/6 
  Cleanup    : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                                                   5/6 
  Cleanup    : containerd.io-1.2.0-3.el7.x86_64                                                                       6/6 
  Verifying  : containerd.io-1.2.13-3.2.el7.x86_64                                                                    1/6 
  Verifying  : 1:docker-ce-cli-19.03.12-3.el7.x86_64                                                                  2/6 
  Verifying  : 3:docker-ce-19.03.12-3.el7.x86_64                                                                      3/6 
  Verifying  : 3:docker-ce-18.09.0-3.el7.x86_64                                                                       4/6 
  Verifying  : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                                                   5/6 
  Verifying  : containerd.io-1.2.0-3.el7.x86_64                                                                       6/6 

Updated:
  containerd.io.x86_64 0:1.2.13-3.2.el7    docker-ce.x86_64 3:19.03.12-3.el7    docker-ce-cli.x86_64 1:19.03.12-3.el7   

Complete!
[root@izj6c5nf8wcjt6n2zcxwegz ~]# yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
docker-ce.x86_64            3:19.03.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.12-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.12-3.el7                   @docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.10-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
Available Packages
[root@izj6c5nf8wcjt6n2zcxwegz ~]# 
[root@izj6c5nf8wcjt6n2zcxwegz ~]# sudo yum install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.ioLoaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package matching docker-ce-18.06.3.ce-3.el7.x86_64 already installed. Checking for update.
No package docker-ce-cli-18.06.3.ce-3.el7 available.
Package containerd.io-1.2.13-3.2.el7.x86_64 already installed and latest version
Nothing to do
[root@izj6c5nf8wcjt6n2zcxwegz ~]# sudo systemctl start docker
[root@izj6c5nf8wcjt6n2zcxwegz ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        176M        145M        388K        1.5G        1.4G
Swap:            0B          0B          0B

列出和删除 image 文件

# 列出本机的所有 image 文件。
$ docker image ls

# 删除 image 文件
$ docker image rm [imageName]

拉取 image 文件

docker image pull library/hello-world

上面代码中,docker image pull是抓取 image 文件的命令。library/hello-world是 image 文件在仓库里面的位置,其中library是 image 文件所在的组,hello-world是 image 文件的名字。

由于 Docker 官方提供的 image 文件,都放在library组里面,所以它的是默认组,可以省略。因此,上面的命令可以写成下面这样。

docker image pull hello-world

运行 image 文件

docker container run hello-world

docker container run 命令会从 image 文件,生成一个正在运行的容器实例。

注意,docker container run命令具有自动抓取 image 文件的功能。如果发现本地没有指定的 image 文件,就会从仓库自动抓取。因此,前面的 docker image pull 命令并不是必需的步骤。

列出和删除容器文件

# 列出本机正在运行的容器
$ docker container ls

# 列出本机所有容器,包括终止运行的容器
$ docker container ls --all

# 删除指定容器 ID 容器文件
$ docker container rm [containerID]

终止或停止容器文件

# 相当于向容器里面的主进程发出 SIGKILL 信号
docker container kill [containID]

# 相当于向容器里面的主进程发出 SIGTERM 信号,然后过一段时间再发出 SIGKILL 信号
docker container stop [containerID]

Compose 安装

https://www.runoob.com/docker/docker-compose.html


^_^
请喝咖啡 ×

文章部分资料可能来源于网络,如有侵权请告知删除。谢谢!
前一篇: 作品署名申请
下一篇: Locust package has moved from 'locustio' to 'locust'. Please update your reference (or pin your version to 0.14.6 if you dont want to update to 1.0)
captcha