一、安装docker
1.1 如果之前有安装过docker ,请先卸载。卸载命令如下:
> yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-selinux \
> docker-engine-selinux \
> docker-engine \
> docker-ce
1.2 安装docker
> yum install -y yum-utils \
device-mapper-persistent-data \
lvm2 --skip-broken
1.2.1 更新本地镜像源
设置docker镜像源
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
yum install -y docker-ce
1.3.启动docker
# 启动docker前,一定要关闭防火墙后!!
systemctl stop firewalld
systemctl stop iptables
# 禁止开机启动防火墙
systemctl disable firewalld
#查看是否关闭防火墙
systemctl status firewalld
systemctl status iptables
# 通过命令启动docker:
systemctl start docker
# 停止docker服务
systemctl stop docker
# 重启docker服务
systemctl restart docker
# 查看docker版本
docker -v
1.4 配置镜像加速
#创建文件夹
sudo mkdir -p /etc/docker
> ##在文件夹内新建一个daemon.json文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://akchsmlh.mirror.aliyuncs.com"]
}
EOF
#重新加载文件
sudo systemctl daemon-reload
#重启docker
sudo systemctl restart docker
查看自己服务器中docker 镜像列表
docker images
# 查看正在运行的容器*
docker ps
# 查看所有的docker容器*
docker ps -a
二、docker安装mysql
2.1、拉取mysql 5.7镜像(以mysql5.7版本为例)
#拉取mysql5.7
docker pull mysql:5.7 入代码片
2.2 创建mysql 容器
> #创建三个目录
> mkdir -p /data/mysql5.7/conf /data/mysql5.7/logs /data/mysql5.7/data
> # 进入目录
> cd /data/mysql5.7/conf
> #创建mysql配置文件
> touch mysqld.cnf
# mysqld.cnf 配置文件内容
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
character-set-server = utf8
default-time-zone = '+08:00'
port =3306
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
relay-log = mysql-relay-bin
log-slave-updates= 1
server-id = 2
#log_bin = mysql-bin
#binlog_format=row
lower_case_table_names=1
max_connections=16380
wait_timeout=36000
interactive_timeout=36000
2.3 用docker命令启动mysql
docker run -p 3306:3306 --name mysql \
-v /data/mysql5.7/conf:/etc/mysql/conf.d \
-v /data/mysql5.7/logs:/logs \
-v /data/mysql5.7/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7
-p:表示端口映射,冒号左面的是我们的宿主机的端口,也就是我们虚拟机的端口,而右侧则表示的是 mysql容器内的端口
–name:是我们给mysql容器取的名字
-v:表示挂载路径,冒号左面的表示我们宿主机的挂载目录,也就是我们虚拟机所在的文件路径,冒号右 边则表是的是mysql容器在容器内部的路径,
-e 设置参数
-d: 后台运行容器
2.4 用docker命令查看mysql是否启动成功
三、docker安装redis
3.1 拉去镜像
docker pull redis
3.2 查看本地镜像
docker images
3.3 创建目录并启动容器
#创建
mkdir -p /data/redis/data
touch redis.conf
# 启动
docker run -p 6379:6379 --name redis -v /data/redis/data:/data \
-v /data/redis/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf