centos8安装docker

Posted PacosonSWJTU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos8安装docker相关的知识,希望对你有一定的参考价值。

【README】本文参考了 docker官方文档安装指南, Install Docker Engine on CentOS | Docker DocumentationInstructions for installing Docker Engine on CentOShttps://docs.docker.com/engine/install/centos/


【1】安装前的工作

1.需要centos7或8上;

// 查看centos版本
[root@centos204 ~]# uname -r
4.18.0-240.el8.x86_64
[root@centos204 ~]# uname -a
Linux centos204 4.18.0-240.el8.x86_64 #1 SMP Fri Sep 25 19:48:47 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

2. 卸载旧版本的docker,若有的话;

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

/var/lib/docker 目录中的内容,包括镜像,容器,卷和网络,是受保护的; 现在 docker引擎包称为 docker-ce;

3.docker安装方式有很多种,官方推荐使用 docker 仓库安装,以便于安装和升级工作;参见

Install Docker Engine on CentOS | Docker Documentationhttps://docs.docker.com/engine/install/centos/#install-using-the-repository


【2】基于docker仓库安装docker

在你第一次安装docker引擎到新机器前,你需要安装docker仓库;此后,你可以从仓库中安装和升级docker;

步骤1,安装docker仓库 ;安装 yum-utils 包,提供了 yum-config-manager工具,使用它安装稳定的docker仓库;

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

可选:启用夜间或测试仓库;

这些仓库包含在 上述文件docker.repo中,但默认是禁用的;你可以顺着稳定的版本启用它;

启用命令如下:

 启用夜间仓库

 sudo yum-config-manager --enable docker-ce-nightly

启用测试仓库 

 sudo yum-config-manager --enable docker-ce-test

 你也可以使用 disable 禁用他们

 sudo yum-config-manager --disable docker-ce-nightly

步骤2,安装docker引擎

安装最新版的docker引擎;或者可以安装特定版本的docker引擎;

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

安装特定版本的docker;

1.列出docker引擎版本;

yum list docker-ce --showduplicates | sort -r

2.安装特定版本

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

步骤3, 启动docker;

 sudo systemctl start docker

步骤4,运行以下命令验证docker安装正确

 安装成功了;

补充:docker升级;

要升级 Docker 引擎,请下载更新的包文件并重复安装过程 Install Docker Engine on CentOS | Docker Documentation,使用 yum -y upgrade 而不是 yum -y install,并指向新文件。


【3】验证docker

1)验证版本

[root@centos204 ~]# docker --version
Docker version 20.10.9, build c2ea9bc

2)配置docker中国区私有仓库

# 配置中国区私有仓库
vim /etc/docker/daemon.json 

{
        "registry-mirrors":["https://registry.docker-cn.com"]
}

注意registry-mirrors与[]间 不能有空格

3)启动docker

systemctl start docker

4)查看docker状态

[root@centos204 docker]# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
// running 证明运行成功了 bingo
   Active: active (running) (thawing) since Thu 2021-10-14 11:00:45 EDT; 20s ago
     Docs: https://docs.docker.com
 Main PID: 14924 (dockerd)
    Tasks: 8
   Memory: 60.2M
   CGroup: /system.slice/docker.service
           └─14924 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Oct 14 11:00:43 centos204 dockerd[14924]: time="2021-10-14T11:00:43.287011628-04:00" level=info msg="Firewalld: docker zone already exists, returning"
Oct 14 11:00:44 centos204 dockerd[14924]: time="2021-10-14T11:00:44.042473792-04:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Oct 14 11:00:44 centos204 dockerd[14924]: time="2021-10-14T11:00:44.081104218-04:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Oct 14 11:00:44 centos204 dockerd[14924]: time="2021-10-14T11:00:44.350028734-04:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used>
Oct 14 11:00:44 centos204 dockerd[14924]: time="2021-10-14T11:00:44.517659434-04:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Oct 14 11:00:44 centos204 dockerd[14924]: time="2021-10-14T11:00:44.647868302-04:00" level=info msg="Loading containers: done."
Oct 14 11:00:45 centos204 dockerd[14924]: time="2021-10-14T11:00:45.016948340-04:00" level=info msg="Docker daemon" commit=79ea9d3 graphdriver(s)=overlay2 version=20.10.9
Oct 14 11:00:45 centos204 dockerd[14924]: time="2021-10-14T11:00:45.017100694-04:00" level=info msg="Daemon has completed initialization"
Oct 14 11:00:45 centos204 systemd[1]: Started Docker Application Container Engine.
Oct 14 11:00:45 centos204 dockerd[14924]: time="2021-10-14T11:00:45.111491314-04:00" level=info msg="API listen on /var/run/docker.sock"

5)测试案例:从docker仓库中查询mysql镜像

[root@centos204 docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11533     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4381      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   853                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   91                   
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   88                   
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                   [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         51                   
prom/mysqld-exporter                                                              43                   [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                   [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                   
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   32                   
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   31                   [OK]
mysql/mysql-router                MySQL Router provides transparent routing be…   23                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                   
arey/mysql-client                 Run a MySQL client from a docker container      19                   [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   16                   [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   7                    [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  7                    [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                    
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                    
idoall/mysql                      MySQL is a widely used, open-source relation…   3                    [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                    [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   2                    
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                    [OK]
centos/mysql-80-centos7           MySQL 8.0 SQL database server              

以上是关于centos8安装docker的主要内容,如果未能解决你的问题,请参考以下文章

centos8 安装原生docker

Centos8下安装docker

CentOS8快速安装Docker

CentOS8快速安装Docker

CentOS8.1安装docker

Centos8 安装docker