Docker的安装、配置及其基本使用

1、安装平台

Linux虚拟机:VirtualBox上安装的CentOS7
注意:CentOS7的内核要在3.10以上才能安装docker,查看内核命令:

  uname -r

若不满足要求,可更新内核,但该步耗时较长,谨慎使用,更新命令如下:

   yum update

2、安装过程

  • ① 安装需要的软件包:
  sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  • ② 设置yum源
   sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • ③ 查看仓库中docker版本
  yum list docker-ce --showduplicates | sort -r
  • ④ 安装docker
   sudo yum install docker-ce

为什么要如此复杂的去安装docker,是因为使用简单命令yum install dacker进行安装时,往往只安装了docker的客户端,没有安装docker的服务器端,所以使用以上流程进行安装。

  • ⑤ 安装后查看docker版本
root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

安装之后,查看版本情况如上。

3、docker使用

  • ① 启动docker并设置开机启动
  sudo systemctl start docker
  sudo systemctl enable docker
  • ② 在docker启动后,执行docker search mysql之前需要先进行以下配置:

(1)改变daemon.json文件,具体操作如下:

命令行中输入以下命令,进入daemon.json文件,进行对其修改:

  [root@localhost ~]# vi /etc/docker/daemon.json

进入daemon.json文件后,若为空,直接将以下内容复制进去

//从docker官方网站上下载各种镜像资源
 {
     
  "registry-mirrors": ["https://registry.docker-cn.com"]
 }

若使用官方镜像下载速度过慢,可更换为阿里云的镜像资源库进行下载:

{
     
 "registry-mirrors":["https://khec465u.mirror.aliyuncs.com"]
}

复制完成后,按Esc,再输入 :wq ,保存并退出。

(2)改变服务器DNS网络配置

  //输入该命令,进入resolv.conf的配置文件
  vi /etc/resolv.conf

进入resolv.conf的配置文件后,将其中内容全部清除,复制以下内容:

  nameserver 8.8.8.8
  nameserver 8.8.8.4

复制完成后,按Esc,再输入 :wq ,保存并退出,需要注意的是,该文件每次重启都会改变,若出现``timeout情况,就可以尝试去更改resolv.conf```的配置文件。

  • ③ 输入docker search mysql命令,查看mysql版本,结果如下:
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9269                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3308                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   681                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   71                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   66                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      36                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   24                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   14                                      
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         13                                      
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
  • ④ 输入docker pull mysql命令,下载mysql最新版本,结果如下:
Using default tag: latest
68ced04f60ab: Pull complete 
f9748e016a5c: Pull complete 
da54b038fed1: Pull complete 
6895ec5eb2c0: Pull complete 
111ba0647b87: Pull complete 
c1dce60f2f1a: Pull complete 
702ec598d0af: Pull complete 
4aba2fcbe869: Pull complete 
b26bbbd533e6: Downloading  33.83MB/113MB
7bd100a66c55: Download complete 
74149336419a: Download complete 
145ea1f01648: Download complete 
  • ⑤ 查看docker中的所有镜像,使用命令docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              9b51d9275906        2 weeks ago         547MB
  • ⑥ 使用镜像id删除相应镜像,使用命令docker rmi 对应的IMAGE ID

到此,docker安装、配置及其基本使用操作完成。

你可能感兴趣的:(后端,docker,linux,centos,mysql,java)