欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker安装教程(mysql)

程序员文章站 2022-06-16 22:44:17
...

1. 安装环境

  • 系统版本: Red Hat Enterprise Linux Server release 7.4 (Maipo)

2. 安装步骤

2.1 yum源码

配置系统yum源

  • 镜像yum源,挂在iso镜像,搭建本地yum,/etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///iso
enabled=1
gpgcheck=0
  • docker-ce的阿里yum源码
cd /etc/yum.repos.d/  &&  wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 安装docker-ce
yum clean all 
yum install docker-ce
  • 如果遇到下面报错,说明依赖有问题
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.3.19.03.4-3.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-19.03.4-3.el7.x86_64 需要
--> 正在处理依赖关系 containerd.io >= 1.2.2-3,它被软件包 3:docker-ce-19.03.4-3.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-cli,它被软件包 3:docker-ce-19.03.4-3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.2.10-3.2.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 containerd.io-1.2.10-3.2.el7.x86_64 需要
---> 软件包 docker-ce.x86_64.3.19.03.4-3.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-19.03.4-3.el7.x86_64 需要
---> 软件包 docker-ce-cli.x86_64.1.19.03.4-3.el7 将被 安装
--> 解决依赖关系完成
错误:软件包:containerd.io-1.2.10-3.2.el7.x86_64 (docker-ce-edge)
          需要:container-selinux >= 2:2.74
错误:软件包:3:docker-ce-19.03.4-3.el7.x86_64 (docker-ce-edge)
          需要:container-selinux >= 2:2.74
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest

将container-selinux安装包下载本地

wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.107-3.el7.noarch.rpm
yum localinstall container-selinux-2.107-3.el7.noarch.rpm
  • 安装docker-ce
 yum install docker-ce

2.2 启动docker服务

启动docker系统服务,通过systemctl status docker查看进程状态,正常为active(running)


systemctl start docker
systemctl enable docker
systemctl status docker

2.3 更改docker配置

2.3.1 查看docker信息
[08:13:18-localhost:~/docker]#  docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.4
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-693.el7.x86_64
 Operating System: Red Hat Enterprise Linux Server 7.4 (Maipo)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.796GiB
 Name: localhost.localdomain
 ID: DXLV:NRFT:N4NQ:SAF3:H4WR:5USY:2J6D:GZZ5:AS35:KOAH:TVQZ:TKB2
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
2.3.2 修改镜像存储位置

从docker info中可以看到docker root dir: /var/lib/docker下面

$ cat /etc/docker/daemon.json
{
    "graph": "/docker/images/"
}

$ systemctl restart docker
  • 查看是否改成功

$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.4
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-693.el7.x86_64
 Operating System: Red Hat Enterprise Linux Server 7.4 (Maipo)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.796GiB
 Name: localhost.localdomain
 ID: DXLV:NRFT:N4NQ:SAF3:H4WR:5USY:2J6D:GZZ5:AS35:KOAH:TVQZ:TKB2
 Docker Root Dir: /docker/images
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

2.4 安装mysql容器

借鉴技术文档https://hub.docker.com/_/mysql/

2.4.1 查找mysql镜像

[08:02:01-localhost:~/docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8707                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3040                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   643                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   63
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   53
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…   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                   16
circleci/mysql                    MySQL is a widely used, open-source relation…   15
mysql/mysql-router                MySQL Router provides transparent routing be…   13
arey/mysql-client                 Run a MySQL client from a docker container      11                                      [OK]
imega/mysql-client                Size: 36 MB, alpine:3.5, Mysql client: 10.1.…   8                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   6                                       [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   4                                       [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  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.4.2 拉取镜像


[08:19:44-localhost:~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
80369df48736: Pull complete
e8f52315cb10: Pull complete
cf2189b391fc: Pull complete
cc98f645c682: Pull complete
27a27ac83f74: Pull complete
fa1f04453414: Pull complete
d45bf7d22d33: Pull complete
3dbac26e409c: Pull complete
9017140fb8c1: Pull complete
b76dda2673ae: Pull complete
bea9eb46d12a: Pull complete
e1f050a38d0f: Pull complete
Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

查看当前镜像情况

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              c8ee894bd2bd        2 days ago          456MB
2.4.3 运行容器

$ docker run -p 3306:3306 --name mymysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs/:/logs -v /root/mysql/data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
a4a457d8225b58982d8c347fc8df474e4653c638154927d6b63ce998ec6c16eb
参数 解释
-p 表示端口映射,宿主机主机0.0.0.0:3306映射到容器3306
–name 容器的名称
-v volume,表示将容器中相应的目录持久化到宿主机本地
-e env 表示环境变量,修改容器中的环境变量配置
2.4.4 查看容器运行状态
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
a4a457d8225b        mysql               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   mymysql
2.4.5 登录docker容器终端
$ docker exec -it mymysql /bin/bash
  • 登录mysql

[email protected]:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

2.4.6 容器启动和停止
## 启动容器
docker start mysqlserver

## 停止容器   
docker stop mysqlserver