一、清除已有的docker
打开docker官网:https://docs.docker.com/engine/install/centos/
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
二、安装依赖的包
yum install -y yum-utils
三、安装docker
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
四、启动docker
systemctl start docker
五、查看docker版本
docker -v
六、设置docker开机自启动
systemctl enable docker
七、配置docker镜像加速服务
使用阿里云的镜像加速服务
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://XXXXXXXXXXXX.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
这里需要自己登录阿里云并找到:容器镜像服务--->镜像加速器
八、docker安装MySQL5.7
docker pull mysql:5.7
docker启动MySQL
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7
修改MySQL的字符编码
首先进入到/mydata/mysql/conf文件夹,然后vi my.cnf打开加入下面内容:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
保存之后重启MySQL;docker restart mysql
九、docker安装redis
docker pull redis
安装完成之后创建/mydata/redis/conf文件夹,并在conf文件夹下创建redis.conf文件
mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
docker启动redis
docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf
redis数据持久化,进入到/mydata/redis/conf/文件夹,vi redis.conf添加如下内容:
appendonly yes
重启redis:
docker restart redis
十、配置容器随docker启动而启动,也就是容器自启动
配置redis
docker update redis --restart=always
配置mysql
docker update mysql--restart=always