1. 查找Docker Hub上的mysql镜像

$ sudo docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8922                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3135                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   659                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   462                 [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   65
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   59
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      35                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
prom/mysqld-exporter                                                              23                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   22
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   17
circleci/mysql                    MySQL is a widely used, open-source relation…   16
mysql/mysql-router                MySQL Router provides transparent routing be…   14
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   4                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  3                                       [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]

2. 选择centos 7下的 mysql-57

$ sudo docker pull centos/mysql-57-centos7

3. 创建mysql数据库目录

$ sudo mkdir /data/mysql57
$ sudo chown mysql:mysql /data/mysql57
$ sudo chcon system_u:object_r:mysqld_db_t:s0 /data/mysql57

4. 使用mysql镜像

$ sudo docker run -p 3306:3306 --name mysql57 \
--volume=/data/mysql57/:/var/lib/mysql/data/ \
--restart=always \
-e MYSQL_ROOT_PASSWORD=123456 -d centos/mysql-57-centos7

5. 连接docker mysql

$ sudo docker exec -ti -u root mysql57 bash

  在RHEL 8/CentOS 8上安装Docker和Docker Compose的文章已经很多了, 不过都是要求禁用firewalld, 因为默认安装的 firewalld 会导致Docker容器DNS解析失败, 其实只需要修改一下 firewalld 的配置就可以正常工作的.

  在CentOS 8上安装Docker CE的步骤如下:

  1. 准备工作,CentOS 8的工作实例和具有sudo特权的用户

  2. 添加必要的Docker存储库

  我们将使用dnf config-manager实用程序添加Docker存储库。

$ sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

  3. 直接安装Docker CE会失败, 因为其要求 containerd.io 版本比较高, 然后会CentOS 8默认安装冲突, 所以需要手动安装 containerd.io 。

$ sudo dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.10-3.2.el7.x86_64.rpm

  4. 这时候就可以直接安装Docker CE了。

$ sudo dnf install docker-ce

  5. 为了强制DNS解析在Docker容器中起作用,必须禁用firewalld。

$ sudo systemctl disable firewalld

  6. 启用docker守护进程

$ sudo systemctl enable --now docker

  7. 为了无需使用 sudo 来启动 docker 我们需要把当前用户加入 docker 组.

$ sudo usermod -aG docker $USER

  以上就是CentOS 8上安装Docker CE的方法,不过这里面有个步骤就是要禁用firewalld,但是这个在生产环境下是绝对不可以的, 经查资料发现只需要开启伪装IP的功能就可以, 无需禁用firewalld。

$ sudo firewall-cmd --zone=public --add-masquerade --permanent
$ sudo firewall-cmd --reload
$ sudo systemctl restart docker

  正常安装未禁用firewalld下在Docker容器中无法解析域名.

$ docker run -it  --rm centos

[[email protected] /]# ping 192.168.0.5
PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data.
64 bytes from 192.168.0.5: icmp_seq=1 ttl=63 time=0.283 ms
^C
--- 192.168.0.5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.283/0.283/0.283/0.000 ms

[[email protected] /]# ping www.baidu.com
ping: www.baidu.com: Name or service not known

  启用伪装IP后即可正常访问.

$ docker run -it  --rm centos
[[email protected] /]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=54 time=22.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=54 time=21.9 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 21.915/22.122/22.330/0.255 ms